kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

openapi2conv: v2 Response examples are lost after conversion

Open jspdown opened this issue 2 years ago • 0 comments

When translating v2 spec into v3, the openapi2.Response.examples property is not converted into openapi3.Response.Content[mime].Examples.

Reproducible test case:


func TestToV3Response_Examples(t *testing.T) {
	got, err := openapi2conv.ToV3Response(&openapi2.Response{
		Description: "my response",
		Schema: &openapi3.SchemaRef{
			Value: &openapi3.Schema{
				Type: "object",
			},
		},
		Examples: map[string]interface{}{
			"application/json": `{"status": "ok"}`,
		},
	}, []string{"application/json"})
	require.NoError(t, err)

	assert.Equal(t, &openapi3.ResponseRef{
		Value: &openapi3.Response{
			Description: ptrTo("my response"),
			Content: openapi3.Content{
				"application/json": &openapi3.MediaType{
					Schema: &openapi3.SchemaRef{
						Value: &openapi3.Schema{
							Type: "object",
						},
					},
					Examples: map[string]*openapi3.ExampleRef{
						"example": {
							Value: &openapi3.Example{Value: `{"status": "ok"}`},
						},
					},
				},
			},
		},
	}, got)
}

To fix this issue we should also remove the hard requirement of having a Schema to attempt adding a MediaType to the converted Response. The schema field in v2 is optional. One could be defining examples while not providing a schema.

jspdown avatar Jul 27 '23 09:07 jspdown