Skip to content

Commit

Permalink
fix: openapi2conv respects produces field (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirhaas authored Jul 31, 2022
1 parent 3244585 commit fd4bae8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
48 changes: 48 additions & 0 deletions openapi2conv/issue573_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package openapi2conv

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestIssue573(t *testing.T) {
spec := []byte(`paths:
/ping:
get:
produces:
- application/toml
- application/xml
responses:
200:
schema:
type: object
properties:
username:
type: string
description: The user name.
post:
responses:
200:
schema:
type: object
properties:
username:
type: string
description: The user name.`)

v3, err := v2v3YAML(spec)
require.NoError(t, err)

// Make sure the response content appears for each mime-type originally
// appeared in "produces".
pingGetContent := v3.Paths["/ping"].Get.Responses["200"].Value.Content
require.Len(t, pingGetContent, 2)
require.Contains(t, pingGetContent, "application/toml")
require.Contains(t, pingGetContent, "application/xml")

// Is "produces" is not explicitly specified, default to "application/json".
pingPostContent := v3.Paths["/ping"].Post.Responses["200"].Value.Content
require.Len(t, pingPostContent, 1)
require.Contains(t, pingPostContent, "application/json")
}
18 changes: 14 additions & 4 deletions openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func ToV3(doc2 *openapi2.T) (*openapi3.T, error) {
if responses := doc2.Responses; len(responses) != 0 {
doc3.Components.Responses = make(map[string]*openapi3.ResponseRef, len(responses))
for k, response := range responses {
r, err := ToV3Response(response)
r, err := ToV3Response(response, doc2.Produces)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -193,7 +193,7 @@ func ToV3Operation(doc2 *openapi2.T, components *openapi3.Components, pathItem *
if responses := operation.Responses; responses != nil {
doc3Responses := make(openapi3.Responses, len(responses))
for k, response := range responses {
doc3, err := ToV3Response(response)
doc3, err := ToV3Response(response, operation.Produces)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -413,7 +413,7 @@ func onlyOneReqBodyParam(bodies []*openapi3.RequestBodyRef, formDataSchemas map[
return nil, nil
}

func ToV3Response(response *openapi2.Response) (*openapi3.ResponseRef, error) {
func ToV3Response(response *openapi2.Response, produces []string) (*openapi3.ResponseRef, error) {
if ref := response.Ref; ref != "" {
return &openapi3.ResponseRef{Ref: ToV3Ref(ref)}, nil
}
Expand All @@ -422,8 +422,18 @@ func ToV3Response(response *openapi2.Response) (*openapi3.ResponseRef, error) {
Description: &response.Description,
ExtensionProps: response.ExtensionProps,
}

// Default to "application/json" if "produces" is not specified.
if len(produces) == 0 {
produces = []string{"application/json"}
}

if schemaRef := response.Schema; schemaRef != nil {
result.WithJSONSchemaRef(ToV3SchemaRef(schemaRef))
schema := ToV3SchemaRef(schemaRef)
result.Content = make(openapi3.Content, len(produces))
for _, mime := range produces {
result.Content[mime] = openapi3.NewMediaType().WithSchemaRef(schema)
}
}
if headers := response.Headers; len(headers) > 0 {
result.Headers = ToV3Headers(headers)
Expand Down

0 comments on commit fd4bae8

Please sign in to comment.