Skip to content

Commit

Permalink
Fix openapi3 validation: path param must be required (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasayxtx authored Feb 21, 2022
1 parent 124b07e commit 7027e1b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
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

0 comments on commit 7027e1b

Please sign in to comment.