Skip to content

Commit

Permalink
feat: support default value binding with allOfSchema (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
podhmo committed Dec 9, 2023
1 parent c00d180 commit 62bf0f7
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 17 deletions.
113 changes: 113 additions & 0 deletions openapi3filter/issue884_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package openapi3filter

import (
"net/http"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func TestIssue884(t *testing.T) {
loader := openapi3.NewLoader()
ctx := loader.Context
spec := `
openapi: 3.0.0
info:
version: 1.0.0
title: Sample API
components:
schemas:
TaskSortEnum:
enum:
- createdAt
- -createdAt
- updatedAt
- -updatedAt
paths:
/tasks:
get:
operationId: ListTask
parameters:
- in: query
name: withDefault
schema:
allOf:
- $ref: '#/components/schemas/TaskSortEnum'
- default: -createdAt
- in: query
name: withoutDefault
schema:
allOf:
- $ref: '#/components/schemas/TaskSortEnum'
- in: query
name: withManyDefaults
schema:
allOf:
- default: -updatedAt
- $ref: '#/components/schemas/TaskSortEnum'
- default: -createdAt
responses:
'200':
description: Successful response
`[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
expectedQuery url.Values
}{
{
name: "no defaults are added to requests",
options: &Options{
SkipSettingDefaults: true,
},
expectedQuery: url.Values{},
},

{
name: "defaults are added to requests",
expectedQuery: url.Values{
"withDefault": []string{"-createdAt"},
"withManyDefaults": []string{"-updatedAt"}, // first default is win
},
},
}

for _, testcase := range tests {
t.Run(testcase.name, func(t *testing.T) {
httpReq, err := http.NewRequest(http.MethodGet, "/tasks", nil)
require.NoError(t, err)
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)

q := httpReq.URL.Query()
assert.Equal(t, testcase.expectedQuery, q)
})
}
}
43 changes: 26 additions & 17 deletions openapi3filter/validate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,33 @@ func ValidateParameter(ctx context.Context, input *RequestValidationInput, param
}

// Set default value if needed
if !options.SkipSettingDefaults && value == nil && schema != nil && schema.Default != nil {
if !options.SkipSettingDefaults && value == nil && schema != nil {
value = schema.Default
req := input.Request
switch parameter.In {
case openapi3.ParameterInPath:
// Path parameters are required.
// Next check `parameter.Required && !found` will catch this.
case openapi3.ParameterInQuery:
q := req.URL.Query()
q.Add(parameter.Name, fmt.Sprintf("%v", value))
req.URL.RawQuery = q.Encode()
case openapi3.ParameterInHeader:
req.Header.Add(parameter.Name, fmt.Sprintf("%v", value))
case openapi3.ParameterInCookie:
req.AddCookie(&http.Cookie{
Name: parameter.Name,
Value: fmt.Sprintf("%v", value),
})
for _, subSchema := range schema.AllOf {
if subSchema.Value.Default != nil {
value = subSchema.Value.Default
break // This is not a validation of the schema itself, so use the first default value.
}
}

if value != nil {
req := input.Request
switch parameter.In {
case openapi3.ParameterInPath:
// Path parameters are required.
// Next check `parameter.Required && !found` will catch this.
case openapi3.ParameterInQuery:
q := req.URL.Query()
q.Add(parameter.Name, fmt.Sprintf("%v", value))
req.URL.RawQuery = q.Encode()
case openapi3.ParameterInHeader:
req.Header.Add(parameter.Name, fmt.Sprintf("%v", value))
case openapi3.ParameterInCookie:
req.AddCookie(&http.Cookie{
Name: parameter.Name,
Value: fmt.Sprintf("%v", value),
})
}
}
}

Expand Down

0 comments on commit 62bf0f7

Please sign in to comment.