Skip to content
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(openapi3filter): add ExludeRequestQuery #886

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/docs/openapi3filter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ type Options struct {
// Set ExcludeRequestBody so ValidateRequest skips request body validation
ExcludeRequestBody bool

// Set ExcludeRequestQueryParams so ValidateRequest skips request query params validation
ExcludeRequestQueryParams bool

// Set ExcludeResponseBody so ValidateResponse skips response body validation
ExcludeResponseBody bool

Expand Down
3 changes: 3 additions & 0 deletions openapi3filter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ type Options struct {
// Set ExcludeRequestBody so ValidateRequest skips request body validation
ExcludeRequestBody bool

// Set ExcludeRequestQueryParams so ValidateRequest skips request query params validation
ExcludeRequestQueryParams bool

// Set ExcludeResponseBody so ValidateResponse skips response body validation
ExcludeResponseBody bool

Expand Down
3 changes: 3 additions & 0 deletions openapi3filter/validate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func ValidateRequest(ctx context.Context, input *RequestValidationInput) (err er

// For each parameter of the Operation
for _, parameter := range operationParameters {
if options.ExcludeRequestQueryParams && parameter.Value.In == openapi3.ParameterInQuery {
continue
}
if err = ValidateParameter(ctx, input, parameter.Value); err != nil && !options.MultiError {
return
}
Expand Down
46 changes: 46 additions & 0 deletions openapi3filter/validate_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,49 @@ components:
})
}
}

func TestValidateRequestExcludeQueryParams(t *testing.T) {
const spec = `
openapi: 3.0.0
info:
title: 'Validator'
version: 0.0.1
paths:
/category:
post:
parameters:
- name: category
in: query
schema:
type: integer
required: true
responses:
'200':
description: Ok
`
req, err := http.NewRequest(http.MethodPost, "/category?category=foo", nil)
require.NoError(t, err)
router := setupTestRouter(t, spec)
route, pathParams, err := router.FindRoute(req)
require.NoError(t, err)

err = ValidateRequest(context.Background(), &RequestValidationInput{
Request: req,
PathParams: pathParams,
Route: route,
Options: &Options{
ExcludeRequestQueryParams: true,
},
})
require.NoError(t, err)

err = ValidateRequest(context.Background(), &RequestValidationInput{
Request: req,
PathParams: pathParams,
Route: route,
Options: &Options{
ExcludeRequestQueryParams: false,
},
})
require.Error(t, err)
}