Skip to content

Commit fd4bae8

Browse files
authored
fix: openapi2conv respects produces field (#575)
1 parent 3244585 commit fd4bae8

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

openapi2conv/issue573_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package openapi2conv
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestIssue573(t *testing.T) {
10+
spec := []byte(`paths:
11+
/ping:
12+
get:
13+
produces:
14+
- application/toml
15+
- application/xml
16+
responses:
17+
200:
18+
schema:
19+
type: object
20+
properties:
21+
username:
22+
type: string
23+
description: The user name.
24+
post:
25+
responses:
26+
200:
27+
schema:
28+
type: object
29+
properties:
30+
username:
31+
type: string
32+
description: The user name.`)
33+
34+
v3, err := v2v3YAML(spec)
35+
require.NoError(t, err)
36+
37+
// Make sure the response content appears for each mime-type originally
38+
// appeared in "produces".
39+
pingGetContent := v3.Paths["/ping"].Get.Responses["200"].Value.Content
40+
require.Len(t, pingGetContent, 2)
41+
require.Contains(t, pingGetContent, "application/toml")
42+
require.Contains(t, pingGetContent, "application/xml")
43+
44+
// Is "produces" is not explicitly specified, default to "application/json".
45+
pingPostContent := v3.Paths["/ping"].Post.Responses["200"].Value.Content
46+
require.Len(t, pingPostContent, 1)
47+
require.Contains(t, pingPostContent, "application/json")
48+
}

openapi2conv/openapi2_conv.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func ToV3(doc2 *openapi2.T) (*openapi3.T, error) {
8484
if responses := doc2.Responses; len(responses) != 0 {
8585
doc3.Components.Responses = make(map[string]*openapi3.ResponseRef, len(responses))
8686
for k, response := range responses {
87-
r, err := ToV3Response(response)
87+
r, err := ToV3Response(response, doc2.Produces)
8888
if err != nil {
8989
return nil, err
9090
}
@@ -193,7 +193,7 @@ func ToV3Operation(doc2 *openapi2.T, components *openapi3.Components, pathItem *
193193
if responses := operation.Responses; responses != nil {
194194
doc3Responses := make(openapi3.Responses, len(responses))
195195
for k, response := range responses {
196-
doc3, err := ToV3Response(response)
196+
doc3, err := ToV3Response(response, operation.Produces)
197197
if err != nil {
198198
return nil, err
199199
}
@@ -413,7 +413,7 @@ func onlyOneReqBodyParam(bodies []*openapi3.RequestBodyRef, formDataSchemas map[
413413
return nil, nil
414414
}
415415

416-
func ToV3Response(response *openapi2.Response) (*openapi3.ResponseRef, error) {
416+
func ToV3Response(response *openapi2.Response, produces []string) (*openapi3.ResponseRef, error) {
417417
if ref := response.Ref; ref != "" {
418418
return &openapi3.ResponseRef{Ref: ToV3Ref(ref)}, nil
419419
}
@@ -422,8 +422,18 @@ func ToV3Response(response *openapi2.Response) (*openapi3.ResponseRef, error) {
422422
Description: &response.Description,
423423
ExtensionProps: response.ExtensionProps,
424424
}
425+
426+
// Default to "application/json" if "produces" is not specified.
427+
if len(produces) == 0 {
428+
produces = []string{"application/json"}
429+
}
430+
425431
if schemaRef := response.Schema; schemaRef != nil {
426-
result.WithJSONSchemaRef(ToV3SchemaRef(schemaRef))
432+
schema := ToV3SchemaRef(schemaRef)
433+
result.Content = make(openapi3.Content, len(produces))
434+
for _, mime := range produces {
435+
result.Content[mime] = openapi3.NewMediaType().WithSchemaRef(schema)
436+
}
427437
}
428438
if headers := response.Headers; len(headers) > 0 {
429439
result.Headers = ToV3Headers(headers)

0 commit comments

Comments
 (0)