From b284f6dd3fc7d9f5b922e38b80fefb08a58e2de5 Mon Sep 17 00:00:00 2001 From: JayanthTatina Date: Wed, 13 Nov 2024 16:53:54 +0530 Subject: [PATCH] convert schemaRef for additional props --- openapi2conv/issue1016_test.go | 81 ++++++++++++++++++++++++++++++++++ openapi2conv/openapi2_conv.go | 4 ++ 2 files changed, 85 insertions(+) create mode 100644 openapi2conv/issue1016_test.go diff --git a/openapi2conv/issue1016_test.go b/openapi2conv/issue1016_test.go new file mode 100644 index 00000000..acd87d07 --- /dev/null +++ b/openapi2conv/issue1016_test.go @@ -0,0 +1,81 @@ +package openapi2conv + +import ( + "context" + "encoding/json" + "testing" + + "github.com/getkin/kin-openapi/openapi2" + "github.com/stretchr/testify/require" +) + +func TestIssue1016(t *testing.T) { + v2 := []byte(` +{ + "basePath": "/v2", + "host": "test.example.com", + "info": { + "title": "MyAPI", + "version": "0.1", + "x-info": "info extension" + }, + "paths": { + "/foo": { + "get": { + "operationId": "getFoo", + "responses": { + "200": { + "description": "returns all information", + "schema": { + "$ref": "#/definitions/PetDirectory" + } + }, + "default": { + "description": "OK" + } + }, + "summary": "get foo" + } + } + }, + "schemes": [ + "http" + ], + "swagger": "2.0", + "definitions": { + "Pet": { + "type": "object", + "required": ["petType"], + "properties": { + "petType": { + "type": "string" + }, + "name": { + "type": "string" + }, + "age": { + "type": "integer" + } + } + }, + "PetDirectory":{ + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/Pet" + } + } + } +} +`) + + var doc2 openapi2.T + err := json.Unmarshal(v2, &doc2) + require.NoError(t, err) + + doc3, err := v2v3YAML(v2) + require.NoError(t, err) + + err = doc3.Validate(context.Background()) + require.NoError(t, err) + require.Equal(t, "#/components/schemas/Pet", doc3.Components.Schemas["PetDirectory"].Value.AdditionalProperties.Schema.Ref) +} diff --git a/openapi2conv/openapi2_conv.go b/openapi2conv/openapi2_conv.go index ef0a5edd..ec7646d2 100644 --- a/openapi2conv/openapi2_conv.go +++ b/openapi2conv/openapi2_conv.go @@ -516,6 +516,10 @@ func ToV3SchemaRef(schema *openapi2.SchemaRef) *openapi3.SchemaRef { AdditionalProperties: schema.Value.AdditionalProperties, } + if schema.Value.AdditionalProperties.Schema != nil { + v3Schema.AdditionalProperties.Schema.Ref = ToV3Ref(schema.Value.AdditionalProperties.Schema.Ref) + } + if schema.Value.Discriminator != "" { v3Schema.Discriminator = &openapi3.Discriminator{ PropertyName: schema.Value.Discriminator,