-
-
Notifications
You must be signed in to change notification settings - Fork 431
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
feat: support default value binding with allOfSchema #885
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
fenollp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
} | ||
|
||
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this definition should be schema validation error ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unable to find in the spec à justification to "forbid multiple schema defaults". But if you find one then yes let's impl this validation pass.
Otherwise let's continue to not return a validation error here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is not required according to the specifications.