diff --git a/openapi3filter/validate_readonly_test.go b/openapi3filter/validate_readonly_test.go index 454a927e9..8b7ccb7ef 100644 --- a/openapi3filter/validate_readonly_test.go +++ b/openapi3filter/validate_readonly_test.go @@ -3,6 +3,7 @@ package openapi3filter import ( "bytes" "encoding/json" + "io/ioutil" "net/http" "testing" @@ -26,6 +27,16 @@ func TestValidatingRequestBodyWithReadOnlyProperty(t *testing.T) { "/accounts": { "post": { "description": "Create a new account", + "parameters": [ + { + "in": "query", + "name": "q", + "schema": { + "type": "string", + "default": "Q" + } + } + ], "requestBody": { "required": true, "content": { @@ -34,6 +45,10 @@ func TestValidatingRequestBodyWithReadOnlyProperty(t *testing.T) { "type": "object", "required": ["_id"], "properties": { + "_": { + "type": "boolean", + "default": false + }, "_id": { "type": "string", "description": "Unique identifier for this object.", @@ -61,10 +76,6 @@ func TestValidatingRequestBodyWithReadOnlyProperty(t *testing.T) { } ` - type Request struct { - ID string `json:"_id"` - } - sl := openapi3.NewLoader() doc, err := sl.LoadFromData([]byte(spec)) require.NoError(t, err) @@ -73,7 +84,12 @@ func TestValidatingRequestBodyWithReadOnlyProperty(t *testing.T) { router, err := legacyrouter.NewRouter(doc) require.NoError(t, err) - b, err := json.Marshal(Request{ID: "bt6kdc3d0cvp6u8u3ft0"}) + b, err := json.Marshal(struct { + Blank bool `json:"_,omitempty"` + ID string `json:"_id"` + }{ + ID: "bt6kdc3d0cvp6u8u3ft0", + }) require.NoError(t, err) httpReq, err := http.NewRequest(http.MethodPost, "/accounts", bytes.NewReader(b)) @@ -89,4 +105,12 @@ func TestValidatingRequestBodyWithReadOnlyProperty(t *testing.T) { Route: route, }) require.NoError(t, err) + + // Unset default values in body were set + validatedReqBody, err := ioutil.ReadAll(httpReq.Body) + require.NoError(t, err) + require.JSONEq(t, `{"_":false,"_id":"bt6kdc3d0cvp6u8u3ft0"}`, string(validatedReqBody)) + // Unset default values in URL were set + // Unset default values in headers were set + // Unset default values in cookies were set } diff --git a/openapi3filter/validate_request.go b/openapi3filter/validate_request.go index 990b299ef..5e5a0e6a5 100644 --- a/openapi3filter/validate_request.go +++ b/openapi3filter/validate_request.go @@ -52,7 +52,6 @@ func ValidateRequest(ctx context.Context, input *RequestValidationInput) error { if err = ValidateParameter(ctx, input, parameter); err != nil && !options.MultiError { return err } - if err != nil { me = append(me, err) } @@ -63,7 +62,6 @@ func ValidateRequest(ctx context.Context, input *RequestValidationInput) error { if err = ValidateParameter(ctx, input, parameter.Value); err != nil && !options.MultiError { return err } - if err != nil { me = append(me, err) } @@ -75,7 +73,6 @@ func ValidateRequest(ctx context.Context, input *RequestValidationInput) error { if err = ValidateRequestBody(ctx, input, requestBody.Value); err != nil && !options.MultiError { return err } - if err != nil { me = append(me, err) } @@ -92,7 +89,6 @@ func ValidateRequest(ctx context.Context, input *RequestValidationInput) error { if err = ValidateSecurityRequirements(ctx, input, *security); err != nil && !options.MultiError { return err } - if err != nil { me = append(me, err) } @@ -137,6 +133,12 @@ func ValidateParameter(ctx context.Context, input *RequestValidationInput, param } schema = parameter.Schema.Value } + + // Maybe use default value + if value == nil && schema != nil { + value = schema.Default + } + // Validate a parameter's value. if value == nil { if parameter.Required {