Skip to content

Commit acb0508

Browse files
NerzalTobias Theel
and
Tobias Theel
authored
V2: Fix tests after module upgrade (#1557)
* V2: Fix tests after v2 module upgrade * formData is now being placed into the request body instead of the parameters --------- Co-authored-by: Tobias Theel <tt@fino.digital>
1 parent 6d1a872 commit acb0508

File tree

11 files changed

+89
-68
lines changed

11 files changed

+89
-68
lines changed

enums_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestParseGlobalEnums(t *testing.T) {
1414
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
1515
require.NoError(t, err)
1616

17-
const constsPath = "github.com/swaggo/swag/testdata/enums/consts"
17+
const constsPath = "github.com/swaggo/swag/v2/testdata/enums/consts"
1818
table := p.packages.packages[constsPath].ConstTable
1919
require.NotNil(t, table, "const table must not be nil")
2020

gen/gen_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func TestGen_BuildDescriptionWithQuotes(t *testing.T) {
223223
require.NoError(t, err)
224224
}
225225
}
226-
cmd := exec.Command("go", "build", "-buildmode=plugin", "github.com/swaggo/swag/testdata/quotes")
226+
cmd := exec.Command("go", "build", "-buildmode=plugin", "github.com/swaggo/swag/v2/testdata/quotes")
227227

228228
cmd.Dir = config.SearchDir
229229

operationv3.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (o *OperationV3) ParseParamComment(commentLine string, astFile *ast.File) e
364364
case OBJECT:
365365
return fmt.Errorf("%s is not supported type for %s", refType, paramType)
366366
}
367-
case "query", "formData":
367+
case "query":
368368
switch objectType {
369369
case ARRAY:
370370
if !IsPrimitiveType(refType) && !(refType == "file" && paramType == "formData") {
@@ -417,7 +417,7 @@ func (o *OperationV3) ParseParamComment(commentLine string, astFile *ast.File) e
417417

418418
return nil
419419
}
420-
case "body":
420+
case "body", "formData":
421421
if objectType == PRIMITIVE {
422422
schema := PrimitiveSchemaV3(refType)
423423

@@ -426,7 +426,7 @@ func (o *OperationV3) ParseParamComment(commentLine string, astFile *ast.File) e
426426
return err
427427
}
428428

429-
o.fillRequestBody(schema, required, description, true)
429+
o.fillRequestBody(schema, required, description, true, paramType == "formData")
430430

431431
return nil
432432

@@ -442,7 +442,7 @@ func (o *OperationV3) ParseParamComment(commentLine string, astFile *ast.File) e
442442
return err
443443
}
444444

445-
o.fillRequestBody(schema, required, description, false)
445+
o.fillRequestBody(schema, required, description, false, paramType == "formData")
446446

447447
return nil
448448

@@ -464,13 +464,15 @@ func (o *OperationV3) ParseParamComment(commentLine string, astFile *ast.File) e
464464
return nil
465465
}
466466

467-
func (o *OperationV3) fillRequestBody(schema *spec.RefOrSpec[spec.Schema], required bool, description string, primitive bool) {
467+
func (o *OperationV3) fillRequestBody(schema *spec.RefOrSpec[spec.Schema], required bool, description string, primitive, formData bool) {
468468
if o.RequestBody == nil {
469469
o.RequestBody = spec.NewRequestBodySpec()
470470
o.RequestBody.Spec.Spec.Content = make(map[string]*spec.Extendable[spec.MediaType])
471471

472-
if primitive {
472+
if primitive && !formData {
473473
o.RequestBody.Spec.Spec.Content["text/plain"] = spec.NewMediaType()
474+
} else if formData {
475+
o.RequestBody.Spec.Spec.Content["application/x-www-form-urlencoded"] = spec.NewMediaType()
474476
} else {
475477
o.RequestBody.Spec.Spec.Content["application/json"] = spec.NewMediaType()
476478
}

operationv3_test.go

+19-23
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ func TestOperation_ParseParamCommentV3(t *testing.T) {
773773

774774
t.Run("integer", func(t *testing.T) {
775775
t.Parallel()
776-
for _, paramType := range []string{"header", "path", "query", "formData"} {
776+
for _, paramType := range []string{"header", "path", "query"} {
777777
t.Run(paramType, func(t *testing.T) {
778778
o := NewOperationV3(New())
779779
err := o.ParseComment(`@Param some_id `+paramType+` int true "Some ID"`, nil)
@@ -808,7 +808,7 @@ func TestOperation_ParseParamCommentV3(t *testing.T) {
808808

809809
t.Run("string", func(t *testing.T) {
810810
t.Parallel()
811-
for _, paramType := range []string{"header", "path", "query", "formData"} {
811+
for _, paramType := range []string{"header", "path", "query"} {
812812
t.Run(paramType, func(t *testing.T) {
813813
o := NewOperationV3(New())
814814
err := o.ParseComment(`@Param some_string `+paramType+` string true "Some String"`, nil)
@@ -842,7 +842,7 @@ func TestOperation_ParseParamCommentV3(t *testing.T) {
842842

843843
t.Run("object", func(t *testing.T) {
844844
t.Parallel()
845-
for _, paramType := range []string{"header", "path", "query", "formData"} {
845+
for _, paramType := range []string{"header", "path", "query"} {
846846
t.Run(paramType, func(t *testing.T) {
847847
assert.Error(t,
848848
NewOperationV3(New()).
@@ -1108,18 +1108,17 @@ func TestParseParamCommentByFormDataTypeV3(t *testing.T) {
11081108
err := operation.ParseComment(comment, nil)
11091109
assert.NoError(t, err)
11101110

1111-
assert.Len(t, operation.Parameters, 1)
1111+
assert.Len(t, operation.Parameters, 0)
1112+
assert.NotNil(t, operation.RequestBody)
11121113

1113-
parameters := operation.Operation.Parameters
1114-
assert.NotNil(t, parameters)
1114+
requestBody := operation.RequestBody
1115+
assert.True(t, requestBody.Spec.Spec.Required)
1116+
assert.Equal(t, "this is a test file", requestBody.Spec.Spec.Description)
1117+
assert.NotNil(t, requestBody)
11151118

1116-
parameterSpec := parameters[0].Spec.Spec
1117-
assert.NotNil(t, parameterSpec)
1118-
assert.Equal(t, "this is a test file", parameterSpec.Description)
1119-
assert.Equal(t, "file", parameterSpec.Name)
1120-
assert.True(t, parameterSpec.Required)
1121-
assert.Equal(t, "formData", parameterSpec.In)
1122-
assert.Equal(t, typeFile, parameterSpec.Schema.Spec.Type)
1119+
requestBodySpec := requestBody.Spec.Spec
1120+
assert.NotNil(t, requestBodySpec)
1121+
assert.Equal(t, typeFile, requestBodySpec.Content["application/x-www-form-urlencoded"].Spec.Schema.Spec.Type)
11231122
}
11241123

11251124
func TestParseParamCommentByFormDataTypeUint64V3(t *testing.T) {
@@ -1131,18 +1130,15 @@ func TestParseParamCommentByFormDataTypeUint64V3(t *testing.T) {
11311130
err := operation.ParseComment(comment, nil)
11321131
assert.NoError(t, err)
11331132

1134-
assert.Len(t, operation.Parameters, 1)
1133+
assert.Len(t, operation.Parameters, 0)
11351134

1136-
parameters := operation.Operation.Parameters
1137-
assert.NotNil(t, parameters)
1135+
requestBody := operation.RequestBody
1136+
assert.NotNil(t, requestBody)
1137+
assert.Equal(t, "this is a test file", requestBody.Spec.Spec.Description)
11381138

1139-
parameterSpec := parameters[0].Spec.Spec
1140-
assert.NotNil(t, parameterSpec)
1141-
assert.Equal(t, "this is a test file", parameterSpec.Description)
1142-
assert.Equal(t, "file", parameterSpec.Name)
1143-
assert.True(t, parameterSpec.Required)
1144-
assert.Equal(t, "formData", parameterSpec.In)
1145-
assert.Equal(t, typeInteger, parameterSpec.Schema.Spec.Type)
1139+
requestBodySpec := requestBody.Spec.Spec.Content["application/x-www-form-urlencoded"].Spec
1140+
assert.NotNil(t, requestBodySpec)
1141+
assert.Equal(t, typeInteger, requestBodySpec.Schema.Spec.Type)
11461142
}
11471143

11481144
func TestParseParamCommentByNotSupportedTypeV3(t *testing.T) {

parser_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2142,9 +2142,9 @@ func TestParseTypeOverrides(t *testing.T) {
21422142

21432143
searchDir := "testdata/global_override"
21442144
p := New(SetOverrides(map[string]string{
2145-
"github.com/swaggo/swag/testdata/global_override/types.Application": "string",
2146-
"github.com/swaggo/swag/testdata/global_override/types.Application2": "github.com/swaggo/swag/testdata/global_override/othertypes.Application",
2147-
"github.com/swaggo/swag/testdata/global_override/types.ShouldSkip": "",
2145+
"github.com/swaggo/swag/v2/testdata/global_override/types.Application": "string",
2146+
"github.com/swaggo/swag/v2/testdata/global_override/types.Application2": "github.com/swaggo/swag/v2/testdata/global_override/othertypes.Application",
2147+
"github.com/swaggo/swag/v2/testdata/global_override/types.ShouldSkip": "",
21482148
}))
21492149
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
21502150
assert.NoError(t, err)

parserv3_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func TestParseSimpleApiV3(t *testing.T) {
355355
assert.NoError(t, err)
356356

357357
paths := p.openAPI.Paths.Spec.Paths
358-
assert.Equal(t, 14, len(paths))
358+
assert.Equal(t, 15, len(paths))
359359

360360
path := paths["/testapi/get-string-by-int/{some_id}"].Spec.Spec.Get.Spec
361361
assert.Equal(t, "get string by ID", path.Description)
@@ -365,5 +365,8 @@ func TestParseSimpleApiV3(t *testing.T) {
365365
response := path.Responses.Spec.Response["200"]
366366
assert.Equal(t, "ok", response.Spec.Spec.Description)
367367

368+
path = paths["/FormData"].Spec.Spec.Post.Spec
369+
assert.NotNil(t, path)
370+
assert.NotNil(t, path.RequestBody)
368371
//TODO add asserts
369372
}

testdata/conflict_name/expected.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"200": {
2525
"description": "OK",
2626
"schema": {
27-
"$ref": "#/definitions/github_com_swaggo_swag_testdata_conflict_name_model.ErrorsResponse"
27+
"$ref": "#/definitions/github_com_swaggo_swag_v2_testdata_conflict_name_model.ErrorsResponse"
2828
}
2929
}
3030
}
@@ -47,39 +47,39 @@
4747
"200": {
4848
"description": "OK",
4949
"schema": {
50-
"$ref": "#/definitions/github_com_swaggo_swag_testdata_conflict_name_model2.ErrorsResponse"
50+
"$ref": "#/definitions/github_com_swaggo_swag_v2_testdata_conflict_name_model2.ErrorsResponse"
5151
}
5252
}
5353
}
5454
}
5555
}
5656
},
5757
"definitions": {
58-
"github_com_swaggo_swag_testdata_conflict_name_model.ErrorsResponse": {
58+
"github_com_swaggo_swag_v2_testdata_conflict_name_model.ErrorsResponse": {
5959
"type": "object",
6060
"properties": {
6161
"newTime": {
6262
"$ref": "#/definitions/model.MyPayload"
6363
}
6464
}
6565
},
66-
"github_com_swaggo_swag_testdata_conflict_name_model.MyStruct": {
66+
"github_com_swaggo_swag_v2_testdata_conflict_name_model.MyStruct": {
6767
"type": "object",
6868
"properties": {
6969
"name": {
7070
"type": "string"
7171
}
7272
}
7373
},
74-
"github_com_swaggo_swag_testdata_conflict_name_model2.ErrorsResponse": {
74+
"github_com_swaggo_swag_v2_testdata_conflict_name_model2.ErrorsResponse": {
7575
"type": "object",
7676
"properties": {
7777
"newTime": {
7878
"$ref": "#/definitions/model.MyPayload2"
7979
}
8080
}
8181
},
82-
"github_com_swaggo_swag_testdata_conflict_name_model2.MyStruct": {
82+
"github_com_swaggo_swag_v2_testdata_conflict_name_model2.MyStruct": {
8383
"type": "object",
8484
"properties": {
8585
"name": {
@@ -91,7 +91,7 @@
9191
"type": "object",
9292
"properties": {
9393
"my": {
94-
"$ref": "#/definitions/github_com_swaggo_swag_testdata_conflict_name_model.MyStruct"
94+
"$ref": "#/definitions/github_com_swaggo_swag_v2_testdata_conflict_name_model.MyStruct"
9595
},
9696
"name": {
9797
"type": "string"
@@ -102,7 +102,7 @@
102102
"type": "object",
103103
"properties": {
104104
"my": {
105-
"$ref": "#/definitions/github_com_swaggo_swag_testdata_conflict_name_model2.MyStruct"
105+
"$ref": "#/definitions/github_com_swaggo_swag_v2_testdata_conflict_name_model2.MyStruct"
106106
},
107107
"name": {
108108
"type": "string"

0 commit comments

Comments
 (0)