Skip to content

Commit

Permalink
wip setting defaults for getkin#206
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Jan 5, 2021
1 parent 4bb44a2 commit 485d661
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
34 changes: 29 additions & 5 deletions openapi3filter/validate_readonly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openapi3filter
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"testing"

Expand All @@ -25,6 +26,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": {
Expand All @@ -33,6 +44,10 @@ func TestValidatingRequestBodyWithReadOnlyProperty(t *testing.T) {
"type": "object",
"required": ["_id"],
"properties": {
"_": {
"type": "boolean",
"default": false
},
"_id": {
"type": "string",
"description": "Unique identifier for this object.",
Expand Down Expand Up @@ -60,16 +75,17 @@ func TestValidatingRequestBodyWithReadOnlyProperty(t *testing.T) {
}
`

type Request struct {
ID string `json:"_id"`
}

sl := openapi3.NewSwaggerLoader()
l, err := sl.LoadSwaggerFromData([]byte(spec))
require.NoError(t, err)
router := NewRouter().WithSwagger(l)

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))
Expand All @@ -85,4 +101,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
}
10 changes: 6 additions & 4 deletions openapi3filter/validate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func ValidateRequest(c context.Context, input *RequestValidationInput) error {
if err = ValidateParameter(c, input, parameter); err != nil && !options.MultiError {
return err
}

if err != nil {
me = append(me, err)
}
Expand All @@ -65,7 +64,6 @@ func ValidateRequest(c context.Context, input *RequestValidationInput) error {
if err = ValidateParameter(c, input, parameter.Value); err != nil && !options.MultiError {
return err
}

if err != nil {
me = append(me, err)
}
Expand All @@ -77,7 +75,6 @@ func ValidateRequest(c context.Context, input *RequestValidationInput) error {
if err = ValidateRequestBody(c, input, requestBody.Value); err != nil && !options.MultiError {
return err
}

if err != nil {
me = append(me, err)
}
Expand All @@ -97,7 +94,6 @@ func ValidateRequest(c context.Context, input *RequestValidationInput) error {
if err = ValidateSecurityRequirements(c, input, *security); err != nil && !options.MultiError {
return err
}

if err != nil {
me = append(me, err)
}
Expand Down Expand Up @@ -142,6 +138,12 @@ func ValidateParameter(c context.Context, input *RequestValidationInput, paramet
}
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 {
Expand Down

0 comments on commit 485d661

Please sign in to comment.