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: Convert response headers #483

Merged
merged 2 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,33 @@ func ToV3Response(response *openapi2.Response) (*openapi3.ResponseRef, error) {
if schemaRef := response.Schema; schemaRef != nil {
result.WithJSONSchemaRef(ToV3SchemaRef(schemaRef))
}
if headers := response.Headers; len(headers) > 0 {
result.Headers = ToV3Headers(headers)
}
return &openapi3.ResponseRef{Value: result}, nil
}

func ToV3Headers(defs map[string]*openapi2.Header) openapi3.Headers {
headers := make(openapi3.Headers)
fenollp marked this conversation as resolved.
Show resolved Hide resolved
for name, header := range defs {
stripNonCustomExtensions(header.Extensions)
if ref := header.Ref; ref != "" {
headers[name] = &openapi3.HeaderRef{Ref: ToV3Ref(ref)}
} else {
headers[name] = &openapi3.HeaderRef{Value: &openapi3.Header{
Parameter: openapi3.Parameter{
ExtensionProps: header.ExtensionProps,
Description: header.Description,
Schema: &openapi3.SchemaRef{Value: &openapi3.Schema{
Type: header.Type,
SVilgelm marked this conversation as resolved.
Show resolved Hide resolved
}},
},
}}
}
}
return headers
}

func ToV3Schemas(defs map[string]*openapi3.SchemaRef) map[string]*openapi3.SchemaRef {
schemas := make(map[string]*openapi3.SchemaRef, len(defs))
for name, schema := range defs {
Expand Down Expand Up @@ -654,6 +678,7 @@ func FromV3(doc3 *openapi3.T) (*openapi2.T, error) {
doc2.SecurityDefinitions = doc2SecuritySchemes
}
doc2.Security = FromV3SecurityRequirements(doc3.Security)

return doc2, nil
}

Expand Down Expand Up @@ -1048,9 +1073,41 @@ func FromV3Response(ref *openapi3.ResponseRef, components *openapi3.Components)
result.Schema, _ = FromV3SchemaRef(ct.Schema, components)
}
}
if headers := response.Headers; len(headers) > 0 {
result.Headers = FromV3Headers(headers, components)
}
return result, nil
}

func FromV3Headers(defs openapi3.Headers, components *openapi3.Components) map[string]*openapi2.Header {
headers := make(map[string]*openapi2.Header)
fenollp marked this conversation as resolved.
Show resolved Hide resolved
for name, header := range defs {
if ref := header.Ref; ref != "" {
headers[name] = &openapi2.Header{Ref: FromV3Ref(ref)}
} else {
stripNonCustomExtensions(header.Value.Extensions)
var headerType string
if ref := header.Value.Schema.Ref; ref != "" {
name := getParameterNameFromNewRef(ref)
if val, ok := components.Schemas[name]; ok {
headerType = val.Value.Type
}
} else {
headerType = header.Value.Schema.Value.Type
}

headers[name] = &openapi2.Header{
ExtensionProps: header.Value.ExtensionProps,
Description: header.Value.Description,
Type: headerType,
}

}
}

return headers
}

func FromV3SecurityScheme(doc3 *openapi3.T, ref *openapi3.SecuritySchemeRef) (*openapi2.SecurityScheme, error) {
securityScheme := ref.Value
if securityScheme == nil {
Expand Down
19 changes: 17 additions & 2 deletions openapi2conv/openapi2_conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

func TestConvOpenAPIV3ToV2(t *testing.T) {
Expand Down Expand Up @@ -179,6 +180,12 @@ const exampleV2 = `
"$ref": "#/definitions/Item"
},
"type": "array"
},
"headers": {
"ETag": {
"description": "The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource.",
"type": "string"
}
}
},
"404": {
Expand Down Expand Up @@ -543,7 +550,15 @@ const exampleV3 = `
}
}
},
"description": "ok"
"description": "ok",
"headers": {
"ETag": {
"description": "The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource.",
"schema": {
"type": "string"
}
}
}
},
"404": {
"description": "404 response"
Expand Down