Skip to content

Commit 8561606

Browse files
authored
allow object support for "header" param to for backward compatibility (#2178)
Co-authored-by: SergeiSadov <>
1 parent db6574e commit 8561606

2 files changed

Lines changed: 92 additions & 2 deletions

File tree

operationv3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func (o *OperationV3) ParseParamComment(commentLine string, astFile *ast.File) e
367367
param := createParameterV3(paramType, description, name, objectType, refType, required, enums, o.parser.collectionFormatInQuery)
368368

369369
switch paramType {
370-
case "path", "header":
370+
case "path":
371371
switch objectType {
372372
case ARRAY:
373373
if !IsPrimitiveType(refType) {
@@ -376,7 +376,7 @@ func (o *OperationV3) ParseParamComment(commentLine string, astFile *ast.File) e
376376
case OBJECT:
377377
return fmt.Errorf("%s is not supported type for %s", refType, paramType)
378378
}
379-
case "query":
379+
case "query", "header":
380380
switch objectType {
381381
case ARRAY:
382382
if !IsPrimitiveType(refType) && !(refType == "file" && paramType == "formData") {

operationv3_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,96 @@ func TestOperation_ParseParamCommentV3(t *testing.T) {
942942
assert.True(t, tFound, "results should contain t")
943943
assert.True(t, t2Found, "results should contain t2")
944944
})
945+
946+
t.Run("struct headers", func(t *testing.T) {
947+
t.Parallel()
948+
parser := New()
949+
parser.packages.uniqueDefinitions["main.Object"] = &TypeSpecDef{
950+
File: &ast.File{Name: &ast.Ident{Name: "test"}},
951+
TypeSpec: &ast.TypeSpec{
952+
Name: &ast.Ident{Name: "Field"},
953+
TypeParams: &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{{Name: "T"}}}}},
954+
Type: &ast.StructType{
955+
Struct: 100,
956+
Fields: &ast.FieldList{
957+
List: []*ast.Field{
958+
{
959+
Names: []*ast.Ident{
960+
{Name: "T"},
961+
},
962+
Type: ast.NewIdent("string"),
963+
},
964+
{
965+
Names: []*ast.Ident{
966+
{Name: "T2"},
967+
},
968+
Type: ast.NewIdent("string"),
969+
},
970+
},
971+
},
972+
},
973+
},
974+
}
975+
o := NewOperationV3(parser)
976+
err := o.ParseComment(`@Param some_object header main.Object true "Some Object"`,
977+
nil)
978+
979+
assert.NoError(t, err)
980+
981+
expectedT := &spec.RefOrSpec[spec.Extendable[spec.Parameter]]{
982+
Spec: &spec.Extendable[spec.Parameter]{
983+
Spec: &spec.Parameter{
984+
Name: "t",
985+
In: "header",
986+
Schema: &spec.RefOrSpec[spec.Schema]{
987+
Spec: &spec.Schema{
988+
JsonSchema: spec.JsonSchema{
989+
JsonSchemaCore: spec.JsonSchemaCore{
990+
Type: &typeString,
991+
},
992+
},
993+
},
994+
},
995+
},
996+
},
997+
}
998+
expectedT2 := &spec.RefOrSpec[spec.Extendable[spec.Parameter]]{
999+
Spec: &spec.Extendable[spec.Parameter]{
1000+
Spec: &spec.Parameter{
1001+
Name: "t2",
1002+
In: "header",
1003+
Schema: &spec.RefOrSpec[spec.Schema]{
1004+
Spec: &spec.Schema{
1005+
JsonSchema: spec.JsonSchema{
1006+
JsonSchemaCore: spec.JsonSchemaCore{
1007+
Type: &typeString,
1008+
},
1009+
},
1010+
},
1011+
},
1012+
},
1013+
},
1014+
}
1015+
1016+
assert.Len(t, o.Parameters, 2)
1017+
tFound := false
1018+
t2Found := false
1019+
for _, param := range o.Parameters {
1020+
switch param.Spec.Spec.Name {
1021+
case "t":
1022+
assert.EqualValues(t, expectedT, param)
1023+
tFound = true
1024+
case "t2":
1025+
assert.EqualValues(t, expectedT2, param)
1026+
t2Found = true
1027+
default:
1028+
assert.Fail(t, "unexpected result")
1029+
}
1030+
}
1031+
1032+
assert.True(t, tFound, "results should contain t")
1033+
assert.True(t, t2Found, "results should contain t2")
1034+
})
9451035
}
9461036

9471037
// Test ParseParamComment Query Params

0 commit comments

Comments
 (0)