Skip to content

Commit

Permalink
fix required params parsing for routes with multiple paths and multip…
Browse files Browse the repository at this point in the history
…le params (#1621)

* fix required params parsing for routes with multiple paths and multiple params

* fix incorrect variable declaration of validParams
  • Loading branch information
Phenix66 authored Jul 7, 2023
1 parent c8372f6 commit 0cee1c5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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
var validParams []spec.Parameter
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

0 comments on commit 0cee1c5

Please sign in to comment.