From fd08c7f0b51291689a0530e0040dbfa4730cc590 Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Thu, 2 Dec 2021 16:32:27 +0000 Subject: [PATCH] v2Tov3: handle parameter schema refs (#455) Co-authored-by: Vincent Behar --- openapi2conv/openapi2_conv.go | 12 ++++- openapi2conv/openapi2_conv_test.go | 77 ++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/openapi2conv/openapi2_conv.go b/openapi2conv/openapi2_conv.go index 245702622..04cb18d2e 100644 --- a/openapi2conv/openapi2_conv.go +++ b/openapi2conv/openapi2_conv.go @@ -298,6 +298,10 @@ func ToV3Parameter(components *openapi3.Components, parameter *openapi2.Paramete required = true } + var schemaRefRef string + if schemaRef := parameter.Schema; schemaRef != nil && schemaRef.Ref != "" { + schemaRefRef = schemaRef.Ref + } result := &openapi3.Parameter{ In: parameter.In, Name: parameter.Name, @@ -322,7 +326,9 @@ func ToV3Parameter(components *openapi3.Components, parameter *openapi2.Paramete AllowEmptyValue: parameter.AllowEmptyValue, UniqueItems: parameter.UniqueItems, MultipleOf: parameter.MultipleOf, - }}), + }, + Ref: schemaRefRef, + }), } return &openapi3.ParameterRef{Value: result}, nil, nil, nil } @@ -980,6 +986,10 @@ func FromV3Parameter(ref *openapi3.ParameterRef, components *openapi3.Components } if schemaRef := parameter.Schema; schemaRef != nil { schemaRef, _ = FromV3SchemaRef(schemaRef, components) + if ref := schemaRef.Ref; ref != "" { + result.Schema = &openapi3.SchemaRef{Ref: FromV3Ref(ref)} + return result, nil + } schema := schemaRef.Value result.Type = schema.Type result.Format = schema.Format diff --git a/openapi2conv/openapi2_conv_test.go b/openapi2conv/openapi2_conv_test.go index a8322698f..f8f287836 100644 --- a/openapi2conv/openapi2_conv_test.go +++ b/openapi2conv/openapi2_conv_test.go @@ -79,6 +79,14 @@ const exampleV2 = ` "ItemExtension": { "description": "It could be anything.", "type": "boolean" + }, + "foo": { + "description": "foo description", + "enum": [ + "bar", + "baz" + ], + "type": "string" } }, "externalDocs": { @@ -305,6 +313,34 @@ const exampleV2 = ` }, "x-path": "path extension 1", "x-path2": "path extension 2" + }, + "/foo": { + "get": { + "operationId": "getFoo", + "consumes": [ + "application/json", + "application/xml" + ], + "parameters": [ + { + "x-originalParamName": "foo", + "in": "body", + "name": "foo", + "schema": { + "$ref": "#/definitions/foo" + } + } + ], + "responses": { + "default": { + "description": "OK", + "schema": { + "$ref": "#/definitions/foo" + } + } + }, + "summary": "get foo" + } } }, "responses": { @@ -420,6 +456,14 @@ const exampleV3 = ` "type": "string", "x-formData-name": "fileUpload2", "x-mimetype": "text/plain" + }, + "foo": { + "description": "foo description", + "enum": [ + "bar", + "baz" + ], + "type": "string" } } }, @@ -646,6 +690,39 @@ const exampleV3 = ` }, "x-path": "path extension 1", "x-path2": "path extension 2" + }, + "/foo": { + "get": { + "operationId": "getFoo", + "requestBody": { + "x-originalParamName": "foo", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/foo" + } + }, + "application/xml": { + "schema": { + "$ref": "#/components/schemas/foo" + } + } + } + }, + "responses": { + "default": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/foo" + } + } + }, + "description": "OK" + } + }, + "summary": "get foo" + } } }, "security": [