Skip to content

Commit

Permalink
openapi2conv: Convert response headers (getkin#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
SVilgelm committed Feb 3, 2022
1 parent a99f24a commit 0846d70
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
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

0 comments on commit 0846d70

Please sign in to comment.