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 all commits
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
5 changes: 1 addition & 4 deletions openapi2/openapi2.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,7 @@ func (response *Response) UnmarshalJSON(data []byte) error {
}

type Header struct {
openapi3.ExtensionProps
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Parameter
}

func (header *Header) MarshalJSON() ([]byte, error) {
Expand Down
42 changes: 42 additions & 0 deletions openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,29 @@ 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, len(defs))
for name, header := range defs {
header.In = ""
header.Name = ""
if ref := header.Ref; ref != "" {
headers[name] = &openapi3.HeaderRef{Ref: ToV3Ref(ref)}
} else {
parameter, _, _, _ := ToV3Parameter(nil, &header.Parameter, nil)
headers[name] = &openapi3.HeaderRef{Value: &openapi3.Header{
Parameter: *parameter.Value,
}}
}
}
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 +674,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 +1069,30 @@ func FromV3Response(ref *openapi3.ResponseRef, components *openapi3.Components)
result.Schema, _ = FromV3SchemaRef(ct.Schema, components)
}
}
if headers := response.Headers; len(headers) > 0 {
var err error
if result.Headers, err = FromV3Headers(headers, components); err != nil {
return nil, err
}
}
return result, nil
}

func FromV3Headers(defs openapi3.Headers, components *openapi3.Components) (map[string]*openapi2.Header, error) {
headers := make(map[string]*openapi2.Header, len(defs))
for name, header := range defs {
ref := openapi3.ParameterRef{Ref: header.Ref, Value: &header.Value.Parameter}
parameter, err := FromV3Parameter(&ref, components)
if err != nil {
return nil, err
}
parameter.In = ""
parameter.Name = ""
headers[name] = &openapi2.Header{Parameter: *parameter}
}
return headers, nil
}

func FromV3SecurityScheme(doc3 *openapi3.T, ref *openapi3.SecuritySchemeRef) (*openapi2.SecurityScheme, error) {
securityScheme := ref.Value
if securityScheme == nil {
Expand Down
21 changes: 19 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,13 @@ 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",
"maxLength": 64
}
}
},
"404": {
Expand Down Expand Up @@ -543,7 +551,16 @@ 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",
"maxLength": 64
}
}
}
},
"404": {
"description": "404 response"
Expand Down