Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Refactor SwaggerGen to support multiple values with the same path #145

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/ffapi/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just being careful with this, as it would affect all the swagger documents generated from now on

Is there a scenario where normalising does make sense? I tried to find the history in the kin-openapi repo and it seems that it was there from the original commit

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, we can wait for a response here.

if pi != nil {
return pi
}
Expand Down Expand Up @@ -394,7 +394,7 @@ func (sg *SwaggerGen) addRoute(ctx context.Context, doc *openapi3.T, route *Rout
} 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{
Expand Down
31 changes: 30 additions & 1 deletion pkg/ffapi/openapi3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
{
Expand Down