Skip to content

Commit fd08c7f

Browse files
fenollpvbehar
andauthored
v2Tov3: handle parameter schema refs (#455)
Co-authored-by: Vincent Behar <[email protected]>
1 parent f1075be commit fd08c7f

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

openapi2conv/openapi2_conv.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ func ToV3Parameter(components *openapi3.Components, parameter *openapi2.Paramete
298298
required = true
299299
}
300300

301+
var schemaRefRef string
302+
if schemaRef := parameter.Schema; schemaRef != nil && schemaRef.Ref != "" {
303+
schemaRefRef = schemaRef.Ref
304+
}
301305
result := &openapi3.Parameter{
302306
In: parameter.In,
303307
Name: parameter.Name,
@@ -322,7 +326,9 @@ func ToV3Parameter(components *openapi3.Components, parameter *openapi2.Paramete
322326
AllowEmptyValue: parameter.AllowEmptyValue,
323327
UniqueItems: parameter.UniqueItems,
324328
MultipleOf: parameter.MultipleOf,
325-
}}),
329+
},
330+
Ref: schemaRefRef,
331+
}),
326332
}
327333
return &openapi3.ParameterRef{Value: result}, nil, nil, nil
328334
}
@@ -980,6 +986,10 @@ func FromV3Parameter(ref *openapi3.ParameterRef, components *openapi3.Components
980986
}
981987
if schemaRef := parameter.Schema; schemaRef != nil {
982988
schemaRef, _ = FromV3SchemaRef(schemaRef, components)
989+
if ref := schemaRef.Ref; ref != "" {
990+
result.Schema = &openapi3.SchemaRef{Ref: FromV3Ref(ref)}
991+
return result, nil
992+
}
983993
schema := schemaRef.Value
984994
result.Type = schema.Type
985995
result.Format = schema.Format

openapi2conv/openapi2_conv_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ const exampleV2 = `
7979
"ItemExtension": {
8080
"description": "It could be anything.",
8181
"type": "boolean"
82+
},
83+
"foo": {
84+
"description": "foo description",
85+
"enum": [
86+
"bar",
87+
"baz"
88+
],
89+
"type": "string"
8290
}
8391
},
8492
"externalDocs": {
@@ -305,6 +313,34 @@ const exampleV2 = `
305313
},
306314
"x-path": "path extension 1",
307315
"x-path2": "path extension 2"
316+
},
317+
"/foo": {
318+
"get": {
319+
"operationId": "getFoo",
320+
"consumes": [
321+
"application/json",
322+
"application/xml"
323+
],
324+
"parameters": [
325+
{
326+
"x-originalParamName": "foo",
327+
"in": "body",
328+
"name": "foo",
329+
"schema": {
330+
"$ref": "#/definitions/foo"
331+
}
332+
}
333+
],
334+
"responses": {
335+
"default": {
336+
"description": "OK",
337+
"schema": {
338+
"$ref": "#/definitions/foo"
339+
}
340+
}
341+
},
342+
"summary": "get foo"
343+
}
308344
}
309345
},
310346
"responses": {
@@ -420,6 +456,14 @@ const exampleV3 = `
420456
"type": "string",
421457
"x-formData-name": "fileUpload2",
422458
"x-mimetype": "text/plain"
459+
},
460+
"foo": {
461+
"description": "foo description",
462+
"enum": [
463+
"bar",
464+
"baz"
465+
],
466+
"type": "string"
423467
}
424468
}
425469
},
@@ -646,6 +690,39 @@ const exampleV3 = `
646690
},
647691
"x-path": "path extension 1",
648692
"x-path2": "path extension 2"
693+
},
694+
"/foo": {
695+
"get": {
696+
"operationId": "getFoo",
697+
"requestBody": {
698+
"x-originalParamName": "foo",
699+
"content": {
700+
"application/json": {
701+
"schema": {
702+
"$ref": "#/components/schemas/foo"
703+
}
704+
},
705+
"application/xml": {
706+
"schema": {
707+
"$ref": "#/components/schemas/foo"
708+
}
709+
}
710+
}
711+
},
712+
"responses": {
713+
"default": {
714+
"content": {
715+
"application/json": {
716+
"schema": {
717+
"$ref": "#/components/schemas/foo"
718+
}
719+
}
720+
},
721+
"description": "OK"
722+
}
723+
},
724+
"summary": "get foo"
725+
}
649726
}
650727
},
651728
"security": [

0 commit comments

Comments
 (0)