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

fix required params parsing for routes with multiple paths and multiple params #1621

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,21 @@ func processRouterOperation(parser *Parser, operation *Operation) error {
parser.debug.Printf("warning: %s\n", err)
}

*op = &operation.Operation
if len(operation.RouterProperties) > 1 {
newOp := *operation
validParams := []spec.Parameter{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use var validParams []spec.Parameter in order to avoid mem allocations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

for _, param := range newOp.Operation.OperationProps.Parameters {
if param.In == "path" && !strings.Contains(routeProperties.Path, param.Name) {
// This path param is not actually contained in the path, skip adding it to the final params
continue
}
validParams = append(validParams, param)
}
newOp.Operation.OperationProps.Parameters = validParams
*op = &newOp.Operation
} else {
*op = &operation.Operation
}

parser.swagger.Paths.Paths[routeProperties.Path] = pathItem
}
Expand Down
34 changes: 34 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2905,6 +2905,40 @@ func Test3(){
assert.NotNil(t, val.Delete)
}

func TestParser_ParseRouterApiMultiplePathsWithMultipleParams(t *testing.T) {
t.Parallel()

src := `
package test

// @Success 200
// @Param group_id path int true "Group ID"
// @Param user_id path int true "User ID"
// @Router /examples/groups/{group_id}/user/{user_id}/address [get]
// @Router /examples/user/{user_id}/address [get]
func Test(){
}
`
p := New()
err := p.packages.ParseFile("api", "api/api.go", src, ParseAll)
assert.NoError(t, err)

err = p.packages.RangeFiles(p.ParseRouterAPIInfo)
assert.NoError(t, err)

ps := p.swagger.Paths.Paths

val, ok := ps["/examples/groups/{group_id}/user/{user_id}/address"]

assert.True(t, ok)
assert.Equal(t, 2, len(val.Get.Parameters))

val, ok = ps["/examples/user/{user_id}/address"]

assert.True(t, ok)
assert.Equal(t, 1, len(val.Get.Parameters))
}

// func TestParseDeterministic(t *testing.T) {
// mainAPIFile := "main.go"
// for _, searchDir := range []string{
Expand Down