Skip to content

Commit 7109250

Browse files
authored
Merge pull request #61 from VanHai1424/fix/form-param-handling
fix: correctly handle form and file parameters as requestBody instead of query parameters
2 parents 1e1411d + b4163f0 commit 7109250

File tree

3 files changed

+109
-7
lines changed

3 files changed

+109
-7
lines changed

parser/operations/param.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ func (p *parser) parseParamComment(pkgPath, pkgName string, operation *oas.Opera
2929

3030
goType := getType(re, matches)
3131

32-
// `file`, `form`
33-
appendRequestBody(operation, parameterObject, goType)
32+
switch parameterObject.In {
33+
// file, form
34+
case "form", "file":
35+
appendRequestBody(operation, parameterObject, goType)
36+
return nil
37+
// body
38+
case "body":
39+
return p.parseRequestBody(pkgPath, pkgName, operation, parameterObject, goType, matches)
3440

35-
// `path`, `query`, `header`, `cookie`
36-
if parameterObject.In != "body" {
41+
// path, query, header, cookie
42+
default:
3743
return p.appendQueryParam(pkgPath, pkgName, operation, parameterObject, goType)
3844
}
39-
40-
return p.parseRequestBody(pkgPath, pkgName, operation, parameterObject, goType, matches)
4145
}
4246

4347
func (p *parser) parseRequestBody(pkgPath string, pkgName string, operation *oas.OperationObject, parameterObject oas.ParameterObject, goType string, matches []string) error {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package operations
2+
3+
import (
4+
"go/ast"
5+
6+
"github.com/parvez3019/go-swagger3/openApi3Schema"
7+
"github.com/stretchr/testify/mock"
8+
)
9+
10+
type MockSchemaParser struct {
11+
mock.Mock
12+
}
13+
14+
func (m *MockSchemaParser) GetPkgAst(pkgPath string) (map[string]*ast.Package, error) {
15+
args := m.Called(pkgPath)
16+
return args.Get(0).(map[string]*ast.Package), args.Error(1)
17+
}
18+
19+
func (m *MockSchemaParser) RegisterType(pkgPath, pkgName, typeName string) (string, error) {
20+
args := m.Called(pkgPath, pkgName, typeName)
21+
return args.String(0), args.Error(1)
22+
}
23+
24+
func (m *MockSchemaParser) ParseSchemaObject(pkgPath, pkgName, typeName string) (*openApi3Schema.SchemaObject, error) {
25+
args := m.Called(pkgPath, pkgName, typeName)
26+
if obj := args.Get(0); obj != nil {
27+
return obj.(*openApi3Schema.SchemaObject), args.Error(1)
28+
}
29+
return nil, args.Error(1)
30+
}

parser/operations/parser_test.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package operations
22

33
import (
44
"errors"
5+
"testing"
6+
57
oas "github.com/parvez3019/go-swagger3/openApi3Schema"
68
"github.com/parvez3019/go-swagger3/parser/schema"
79
"github.com/stretchr/testify/assert"
8-
"testing"
910
)
1011

1112
func Test_ParseHeader(t *testing.T) {
@@ -52,3 +53,70 @@ func Test_ParseHeader(t *testing.T) {
5253
})
5354
}
5455
}
56+
57+
func TestParseParamComment_FormParam(t *testing.T) {
58+
p := &parser{}
59+
op := &oas.OperationObject{}
60+
61+
comment := "@Param file form ignored true \"Upload file\" \"/path/to/file\""
62+
63+
err := p.parseParamComment("example/pkg", "pkg", op, comment)
64+
if err != nil {
65+
t.Fatalf("Expected no error, got: %v", err)
66+
}
67+
if op.RequestBody == nil {
68+
t.Error("Expected RequestBody to be set for form param")
69+
}
70+
}
71+
72+
func TestParseParamComment_BodyParam(t *testing.T) {
73+
mockSchema := new(MockSchemaParser)
74+
75+
p := &parser{
76+
Parser: mockSchema,
77+
}
78+
79+
op := &oas.OperationObject{}
80+
81+
comment := "@Param user body User true \"User info\""
82+
83+
mockSchema.On("RegisterType", "example/pkg", "pkg", "User").Return("UserSchemaRef", nil)
84+
85+
err := p.parseParamComment("example/pkg", "pkg", op, comment)
86+
if err != nil {
87+
t.Fatalf("Expected no error, got: %v", err)
88+
}
89+
90+
if op.RequestBody == nil {
91+
t.Error("Expected RequestBody to be set for body param")
92+
}
93+
94+
mockSchema.AssertExpectations(t)
95+
}
96+
97+
func TestParseParamComment_QueryParam(t *testing.T) {
98+
p := &parser{}
99+
op := &oas.OperationObject{}
100+
101+
comment := "@Param id query int true \"User ID\""
102+
103+
err := p.parseParamComment("example/pkg", "pkg", op, comment)
104+
if err != nil {
105+
t.Fatalf("Expected no error, got: %v", err)
106+
}
107+
if len(op.Parameters) == 0 {
108+
t.Error("Expected query param to be added to operation.Parameters")
109+
}
110+
}
111+
112+
func TestParseParamComment_InvalidComment(t *testing.T) {
113+
p := &parser{}
114+
op := &oas.OperationObject{}
115+
116+
comment := "@Param invalid format only"
117+
118+
err := p.parseParamComment("pkg", "pkg", op, comment)
119+
if err == nil {
120+
t.Error("Expected error for invalid comment format, but got none")
121+
}
122+
}

0 commit comments

Comments
 (0)