Skip to content

Commit 8459893

Browse files
authored
various fixes mainly to openapi2<->openapi3 conversion (#239)
1 parent 62affaa commit 8459893

7 files changed

Lines changed: 635 additions & 401 deletions

File tree

openapi2/openapi2.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
type Swagger struct {
1919
openapi3.ExtensionProps
20+
Swagger string `json:"swagger"`
2021
Info openapi3.Info `json:"info"`
2122
ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty"`
2223
Schemes []string `json:"schemes,omitempty"`
@@ -168,27 +169,30 @@ type Parameters []*Parameter
168169

169170
type Parameter struct {
170171
openapi3.ExtensionProps
171-
Ref string `json:"$ref,omitempty"`
172-
In string `json:"in,omitempty"`
173-
Name string `json:"name,omitempty"`
174-
Description string `json:"description,omitempty"`
175-
Required bool `json:"required,omitempty"`
176-
UniqueItems bool `json:"uniqueItems,omitempty"`
177-
ExclusiveMin bool `json:"exclusiveMinimum,omitempty"`
178-
ExclusiveMax bool `json:"exclusiveMaximum,omitempty"`
179-
Schema *openapi3.SchemaRef `json:"schema,omitempty"`
180-
Type string `json:"type,omitempty"`
181-
Format string `json:"format,omitempty"`
182-
Enum []interface{} `json:"enum,omitempty"`
183-
Minimum *float64 `json:"minimum,omitempty"`
184-
Maximum *float64 `json:"maximum,omitempty"`
185-
MinLength uint64 `json:"minLength,omitempty"`
186-
MaxLength *uint64 `json:"maxLength,omitempty"`
187-
Pattern string `json:"pattern,omitempty"`
188-
Items *openapi3.SchemaRef `json:"items,omitempty"`
189-
MinItems uint64 `json:"minItems,omitempty"`
190-
MaxItems *uint64 `json:"maxItems,omitempty"`
191-
Default interface{} `json:"default,omitempty"`
172+
Ref string `json:"$ref,omitempty"`
173+
In string `json:"in,omitempty"`
174+
Name string `json:"name,omitempty"`
175+
Description string `json:"description,omitempty"`
176+
CollectionFormat string `json:"collectionFormat,omitempty"`
177+
Type string `json:"type,omitempty"`
178+
Format string `json:"format,omitempty"`
179+
Pattern string `json:"pattern,omitempty"`
180+
AllowEmptyValue bool `json:"allowEmptyValue,omitempty"`
181+
Required bool `json:"required,omitempty"`
182+
UniqueItems bool `json:"uniqueItems,omitempty"`
183+
ExclusiveMin bool `json:"exclusiveMinimum,omitempty"`
184+
ExclusiveMax bool `json:"exclusiveMaximum,omitempty"`
185+
Schema *openapi3.SchemaRef `json:"schema,omitempty"`
186+
Items *openapi3.SchemaRef `json:"items,omitempty"`
187+
Enum []interface{} `json:"enum,omitempty"`
188+
MultipleOf *float64 `json:"multipleOf,omitempty"`
189+
Minimum *float64 `json:"minimum,omitempty"`
190+
Maximum *float64 `json:"maximum,omitempty"`
191+
MaxLength *uint64 `json:"maxLength,omitempty"`
192+
MaxItems *uint64 `json:"maxItems,omitempty"`
193+
MinLength uint64 `json:"minLength,omitempty"`
194+
MinItems uint64 `json:"minItems,omitempty"`
195+
Default interface{} `json:"default,omitempty"`
192196
}
193197

194198
func (parameter *Parameter) MarshalJSON() ([]byte, error) {

openapi2/testdata/swagger.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

openapi2conv/issue187_test.go

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77

88
"github.com/getkin/kin-openapi/openapi2"
99
"github.com/getkin/kin-openapi/openapi3"
10+
"github.com/ghodss/yaml"
1011
"github.com/stretchr/testify/require"
1112
)
1213

13-
func v2v3(spec2 []byte) (doc3 *openapi3.Swagger, err error) {
14+
func v2v3JSON(spec2 []byte) (doc3 *openapi3.Swagger, err error) {
1415
var doc2 openapi2.Swagger
1516
if err = json.Unmarshal(spec2, &doc2); err != nil {
1617
return
@@ -19,6 +20,15 @@ func v2v3(spec2 []byte) (doc3 *openapi3.Swagger, err error) {
1920
return
2021
}
2122

23+
func v2v3YAML(spec2 []byte) (doc3 *openapi3.Swagger, err error) {
24+
var doc2 openapi2.Swagger
25+
if err = yaml.Unmarshal(spec2, &doc2); err != nil {
26+
return
27+
}
28+
doc3, err = ToV3Swagger(&doc2)
29+
return
30+
}
31+
2232
func TestIssue187(t *testing.T) {
2333
spec := `
2434
{
@@ -87,12 +97,71 @@ func TestIssue187(t *testing.T) {
8797
}
8898
}
8999
`
90-
doc3, err := v2v3([]byte(spec))
100+
doc3, err := v2v3JSON([]byte(spec))
91101
require.NoError(t, err)
92102

93103
spec3, err := json.Marshal(doc3)
94104
require.NoError(t, err)
95-
const expected = `{"components":{"schemas":{"model.ProductSearchAttributeRequest":{"properties":{"filterField":{"type":"string"},"filterKey":{"type":"string"},"type":{"type":"string"},"values":{"$ref":"#/components/schemas/model.ProductSearchAttributeValueRequest"}},"title":"model.ProductSearchAttributeRequest","type":"object"},"model.ProductSearchAttributeValueRequest":{"properties":{"imageUrl":{"type":"string"},"text":{"type":"string"}},"title":"model.ProductSearchAttributeValueRequest","type":"object"}}},"info":{"contact":{"email":"test@test.com","name":"Test"},"description":"Test Golang Application","title":"Test","version":"1.0"},"openapi":"3.0.2","paths":{"/me":{"get":{"operationId":"someTest","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/model.ProductSearchAttributeRequest"}}},"description":"successful operation"}},"summary":"Some test","tags":["probe"]}}}}`
105+
const expected = `{"components":{"schemas":{"model.ProductSearchAttributeRequest":{"properties":{"filterField":{"type":"string"},"filterKey":{"type":"string"},"type":{"type":"string"},"values":{"$ref":"#/components/schemas/model.ProductSearchAttributeValueRequest"}},"title":"model.ProductSearchAttributeRequest","type":"object"},"model.ProductSearchAttributeValueRequest":{"properties":{"imageUrl":{"type":"string"},"text":{"type":"string"}},"title":"model.ProductSearchAttributeValueRequest","type":"object"}}},"info":{"contact":{"email":"test@test.com","name":"Test"},"description":"Test Golang Application","title":"Test","version":"1.0"},"openapi":"3.0.3","paths":{"/me":{"get":{"operationId":"someTest","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/model.ProductSearchAttributeRequest"}}},"description":"successful operation"}},"summary":"Some test","tags":["probe"]}}}}`
106+
require.Equal(t, string(spec3), expected)
107+
108+
err = doc3.Validate(context.Background())
109+
require.NoError(t, err)
110+
}
111+
112+
func TestIssue237(t *testing.T) {
113+
spec := `
114+
swagger: '2.0'
115+
info:
116+
version: 1.0.0
117+
title: title
118+
paths:
119+
/test:
120+
get:
121+
parameters:
122+
- in: body
123+
schema:
124+
$ref: '#/definitions/TestRef'
125+
responses:
126+
'200':
127+
description: description
128+
definitions:
129+
TestRef:
130+
type: object
131+
allOf:
132+
- $ref: '#/definitions/TestRef2'
133+
TestRef2:
134+
type: object
135+
`
136+
doc3, err := v2v3YAML([]byte(spec))
137+
require.NoError(t, err)
138+
139+
spec3, err := yaml.Marshal(doc3)
140+
require.NoError(t, err)
141+
const expected = `components:
142+
schemas:
143+
TestRef:
144+
allOf:
145+
- $ref: '#/components/schemas/TestRef2'
146+
type: object
147+
TestRef2:
148+
type: object
149+
info:
150+
title: title
151+
version: 1.0.0
152+
openapi: 3.0.3
153+
paths:
154+
/test:
155+
get:
156+
requestBody:
157+
content:
158+
application/json:
159+
schema:
160+
$ref: '#/components/schemas/TestRef'
161+
responses:
162+
"200":
163+
description: description
164+
`
96165
require.Equal(t, string(spec3), expected)
97166

98167
err = doc3.Validate(context.Background())

0 commit comments

Comments
 (0)