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

Fix openapi3 validation: path param must be required #490

Merged
merged 1 commit into from
Feb 21, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions openapi3/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ paths:
parameters:
- name: id,
in: path
required: true
schema:
type: string
responses:
Expand Down
4 changes: 4 additions & 0 deletions openapi3/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func (value *Parameter) Validate(ctx context.Context) error {
return fmt.Errorf("parameter can't have 'in' value %q", value.In)
}

if in == ParameterInPath && !value.Required {
return fmt.Errorf("path parameter %q must be required", value.Name)
}

// Validate a parameter's serialization method.
sm, err := value.SerializationMethod()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ func TestDecodeParameter(t *testing.T) {
path := "/test"
if tc.path != "" {
path += "/{" + tc.param.Name + "}"
tc.param.Required = true
}

info := &openapi3.Info{
Expand Down
7 changes: 4 additions & 3 deletions openapi3filter/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ func TestFilter(t *testing.T) {
Parameters: openapi3.Parameters{
{
Value: &openapi3.Parameter{
In: "path",
Name: "pathArg",
Schema: openapi3.NewStringSchema().WithMaxLength(2).NewRef(),
In: "path",
Name: "pathArg",
Schema: openapi3.NewStringSchema().WithMaxLength(2).NewRef(),
Required: true,
},
},
{
Expand Down