Skip to content

Commit 0cee1c5

Browse files
authoredJul 7, 2023
fix required params parsing for routes with multiple paths and multiple params (#1621)
* fix required params parsing for routes with multiple paths and multiple params * fix incorrect variable declaration of validParams
1 parent c8372f6 commit 0cee1c5

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed
 

‎parser.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,21 @@ func processRouterOperation(parser *Parser, operation *Operation) error {
975975
parser.debug.Printf("warning: %s\n", err)
976976
}
977977

978-
*op = &operation.Operation
978+
if len(operation.RouterProperties) > 1 {
979+
newOp := *operation
980+
var validParams []spec.Parameter
981+
for _, param := range newOp.Operation.OperationProps.Parameters {
982+
if param.In == "path" && !strings.Contains(routeProperties.Path, param.Name) {
983+
// This path param is not actually contained in the path, skip adding it to the final params
984+
continue
985+
}
986+
validParams = append(validParams, param)
987+
}
988+
newOp.Operation.OperationProps.Parameters = validParams
989+
*op = &newOp.Operation
990+
} else {
991+
*op = &operation.Operation
992+
}
979993

980994
parser.swagger.Paths.Paths[routeProperties.Path] = pathItem
981995
}

‎parser_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -2905,6 +2905,40 @@ func Test3(){
29052905
assert.NotNil(t, val.Delete)
29062906
}
29072907

2908+
func TestParser_ParseRouterApiMultiplePathsWithMultipleParams(t *testing.T) {
2909+
t.Parallel()
2910+
2911+
src := `
2912+
package test
2913+
2914+
// @Success 200
2915+
// @Param group_id path int true "Group ID"
2916+
// @Param user_id path int true "User ID"
2917+
// @Router /examples/groups/{group_id}/user/{user_id}/address [get]
2918+
// @Router /examples/user/{user_id}/address [get]
2919+
func Test(){
2920+
}
2921+
`
2922+
p := New()
2923+
err := p.packages.ParseFile("api", "api/api.go", src, ParseAll)
2924+
assert.NoError(t, err)
2925+
2926+
err = p.packages.RangeFiles(p.ParseRouterAPIInfo)
2927+
assert.NoError(t, err)
2928+
2929+
ps := p.swagger.Paths.Paths
2930+
2931+
val, ok := ps["/examples/groups/{group_id}/user/{user_id}/address"]
2932+
2933+
assert.True(t, ok)
2934+
assert.Equal(t, 2, len(val.Get.Parameters))
2935+
2936+
val, ok = ps["/examples/user/{user_id}/address"]
2937+
2938+
assert.True(t, ok)
2939+
assert.Equal(t, 1, len(val.Get.Parameters))
2940+
}
2941+
29082942
// func TestParseDeterministic(t *testing.T) {
29092943
// mainAPIFile := "main.go"
29102944
// for _, searchDir := range []string{

0 commit comments

Comments
 (0)
Please sign in to comment.