Skip to content

Commit 0846d70

Browse files
authored
openapi2conv: Convert response headers (#483)
1 parent a99f24a commit 0846d70

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

openapi2/openapi2.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,7 @@ func (response *Response) UnmarshalJSON(data []byte) error {
232232
}
233233

234234
type Header struct {
235-
openapi3.ExtensionProps
236-
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
237-
Description string `json:"description,omitempty" yaml:"description,omitempty"`
238-
Type string `json:"type,omitempty" yaml:"type,omitempty"`
235+
Parameter
239236
}
240237

241238
func (header *Header) MarshalJSON() ([]byte, error) {

openapi2conv/openapi2_conv.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,29 @@ func ToV3Response(response *openapi2.Response) (*openapi3.ResponseRef, error) {
424424
if schemaRef := response.Schema; schemaRef != nil {
425425
result.WithJSONSchemaRef(ToV3SchemaRef(schemaRef))
426426
}
427+
if headers := response.Headers; len(headers) > 0 {
428+
result.Headers = ToV3Headers(headers)
429+
}
427430
return &openapi3.ResponseRef{Value: result}, nil
428431
}
429432

433+
func ToV3Headers(defs map[string]*openapi2.Header) openapi3.Headers {
434+
headers := make(openapi3.Headers, len(defs))
435+
for name, header := range defs {
436+
header.In = ""
437+
header.Name = ""
438+
if ref := header.Ref; ref != "" {
439+
headers[name] = &openapi3.HeaderRef{Ref: ToV3Ref(ref)}
440+
} else {
441+
parameter, _, _, _ := ToV3Parameter(nil, &header.Parameter, nil)
442+
headers[name] = &openapi3.HeaderRef{Value: &openapi3.Header{
443+
Parameter: *parameter.Value,
444+
}}
445+
}
446+
}
447+
return headers
448+
}
449+
430450
func ToV3Schemas(defs map[string]*openapi3.SchemaRef) map[string]*openapi3.SchemaRef {
431451
schemas := make(map[string]*openapi3.SchemaRef, len(defs))
432452
for name, schema := range defs {
@@ -654,6 +674,7 @@ func FromV3(doc3 *openapi3.T) (*openapi2.T, error) {
654674
doc2.SecurityDefinitions = doc2SecuritySchemes
655675
}
656676
doc2.Security = FromV3SecurityRequirements(doc3.Security)
677+
657678
return doc2, nil
658679
}
659680

@@ -1048,9 +1069,30 @@ func FromV3Response(ref *openapi3.ResponseRef, components *openapi3.Components)
10481069
result.Schema, _ = FromV3SchemaRef(ct.Schema, components)
10491070
}
10501071
}
1072+
if headers := response.Headers; len(headers) > 0 {
1073+
var err error
1074+
if result.Headers, err = FromV3Headers(headers, components); err != nil {
1075+
return nil, err
1076+
}
1077+
}
10511078
return result, nil
10521079
}
10531080

1081+
func FromV3Headers(defs openapi3.Headers, components *openapi3.Components) (map[string]*openapi2.Header, error) {
1082+
headers := make(map[string]*openapi2.Header, len(defs))
1083+
for name, header := range defs {
1084+
ref := openapi3.ParameterRef{Ref: header.Ref, Value: &header.Value.Parameter}
1085+
parameter, err := FromV3Parameter(&ref, components)
1086+
if err != nil {
1087+
return nil, err
1088+
}
1089+
parameter.In = ""
1090+
parameter.Name = ""
1091+
headers[name] = &openapi2.Header{Parameter: *parameter}
1092+
}
1093+
return headers, nil
1094+
}
1095+
10541096
func FromV3SecurityScheme(doc3 *openapi3.T, ref *openapi3.SecuritySchemeRef) (*openapi2.SecurityScheme, error) {
10551097
securityScheme := ref.Value
10561098
if securityScheme == nil {

openapi2conv/openapi2_conv_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"encoding/json"
66
"testing"
77

8+
"github.com/stretchr/testify/require"
9+
810
"github.com/getkin/kin-openapi/openapi2"
911
"github.com/getkin/kin-openapi/openapi3"
10-
"github.com/stretchr/testify/require"
1112
)
1213

1314
func TestConvOpenAPIV3ToV2(t *testing.T) {
@@ -179,6 +180,13 @@ const exampleV2 = `
179180
"$ref": "#/definitions/Item"
180181
},
181182
"type": "array"
183+
},
184+
"headers": {
185+
"ETag": {
186+
"description": "The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource.",
187+
"type": "string",
188+
"maxLength": 64
189+
}
182190
}
183191
},
184192
"404": {
@@ -543,7 +551,16 @@ const exampleV3 = `
543551
}
544552
}
545553
},
546-
"description": "ok"
554+
"description": "ok",
555+
"headers": {
556+
"ETag": {
557+
"description": "The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource.",
558+
"schema": {
559+
"type": "string",
560+
"maxLength": 64
561+
}
562+
}
563+
}
547564
},
548565
"404": {
549566
"description": "404 response"

0 commit comments

Comments
 (0)