Skip to content

Commit

Permalink
V2: Fix tests after module upgrade (#1557)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Nerzal and Tobias Theel authored Apr 17, 2023
1 parent 6d1a872 commit acb0508
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 68 deletions.
2 changes: 1 addition & 1 deletion enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestParseGlobalEnums(t *testing.T) {
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
require.NoError(t, err)

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

Expand Down
2 changes: 1 addition & 1 deletion gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestGen_BuildDescriptionWithQuotes(t *testing.T) {
require.NoError(t, err)
}
}
cmd := exec.Command("go", "build", "-buildmode=plugin", "github.com/swaggo/swag/testdata/quotes")
cmd := exec.Command("go", "build", "-buildmode=plugin", "github.com/swaggo/swag/v2/testdata/quotes")

cmd.Dir = config.SearchDir

Expand Down
14 changes: 8 additions & 6 deletions operationv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (o *OperationV3) ParseParamComment(commentLine string, astFile *ast.File) e
case OBJECT:
return fmt.Errorf("%s is not supported type for %s", refType, paramType)
}
case "query", "formData":
case "query":
switch objectType {
case ARRAY:
if !IsPrimitiveType(refType) && !(refType == "file" && paramType == "formData") {
Expand Down Expand Up @@ -417,7 +417,7 @@ func (o *OperationV3) ParseParamComment(commentLine string, astFile *ast.File) e

return nil
}
case "body":
case "body", "formData":
if objectType == PRIMITIVE {
schema := PrimitiveSchemaV3(refType)

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

o.fillRequestBody(schema, required, description, true)
o.fillRequestBody(schema, required, description, true, paramType == "formData")

return nil

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

o.fillRequestBody(schema, required, description, false)
o.fillRequestBody(schema, required, description, false, paramType == "formData")

return nil

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

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

if primitive {
if primitive && !formData {
o.RequestBody.Spec.Spec.Content["text/plain"] = spec.NewMediaType()
} else if formData {
o.RequestBody.Spec.Spec.Content["application/x-www-form-urlencoded"] = spec.NewMediaType()
} else {
o.RequestBody.Spec.Spec.Content["application/json"] = spec.NewMediaType()
}
Expand Down
42 changes: 19 additions & 23 deletions operationv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ func TestOperation_ParseParamCommentV3(t *testing.T) {

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

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

t.Run("object", func(t *testing.T) {
t.Parallel()
for _, paramType := range []string{"header", "path", "query", "formData"} {
for _, paramType := range []string{"header", "path", "query"} {
t.Run(paramType, func(t *testing.T) {
assert.Error(t,
NewOperationV3(New()).
Expand Down Expand Up @@ -1108,18 +1108,17 @@ func TestParseParamCommentByFormDataTypeV3(t *testing.T) {
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)

assert.Len(t, operation.Parameters, 1)
assert.Len(t, operation.Parameters, 0)
assert.NotNil(t, operation.RequestBody)

parameters := operation.Operation.Parameters
assert.NotNil(t, parameters)
requestBody := operation.RequestBody
assert.True(t, requestBody.Spec.Spec.Required)
assert.Equal(t, "this is a test file", requestBody.Spec.Spec.Description)
assert.NotNil(t, requestBody)

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

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

assert.Len(t, operation.Parameters, 1)
assert.Len(t, operation.Parameters, 0)

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

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

func TestParseParamCommentByNotSupportedTypeV3(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2142,9 +2142,9 @@ func TestParseTypeOverrides(t *testing.T) {

searchDir := "testdata/global_override"
p := New(SetOverrides(map[string]string{
"github.com/swaggo/swag/testdata/global_override/types.Application": "string",
"github.com/swaggo/swag/testdata/global_override/types.Application2": "github.com/swaggo/swag/testdata/global_override/othertypes.Application",
"github.com/swaggo/swag/testdata/global_override/types.ShouldSkip": "",
"github.com/swaggo/swag/v2/testdata/global_override/types.Application": "string",
"github.com/swaggo/swag/v2/testdata/global_override/types.Application2": "github.com/swaggo/swag/v2/testdata/global_override/othertypes.Application",
"github.com/swaggo/swag/v2/testdata/global_override/types.ShouldSkip": "",
}))
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)
Expand Down
5 changes: 4 additions & 1 deletion parserv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func TestParseSimpleApiV3(t *testing.T) {
assert.NoError(t, err)

paths := p.openAPI.Paths.Spec.Paths
assert.Equal(t, 14, len(paths))
assert.Equal(t, 15, len(paths))

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

path = paths["/FormData"].Spec.Spec.Post.Spec
assert.NotNil(t, path)
assert.NotNil(t, path.RequestBody)
//TODO add asserts
}
16 changes: 8 additions & 8 deletions testdata/conflict_name/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/github_com_swaggo_swag_testdata_conflict_name_model.ErrorsResponse"
"$ref": "#/definitions/github_com_swaggo_swag_v2_testdata_conflict_name_model.ErrorsResponse"
}
}
}
Expand All @@ -47,39 +47,39 @@
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/github_com_swaggo_swag_testdata_conflict_name_model2.ErrorsResponse"
"$ref": "#/definitions/github_com_swaggo_swag_v2_testdata_conflict_name_model2.ErrorsResponse"
}
}
}
}
}
},
"definitions": {
"github_com_swaggo_swag_testdata_conflict_name_model.ErrorsResponse": {
"github_com_swaggo_swag_v2_testdata_conflict_name_model.ErrorsResponse": {
"type": "object",
"properties": {
"newTime": {
"$ref": "#/definitions/model.MyPayload"
}
}
},
"github_com_swaggo_swag_testdata_conflict_name_model.MyStruct": {
"github_com_swaggo_swag_v2_testdata_conflict_name_model.MyStruct": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"github_com_swaggo_swag_testdata_conflict_name_model2.ErrorsResponse": {
"github_com_swaggo_swag_v2_testdata_conflict_name_model2.ErrorsResponse": {
"type": "object",
"properties": {
"newTime": {
"$ref": "#/definitions/model.MyPayload2"
}
}
},
"github_com_swaggo_swag_testdata_conflict_name_model2.MyStruct": {
"github_com_swaggo_swag_v2_testdata_conflict_name_model2.MyStruct": {
"type": "object",
"properties": {
"name": {
Expand All @@ -91,7 +91,7 @@
"type": "object",
"properties": {
"my": {
"$ref": "#/definitions/github_com_swaggo_swag_testdata_conflict_name_model.MyStruct"
"$ref": "#/definitions/github_com_swaggo_swag_v2_testdata_conflict_name_model.MyStruct"
},
"name": {
"type": "string"
Expand All @@ -102,7 +102,7 @@
"type": "object",
"properties": {
"my": {
"$ref": "#/definitions/github_com_swaggo_swag_testdata_conflict_name_model2.MyStruct"
"$ref": "#/definitions/github_com_swaggo_swag_v2_testdata_conflict_name_model2.MyStruct"
},
"name": {
"type": "string"
Expand Down
Loading

0 comments on commit acb0508

Please sign in to comment.