1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
package openapi2conv
import (
"context"
"encoding/json"
"testing"
"github.com/invopop/yaml"
"github.com/stretchr/testify/require"
"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi3"
)
func v2v3JSON(spec2 []byte) (doc3 *openapi3.T, err error) {
var doc2 openapi2.T
if err = json.Unmarshal(spec2, &doc2); err != nil {
return
}
doc3, err = ToV3(&doc2)
return
}
func v2v3YAML(spec2 []byte) (doc3 *openapi3.T, err error) {
var doc2 openapi2.T
if err = yaml.Unmarshal(spec2, &doc2); err != nil {
return
}
doc3, err = ToV3(&doc2)
return
}
func TestIssue187(t *testing.T) {
spec := `
{
"swagger": "2.0",
"info": {
"description": "Test Golang Application",
"version": "1.0",
"title": "Test",
"contact": {
"name": "Test",
"email": "test@test.com"
}
},
"paths": {
"/me": {
"get": {
"description": "",
"operationId": "someTest",
"summary": "Some test",
"tags": ["probe"],
"produces": ["application/json"],
"responses": {
"200": {
"description": "successful operation",
"schema": {"$ref": "#/definitions/model.ProductSearchAttributeRequest"}
}
}
}
}
},
"host": "",
"basePath": "/test",
"definitions": {
"model.ProductSearchAttributeRequest": {
"type": "object",
"properties": {
"filterField": {
"type": "string"
},
"filterKey": {
"type": "string"
},
"type": {
"type": "string"
},
"values": {
"$ref": "#/definitions/model.ProductSearchAttributeValueRequest"
}
},
"title": "model.ProductSearchAttributeRequest"
},
"model.ProductSearchAttributeValueRequest": {
"type": "object",
"properties": {
"imageUrl": {
"type": "string"
},
"text": {
"type": "string"
}
},
"title": "model.ProductSearchAttributeValueRequest"
}
}
}
`
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.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.JSONEq(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:
'*/*':
schema:
$ref: '#/components/schemas/TestRef'
responses:
"200":
description: description
`
require.YAMLEq(t, string(spec3), expected)
err = doc3.Validate(context.Background())
require.NoError(t, err)
}
func TestPR449(t *testing.T) {
spec := `
swagger: '2.0'
info:
version: 1.0.0
title: title
securityDefinitions:
OAuth2Application:
type: "oauth2"
flow: "application"
tokenUrl: "example.com/oauth2/token"
`
doc3, err := v2v3YAML([]byte(spec))
require.NoError(t, err)
require.NotNil(t, doc3.Components.SecuritySchemes["OAuth2Application"].Value.Flows.ClientCredentials)
_, err = yaml.Marshal(doc3)
require.NoError(t, err)
doc2, err := FromV3(doc3)
require.NoError(t, err)
require.Equal(t, doc2.SecurityDefinitions["OAuth2Application"].Flow, "application")
}
|