Skip to content

Commit 76fdbcc

Browse files
committed
openapi2conv: fix allOf inside additionalProperties
Signed-off-by: Dani Maarouf <[email protected]>
1 parent f53e403 commit 76fdbcc

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

openapi2conv/openapi2_conv.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,14 @@ func convertRefsInV3SchemaRef(from *openapi3.SchemaRef) *openapi3.SchemaRef {
568568
to.Value.Items.Ref = ToV3Ref(to.Value.Items.Ref)
569569
}
570570
to.Value.AdditionalProperties = toV3AdditionalProperties(to.Value.AdditionalProperties)
571+
572+
if len(to.Value.AllOf) > 0 {
573+
allOf := make(openapi3.SchemaRefs, len(to.Value.AllOf))
574+
for i, schemaRef := range to.Value.AllOf {
575+
allOf[i] = convertRefsInV3SchemaRef(schemaRef)
576+
}
577+
to.Value.AllOf = allOf
578+
}
571579
}
572580
return &to
573581
}

openapi2conv/openapi2_conv_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,87 @@ func TestConvOpenAPIV2ToV3WithNestedAdditionalPropertiesSchemaRef(t *testing.T)
193193
require.Equal(t, "#/components/schemas/Foo", responseSchema.AdditionalProperties.Schema.Value.AdditionalProperties.Schema.Ref)
194194
}
195195

196+
func TestConvOpenAPIV2ToV3WithAllOfInsideAdditionalProperties(t *testing.T) {
197+
v2 := []byte(`
198+
{
199+
"basePath": "/v2",
200+
"host": "test.example.com",
201+
"info": {
202+
"title": "MyAPI",
203+
"version": "0.1"
204+
},
205+
"paths": {
206+
"/v1/objStatus": {
207+
"get": {
208+
"produces": [
209+
"application/json"
210+
],
211+
"responses": {
212+
"200": {
213+
"schema": {
214+
"type": "object",
215+
"properties": {
216+
"result": {
217+
"type": "object",
218+
"additionalProperties": {
219+
"type": "object",
220+
"allOf": [
221+
{
222+
"$ref": "#/definitions/ObjectInfo"
223+
}
224+
],
225+
"additionalProperties": {
226+
"allOf": [
227+
{
228+
"$ref": "#/definitions/ObjectInfo"
229+
}
230+
]
231+
}
232+
}
233+
}
234+
}
235+
},
236+
"description": "Success"
237+
}
238+
}
239+
}
240+
}
241+
},
242+
"definitions": {
243+
"ObjectInfo": {
244+
"type": "object",
245+
"properties": {
246+
"object_id": {
247+
"type": "string",
248+
"format": "uuid"
249+
}
250+
}
251+
}
252+
},
253+
"schemes": [
254+
"http"
255+
],
256+
"swagger": "2.0"
257+
}
258+
`)
259+
260+
var doc2 openapi2.T
261+
err := json.Unmarshal(v2, &doc2)
262+
require.NoError(t, err)
263+
264+
doc3, err := ToV3(&doc2)
265+
require.NoError(t, err)
266+
err = doc3.Validate(context.Background())
267+
require.NoError(t, err)
268+
269+
responseSchema := doc3.Paths.Value("/v1/objStatus").Get.Responses.Value("200").Value.Content.Get("application/json").Schema.Value
270+
require.Equal(t, &openapi3.Types{"object"}, responseSchema.Type)
271+
resultSchema := responseSchema.Properties["result"].Value
272+
require.Equal(t, &openapi3.Types{"object"}, resultSchema.Type)
273+
require.Equal(t, "#/components/schemas/ObjectInfo", resultSchema.AdditionalProperties.Schema.Value.AllOf[0].Ref)
274+
require.Equal(t, "#/components/schemas/ObjectInfo", resultSchema.AdditionalProperties.Schema.Value.AdditionalProperties.Schema.Value.AllOf[0].Ref)
275+
}
276+
196277
const exampleV2 = `
197278
{
198279
"basePath": "/v2",

0 commit comments

Comments
 (0)