Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optional defaults #662

Merged
merged 3 commits into from Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 99 additions & 0 deletions openapi3filter/issue639_test.go
@@ -0,0 +1,99 @@
package openapi3filter

import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/routers/gorillamux"
)

func TestIssue639(t *testing.T) {
loader := openapi3.NewLoader()
ctx := loader.Context
spec := `
openapi: 3.0.0
info:
version: 1.0.0
title: Sample API
paths:
/items:
put:
requestBody:
content:
application/json:
schema:
properties:
testWithdefault:
default: false
type: boolean
testNoDefault:
type: boolean
type: object
responses:
'200':
description: Successful respons
`[1:]

doc, err := loader.LoadFromData([]byte(spec))
require.NoError(t, err)

err = doc.Validate(ctx)
require.NoError(t, err)

router, err := gorillamux.NewRouter(doc)
require.NoError(t, err)

tests := []struct {
name string
options *Options
expectedDefaultVal interface{}
}{
{
name: "no defaults are added to requests",
options: &Options{
SkipDefaultValueSet: true, // <== false by default, true means doesn't add default values to request body
fenollp marked this conversation as resolved.
Show resolved Hide resolved
},
expectedDefaultVal: nil,
},

{
name: "defaults are added to requests",
expectedDefaultVal: false,
},
}

for _, testcase := range tests {
t.Run(testcase.name, func(t *testing.T) {
body := "{\"testNoDefault\": true}"
httpReq, _ := http.NewRequest(http.MethodPut, "/items", strings.NewReader(body))
fenollp marked this conversation as resolved.
Show resolved Hide resolved
httpReq.Header.Set("Content-Type", "application/json")
require.NoError(t, err)

route, pathParams, err := router.FindRoute(httpReq)
require.NoError(t, err)

requestValidationInput := &RequestValidationInput{
Request: httpReq,
PathParams: pathParams,
Route: route,
Options: testcase.options,
}
err = ValidateRequest(ctx, requestValidationInput)
require.NoError(t, err)
bodyAfterValidation, err := ioutil.ReadAll(httpReq.Body)
require.NoError(t, err)

raw := map[string]interface{}{}
err = json.Unmarshal(bodyAfterValidation, &raw)
require.NoError(t, err)
require.Equal(t, testcase.expectedDefaultVal,
raw["testWithdefault"], "default value must not be included")
})
}
}
4 changes: 4 additions & 0 deletions openapi3filter/options.go
Expand Up @@ -21,4 +21,8 @@ type Options struct {

// See NoopAuthenticationFunc
AuthenticationFunc AuthenticationFunc

// Indicates whether default values are set in the
// request. If true, then they are not set
SkipDefaultValueSet bool
fenollp marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 3 additions & 1 deletion openapi3filter/validate_request.go
Expand Up @@ -258,7 +258,9 @@ func ValidateRequestBody(ctx context.Context, input *RequestValidationInput, req
defaultsSet := false
opts := make([]openapi3.SchemaValidationOption, 0, 3) // 3 potential opts here
opts = append(opts, openapi3.VisitAsRequest())
opts = append(opts, openapi3.DefaultsSet(func() { defaultsSet = true }))
if !options.SkipDefaultValueSet {
opts = append(opts, openapi3.DefaultsSet(func() { defaultsSet = true }))
}
if options.MultiError {
opts = append(opts, openapi3.MultiErrors())
}
Expand Down