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

various fixes mainly to openapi2<->openapi3 conversion #239

Merged
merged 4 commits into from
Jul 17, 2020
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
46 changes: 25 additions & 21 deletions openapi2/openapi2.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

type Swagger struct {
openapi3.ExtensionProps
Swagger string `json:"swagger"`
Info openapi3.Info `json:"info"`
ExternalDocs *openapi3.ExternalDocs `json:"externalDocs,omitempty"`
Schemes []string `json:"schemes,omitempty"`
Expand Down Expand Up @@ -168,27 +169,30 @@ type Parameters []*Parameter

type Parameter struct {
openapi3.ExtensionProps
Ref string `json:"$ref,omitempty"`
In string `json:"in,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
UniqueItems bool `json:"uniqueItems,omitempty"`
ExclusiveMin bool `json:"exclusiveMinimum,omitempty"`
ExclusiveMax bool `json:"exclusiveMaximum,omitempty"`
Schema *openapi3.SchemaRef `json:"schema,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Enum []interface{} `json:"enum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
MinLength uint64 `json:"minLength,omitempty"`
MaxLength *uint64 `json:"maxLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
Items *openapi3.SchemaRef `json:"items,omitempty"`
MinItems uint64 `json:"minItems,omitempty"`
MaxItems *uint64 `json:"maxItems,omitempty"`
Default interface{} `json:"default,omitempty"`
Ref string `json:"$ref,omitempty"`
In string `json:"in,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
CollectionFormat string `json:"collectionFormat,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Pattern string `json:"pattern,omitempty"`
AllowEmptyValue bool `json:"allowEmptyValue,omitempty"`
Required bool `json:"required,omitempty"`
UniqueItems bool `json:"uniqueItems,omitempty"`
ExclusiveMin bool `json:"exclusiveMinimum,omitempty"`
ExclusiveMax bool `json:"exclusiveMaximum,omitempty"`
Schema *openapi3.SchemaRef `json:"schema,omitempty"`
Items *openapi3.SchemaRef `json:"items,omitempty"`
Enum []interface{} `json:"enum,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
MaxLength *uint64 `json:"maxLength,omitempty"`
MaxItems *uint64 `json:"maxItems,omitempty"`
MinLength uint64 `json:"minLength,omitempty"`
MinItems uint64 `json:"minItems,omitempty"`
Default interface{} `json:"default,omitempty"`
}

func (parameter *Parameter) MarshalJSON() ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion openapi2/testdata/swagger.json

Large diffs are not rendered by default.

75 changes: 72 additions & 3 deletions openapi2conv/issue187_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (

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

func v2v3(spec2 []byte) (doc3 *openapi3.Swagger, err error) {
func v2v3JSON(spec2 []byte) (doc3 *openapi3.Swagger, err error) {
var doc2 openapi2.Swagger
if err = json.Unmarshal(spec2, &doc2); err != nil {
return
Expand All @@ -19,6 +20,15 @@ func v2v3(spec2 []byte) (doc3 *openapi3.Swagger, err error) {
return
}

func v2v3YAML(spec2 []byte) (doc3 *openapi3.Swagger, err error) {
var doc2 openapi2.Swagger
if err = yaml.Unmarshal(spec2, &doc2); err != nil {
return
}
doc3, err = ToV3Swagger(&doc2)
return
}

func TestIssue187(t *testing.T) {
spec := `
{
Expand Down Expand Up @@ -87,12 +97,71 @@ func TestIssue187(t *testing.T) {
}
}
`
doc3, err := v2v3([]byte(spec))
doc3, err := v2v3JSON([]byte(spec))
require.NoError(t, err)

spec3, err := json.Marshal(doc3)
require.NoError(t, err)
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"]}}}}`
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"]}}}}`
require.Equal(t, string(spec3), expected)

err = doc3.Validate(context.Background())
require.NoError(t, err)
}

func TestIssue237(t *testing.T) {
spec := `
swagger: '2.0'
info:
version: 1.0.0
title: title
paths:
/test:
get:
parameters:
- in: body
schema:
$ref: '#/definitions/TestRef'
responses:
'200':
description: description
definitions:
TestRef:
type: object
allOf:
- $ref: '#/definitions/TestRef2'
TestRef2:
type: object
`
doc3, err := v2v3YAML([]byte(spec))
require.NoError(t, err)

spec3, err := yaml.Marshal(doc3)
require.NoError(t, err)
const expected = `components:
schemas:
TestRef:
allOf:
- $ref: '#/components/schemas/TestRef2'
type: object
TestRef2:
type: object
info:
title: title
version: 1.0.0
openapi: 3.0.3
paths:
/test:
get:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/TestRef'
responses:
"200":
description: description
`
require.Equal(t, string(spec3), expected)

err = doc3.Validate(context.Background())
Expand Down
Loading