From 7bb731202c94eeae93a7f4c1a07a6e42b2278de7 Mon Sep 17 00:00:00 2001 From: dwertent Date: Fri, 16 Aug 2024 10:42:00 -0400 Subject: [PATCH 1/2] chore: Refactor SwaggerGen to support multiple values with the same path Signed-off-by: dwertent --- pkg/ffapi/openapi3.go | 7 +++++-- pkg/ffapi/openapi3_test.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/pkg/ffapi/openapi3.go b/pkg/ffapi/openapi3.go index 262d179..cdc03db 100644 --- a/pkg/ffapi/openapi3.go +++ b/pkg/ffapi/openapi3.go @@ -116,7 +116,7 @@ func (sg *SwaggerGen) getPathItem(doc *openapi3.T, path string) *openapi3.PathIt if doc.Paths == nil { doc.Paths = &openapi3.Paths{} } - pi := doc.Paths.Find(path) + pi := doc.Paths.Value(path) if pi != nil { return pi } @@ -389,12 +389,15 @@ func (sg *SwaggerGen) addFilters(ctx context.Context, route *Route, op *openapi3 func (sg *SwaggerGen) addRoute(ctx context.Context, doc *openapi3.T, route *Route) { var routeDescription string pi := sg.getPathItem(doc, route.Path) + if route.Path == "" { + fmt.Println("route.Path is empty") + } if route.PreTranslatedDescription != "" { routeDescription = route.PreTranslatedDescription } else { routeDescription = i18n.Expand(ctx, route.Description) if routeDescription == "" && sg.options.PanicOnMissingDescription { - log.Panicf(i18n.NewError(ctx, i18n.MsgRouteDescriptionMissing, route.Name).Error()) + log.Panicf("%s", i18n.NewError(ctx, i18n.MsgRouteDescriptionMissing, route.Name).Error()) } } op := &openapi3.Operation{ diff --git a/pkg/ffapi/openapi3_test.go b/pkg/ffapi/openapi3_test.go index f44911a..bd58883 100644 --- a/pkg/ffapi/openapi3_test.go +++ b/pkg/ffapi/openapi3_test.go @@ -19,10 +19,11 @@ package ffapi import ( "context" "fmt" - "github.com/stretchr/testify/require" "net/http" "testing" + "github.com/stretchr/testify/require" + "github.com/getkin/kin-openapi/openapi3" "github.com/ghodss/yaml" "github.com/hyperledger/firefly-common/pkg/config" @@ -268,6 +269,34 @@ func TestWildcards(t *testing.T) { assert.NotNil(t, swagger.Paths.Value("/namespaces/{ns}/example1/{id}")) } +func TestSamePathWithDifferentValues(t *testing.T) { + routes := []*Route{ + { + Name: "op1", + Path: "namespaces/{ns}/example1/{id}", + Method: http.MethodPost, + JSONInputValue: func() interface{} { return &TestStruct1{} }, + JSONOutputCodes: []int{http.StatusOK}, + }, + { + Name: "op2", + Path: "namespaces/{ns}/example1/{did}", + Method: http.MethodPost, + JSONInputValue: func() interface{} { return &TestStruct1{} }, + JSONOutputCodes: []int{http.StatusOK}, + }, + } + swagger := NewSwaggerGen(&SwaggerGenOptions{ + Title: "UnitTest", + Version: "1.0", + BaseURL: "http://localhost:12345/api/v1", + }).Generate(context.Background(), routes) + assert.Equal(t, 2, swagger.Paths.Len()) + assert.NotNil(t, swagger.Paths.Find("/namespaces/{ns}/example1/{id}")) + assert.NotNil(t, swagger.Paths.Value("/namespaces/{ns}/example1/{id}")) + assert.NotNil(t, swagger.Paths.Find("/namespaces/{ns}/example1/{did}")) + assert.NotNil(t, swagger.Paths.Value("/namespaces/{ns}/example1/{did}")) +} func TestFFExcludeTag(t *testing.T) { routes := []*Route{ { From f4ad9bb2bb08afaff4aed4bab4d5a4ecdc4b56af Mon Sep 17 00:00:00 2001 From: dwertent Date: Fri, 16 Aug 2024 11:00:14 -0400 Subject: [PATCH 2/2] Remove debugging print Signed-off-by: dwertent --- pkg/ffapi/openapi3.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/ffapi/openapi3.go b/pkg/ffapi/openapi3.go index cdc03db..cb975ea 100644 --- a/pkg/ffapi/openapi3.go +++ b/pkg/ffapi/openapi3.go @@ -389,9 +389,6 @@ func (sg *SwaggerGen) addFilters(ctx context.Context, route *Route, op *openapi3 func (sg *SwaggerGen) addRoute(ctx context.Context, doc *openapi3.T, route *Route) { var routeDescription string pi := sg.getPathItem(doc, route.Path) - if route.Path == "" { - fmt.Println("route.Path is empty") - } if route.PreTranslatedDescription != "" { routeDescription = route.PreTranslatedDescription } else {