Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/goctl/api/swagger/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func isPostJson(ctx Context, method string, tp apiSpec.Type) (string, bool) {
if strings.EqualFold(method, http.MethodPost) {
if !strings.EqualFold(method, http.MethodPost) {
return "", false
}
structType, ok := tp.(apiSpec.DefineStruct)
Expand Down
91 changes: 91 additions & 0 deletions tools/goctl/api/swagger/parameter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package swagger

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
apiSpec "github.com/zeromicro/go-zero/tools/goctl/api/spec"
)

func TestIsPostJson(t *testing.T) {
tests := []struct {
name string
method string
hasJson bool
expected bool
}{
{"POST with JSON", http.MethodPost, true, true},
{"POST without JSON", http.MethodPost, false, false},
{"GET with JSON", http.MethodGet, true, false},
{"PUT with JSON", http.MethodPut, true, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testStruct := createTestStruct("TestStruct", tt.hasJson)
_, result := isPostJson(testingContext(t), tt.method, testStruct)
assert.Equal(t, tt.expected, result)
})
}
}

func TestParametersFromType(t *testing.T) {
tests := []struct {
name string
method string
useDefinitions bool
hasJson bool
expectedCount int
expectedBody bool
}{
{"POST JSON with definitions", http.MethodPost, true, true, 1, true},
{"POST JSON without definitions", http.MethodPost, false, true, 1, true},
{"GET with form", http.MethodGet, false, false, 1, false},
{"POST with form", http.MethodPost, false, false, 1, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := Context{UseDefinitions: tt.useDefinitions}
testStruct := createTestStruct("TestStruct", tt.hasJson)
params := parametersFromType(ctx, tt.method, testStruct)

assert.Equal(t, tt.expectedCount, len(params))
if tt.expectedBody {
assert.Equal(t, paramsInBody, params[0].In)
} else if len(params) > 0 {
assert.NotEqual(t, paramsInBody, params[0].In)
}
})
}
}

func TestParametersFromType_EdgeCases(t *testing.T) {
ctx := testingContext(t)

params := parametersFromType(ctx, http.MethodPost, nil)
assert.Empty(t, params)

primitiveType := apiSpec.PrimitiveType{RawName: "string"}
params = parametersFromType(ctx, http.MethodPost, primitiveType)
assert.Empty(t, params)
}

func createTestStruct(name string, hasJson bool) apiSpec.DefineStruct {
tag := `form:"username"`
if hasJson {
tag = `json:"username"`
}

return apiSpec.DefineStruct{
RawName: name,
Members: []apiSpec.Member{
{
Name: "Username",
Type: apiSpec.PrimitiveType{RawName: "string"},
Tag: tag,
},
},
}
}