Skip to content

fix: change SwaggerGen to support multiple values with the same path #145

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

Merged
merged 3 commits into from
Jul 3, 2025
Merged
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
2 changes: 1 addition & 1 deletion pkg/ffapi/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,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
Contributor 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Circling back to this issue, I tested it (back in the day) and it worked.

if pi != nil {
return pi
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/ffapi/openapi3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,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