From d30eda34e75ff1f4d0f4eeaff6431ce481044589 Mon Sep 17 00:00:00 2001 From: Marko Juraga Date: Fri, 13 Dec 2024 15:31:34 +0100 Subject: [PATCH] BUG/MINOR: specification: fix open api v3 specification generation --- configure_data_plane.go | 7 ++++++- configure_data_plane_test.go | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/configure_data_plane.go b/configure_data_plane.go index ad12de0a..6693db88 100644 --- a/configure_data_plane.go +++ b/configure_data_plane.go @@ -20,6 +20,7 @@ package dataplaneapi import ( "context" "crypto/tls" + "encoding/json" "io" "net/http" "os" @@ -751,7 +752,11 @@ func configureAPI(api *operations.DataPlaneAPI) http.Handler { //nolint:cyclop,m // setup OpenAPI v3 specification handler api.Version3GetOpenapiv3SpecificationHandler = version3.GetOpenapiv3SpecificationHandlerFunc(func(params version3.GetOpenapiv3SpecificationParams, principal interface{}) middleware.Responder { v2 := openapi2.T{} - err = v2.UnmarshalJSON(SwaggerJSON) + v2JSONString := string(SwaggerJSON) + v2JSONString = strings.ReplaceAll(v2JSONString, "#/definitions", "#/components/schemas") + curatedV2 := json.RawMessage([]byte(v2JSONString)) + + err = v2.UnmarshalJSON(curatedV2) if err != nil { e := misc.HandleError(err) return version3.NewGetOpenapiv3SpecificationDefault(int(*e.Code)).WithPayload(e) diff --git a/configure_data_plane_test.go b/configure_data_plane_test.go index eb99d7db..862f7e6a 100644 --- a/configure_data_plane_test.go +++ b/configure_data_plane_test.go @@ -18,6 +18,8 @@ package dataplaneapi import ( + "encoding/json" + "strings" "testing" "github.com/getkin/kin-openapi/openapi2" @@ -25,12 +27,17 @@ import ( ) func TestConvOpenAPIV2ToV3(t *testing.T) { + v2JSONString := string(SwaggerJSON) + v2JSONString = strings.ReplaceAll(v2JSONString, "#/definitions", "#/components/schemas") + curatedV2 := json.RawMessage([]byte(v2JSONString)) + var v2 openapi2.T - err := v2.UnmarshalJSON(SwaggerJSON) + err := v2.UnmarshalJSON(curatedV2) if err != nil { t.Error(err) return } + _, err = openapi2conv.ToV3(&v2) if err != nil { t.Error(err)