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

openapi2conv does not respect "produces" keyword #573

Closed
nirhaas opened this issue Jul 23, 2022 · 1 comment · Fixed by #575
Closed

openapi2conv does not respect "produces" keyword #573

nirhaas opened this issue Jul 23, 2022 · 1 comment · Fixed by #575

Comments

@nirhaas
Copy link
Contributor

nirhaas commented Jul 23, 2022

It seems like when using openapi2conv.ToV3, the result content-type is always JSON. Please see the example (also pasted below). The output is application/json while the produces field is application/toml.

I guess that this is because that hard-coded "WithJsonSchemaRef" here: https://github.com/getkin/kin-openapi/blob/master/openapi2conv/openapi2_conv.go#L426 or somewhere in this area.

Hope I'm correctly defining a content-type for response in openapi v2, and using openapi2conv correctly as well.

Thanks.


package main

import (
	"fmt"

	"github.com/getkin/kin-openapi/openapi2"
	"github.com/getkin/kin-openapi/openapi2conv"
	"github.com/getkin/kin-openapi/openapi3"
	"github.com/invopop/yaml"
)

func v2v3YAML(spec2 []byte) (doc3 *openapi3.T, err error) {
	var doc2 openapi2.T
	if err = yaml.Unmarshal(spec2, &doc2); err != nil {
		return
	}
	doc3, err = openapi2conv.ToV3(&doc2)
	return
}

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

	v3, err := v2v3YAML(spec)
	if err != nil {
		panic(err)
	}

	yamlV3, err := yaml.Marshal(v3)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(yamlV3))
}

Got:

components: {}
info:
    title: ""
    version: ""
openapi: 3.0.3
paths:
    /ping:
        get:
            responses:
                "200":
                    content:
                        application/json:
                            schema:
                                properties:
                                    username:
                                        description: The user name.
                                        type: string
                                type: object
                    description: ""

Expected:

components: {}
info:
    title: ""
    version: ""
openapi: 3.0.3
paths:
    /ping:
        get:
            responses:
                "200":
                    content:
                        application/toml:
                            schema:
                                properties:
                                    username:
                                        description: The user name.
                                        type: string
                                type: object
                    description: ""
@fenollp
Copy link
Collaborator

fenollp commented Jul 29, 2022

Your report seems correct! I'd suggest you edit that place in the code you found to something like:

-		result.WithJSONSchemaRef(ToV3SchemaRef(schemaRef))
+		// Read `produces` from sibling `operation>responses` field `operation>produces`
+		schema := ToV3SchemaRef(schemaRef)
+		result.Content = make(Content, len(produces))
+		for _, mime := range produces {
+			result.Content[mime] = NewMediaType().WithSchema(schema)
+		}

and add your example document as a regression test! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants