Skip to content

Commit 86df652

Browse files
authored
fix: avoid shadowed variable on operationv3 (#1680)
1 parent 0709499 commit 86df652

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

operationv3.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,27 +388,29 @@ func (o *OperationV3) ParseParamComment(commentLine string, astFile *ast.File) e
388388
continue
389389
}
390390

391+
itemParam := param // Avoid shadowed variable which could cause side effects to o.Operation.Parameters
392+
391393
switch {
392394
case prop.Type[0] == ARRAY &&
393395
prop.Items.Schema != nil &&
394396
len(prop.Items.Schema.Spec.Type) > 0 &&
395397
IsSimplePrimitiveType(prop.Items.Schema.Spec.Type[0]):
396398

397-
param = createParameterV3(paramType, prop.Description, name, prop.Type[0], prop.Items.Schema.Spec.Type[0], findInSlice(schema.Spec.Required, name), enums, o.parser.collectionFormatInQuery)
399+
itemParam = createParameterV3(paramType, prop.Description, name, prop.Type[0], prop.Items.Schema.Spec.Type[0], findInSlice(schema.Spec.Required, name), enums, o.parser.collectionFormatInQuery)
398400

399401
case IsSimplePrimitiveType(prop.Type[0]):
400-
param = createParameterV3(paramType, prop.Description, name, PRIMITIVE, prop.Type[0], findInSlice(schema.Spec.Required, name), enums, o.parser.collectionFormatInQuery)
402+
itemParam = createParameterV3(paramType, prop.Description, name, PRIMITIVE, prop.Type[0], findInSlice(schema.Spec.Required, name), enums, o.parser.collectionFormatInQuery)
401403
default:
402404
o.parser.debug.Printf("skip field [%s] in %s is not supported type for %s", name, refType, paramType)
403405

404406
continue
405407
}
406408

407-
param.Schema.Spec = prop
409+
itemParam.Schema.Spec = prop
408410

409411
listItem := &spec.RefOrSpec[spec.Extendable[spec.Parameter]]{
410412
Spec: &spec.Extendable[spec.Parameter]{
411-
Spec: &param,
413+
Spec: &itemParam,
412414
},
413415
}
414416

@@ -710,7 +712,7 @@ func (o *OperationV3) parseAPIObjectSchema(commentLine, schemaType, refType stri
710712

711713
result := spec.NewSchemaSpec()
712714
result.Spec.Type = spec.NewSingleOrArray("array")
713-
result.Spec.Items = spec.NewBoolOrSchema(false, schema) //TODO: allowed?
715+
result.Spec.Items = spec.NewBoolOrSchema(false, schema) // TODO: allowed?
714716
return result, nil
715717

716718
default:

operationv3_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package swag
22

33
import (
4+
"go/ast"
45
goparser "go/parser"
56
"go/token"
67
"testing"
@@ -852,6 +853,95 @@ func TestOperation_ParseParamCommentV3(t *testing.T) {
852853
}
853854
})
854855

856+
t.Run("struct queries", func(t *testing.T) {
857+
t.Parallel()
858+
parser := New()
859+
parser.packages.uniqueDefinitions["main.Object"] = &TypeSpecDef{
860+
File: &ast.File{Name: &ast.Ident{Name: "test"}},
861+
TypeSpec: &ast.TypeSpec{
862+
Name: &ast.Ident{Name: "Field"},
863+
TypeParams: &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{{Name: "T"}}}}},
864+
Type: &ast.StructType{
865+
Struct: 100,
866+
Fields: &ast.FieldList{
867+
List: []*ast.Field{
868+
{
869+
Names: []*ast.Ident{
870+
{Name: "T"},
871+
},
872+
Type: ast.NewIdent("string"),
873+
},
874+
{
875+
Names: []*ast.Ident{
876+
{Name: "T2"},
877+
},
878+
Type: ast.NewIdent("string"),
879+
},
880+
},
881+
},
882+
},
883+
},
884+
}
885+
o := NewOperationV3(parser)
886+
err := o.ParseComment(`@Param some_object query main.Object true "Some Object"`,
887+
nil)
888+
889+
assert.NoError(t, err)
890+
891+
expectedT := &spec.RefOrSpec[spec.Extendable[spec.Parameter]]{
892+
Spec: &spec.Extendable[spec.Parameter]{
893+
Spec: &spec.Parameter{
894+
Name: "t",
895+
In: "query",
896+
Schema: &spec.RefOrSpec[spec.Schema]{
897+
Spec: &spec.Schema{
898+
JsonSchema: spec.JsonSchema{
899+
JsonSchemaCore: spec.JsonSchemaCore{
900+
Type: typeString,
901+
},
902+
},
903+
},
904+
},
905+
},
906+
},
907+
}
908+
expectedT2 := &spec.RefOrSpec[spec.Extendable[spec.Parameter]]{
909+
Spec: &spec.Extendable[spec.Parameter]{
910+
Spec: &spec.Parameter{
911+
Name: "t2",
912+
In: "query",
913+
Schema: &spec.RefOrSpec[spec.Schema]{
914+
Spec: &spec.Schema{
915+
JsonSchema: spec.JsonSchema{
916+
JsonSchemaCore: spec.JsonSchemaCore{
917+
Type: typeString,
918+
},
919+
},
920+
},
921+
},
922+
},
923+
},
924+
}
925+
926+
assert.Len(t, o.Parameters, 2)
927+
tFound := false
928+
t2Found := false
929+
for _, param := range o.Parameters {
930+
switch param.Spec.Spec.Name {
931+
case "t":
932+
assert.EqualValues(t, expectedT, param)
933+
tFound = true
934+
case "t2":
935+
assert.EqualValues(t, expectedT2, param)
936+
t2Found = true
937+
default:
938+
assert.Fail(t, "unexpected result")
939+
}
940+
}
941+
942+
assert.True(t, tFound, "results should contain t")
943+
assert.True(t, t2Found, "results should contain t2")
944+
})
855945
}
856946

857947
// Test ParseParamComment Query Params

0 commit comments

Comments
 (0)