Skip to content

Commit

Permalink
fix: disable array request body
Browse files Browse the repository at this point in the history
  • Loading branch information
kesonan committed Jun 28, 2024
1 parent 01bbc78 commit 46a4367
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
4 changes: 4 additions & 0 deletions tools/goctl/pkg/parser/api/ast/servicestatement.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,7 @@ func (e *BodyExpr) Pos() token.Position {
}

func (e *BodyExpr) exprNode() {}

func (e *BodyExpr) IsArrayType() bool {
return e.LBrack != nil
}
10 changes: 7 additions & 3 deletions tools/goctl/pkg/parser/api/parser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ func (a *Analyzer) fillService() error {
}

if astRoute.Route.Request != nil && astRoute.Route.Request.Body != nil {
requestType, err := a.getType(astRoute.Route.Request)
requestType, err := a.getType(astRoute.Route.Request, true)
if err != nil {
return err
}
route.RequestType = requestType
}
if astRoute.Route.Response != nil && astRoute.Route.Response.Body != nil {
responseType, err := a.getType(astRoute.Route.Response)
responseType, err := a.getType(astRoute.Route.Response, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -391,8 +391,12 @@ func (a *Analyzer) findDefinedType(name string) (spec.Type, error) {
return nil, fmt.Errorf("type %s not defined", name)
}

func (a *Analyzer) getType(expr *ast.BodyStmt) (spec.Type, error) {
func (a *Analyzer) getType(expr *ast.BodyStmt, req bool) (spec.Type, error) {
body := expr.Body
if req && body.IsArrayType() {
return nil, ast.SyntaxError(body.Pos(), "request body must be struct")
}

var tp spec.Type
var err error
var rawText = body.Format("")
Expand Down
4 changes: 4 additions & 0 deletions tools/goctl/pkg/parser/api/parser/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func Test_Parse(t *testing.T) {
for _, v := range testFile {
_, err := Parse(v, nil)
assertx.Error(t, err)
if err == nil {
data, _ := os.ReadFile(v)
fmt.Println(string(data))
}
}
})

Expand Down
23 changes: 16 additions & 7 deletions tools/goctl/pkg/parser/api/parser/testdata/invalid.api
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,6 @@ type Foo {
Bar *map[[]int]string `json:"bar"`
}

-----
// test case: map valu expected literal type
syntax = "v1"
type Foo {
Bar *map[string]{} `json:"bar"`
}

-----
// test case: invalid slice
syntax = "v1"
Expand Down Expand Up @@ -129,4 +122,20 @@ syntax = "v1"
service example {
@handler nestDemo
post /example/nest (NestDemoReq) returns (NestDemoResp)
}

-----
// test case: unsupported array object request body
syntax = "v1"
service example {
@handler nestDemo
post /example/nest ([]NestDemoReq) returns (NestDemoResp)
}

-----
// test case: unsupported array request body
syntax = "v1"
service example {
@handler nestDemo2
post /example/nest2 ([]string) returns (NestDemoResp)
}

0 comments on commit 46a4367

Please sign in to comment.