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 OpenAPI 3 validation: request body content is required #498

Merged
merged 1 commit into from
Feb 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion openapi3/openapi3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ components:
requestBodies:
someRequestBody:
description: Some request body
content: {}
responses:
someResponse:
description: Some response
Expand Down Expand Up @@ -194,7 +195,8 @@ var specJSON = []byte(`
},
"requestBodies": {
"someRequestBody": {
"description": "Some request body"
"description": "Some request body",
"content": {}
}
},
"responses": {
Expand Down Expand Up @@ -253,6 +255,7 @@ func spec() *T {
}
requestBody := &RequestBody{
Description: "Some request body",
Content: NewContent(),
}
responseDescription := "Some response"
response := &Response{
Expand Down
10 changes: 4 additions & 6 deletions openapi3/request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type RequestBody struct {
ExtensionProps
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
Content Content `json:"content,omitempty" yaml:"content,omitempty"`
Content Content `json:"content" yaml:"content"`
}

func NewRequestBody() *RequestBody {
Expand Down Expand Up @@ -98,10 +98,8 @@ func (requestBody *RequestBody) UnmarshalJSON(data []byte) error {
}

func (value *RequestBody) Validate(ctx context.Context) error {
if v := value.Content; v != nil {
if err := v.Validate(ctx); err != nil {
return err
}
if value.Content == nil {
return fmt.Errorf("content of the request body is required")
}
return nil
return value.Content.Validate(ctx)
}