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 Feb 1, 2022
1 parent a99f24a commit 09fc167
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 @@ -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": {
Expand All @@ -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.",
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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
}
10 changes: 6 additions & 4 deletions openapi3filter/validate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 09fc167

Please sign in to comment.