fix: disable array request body (#4220)

This commit is contained in:
kesonan 2024-07-02 11:55:01 +08:00 committed by GitHub
parent 2f9b6cf8ec
commit 4a62d084a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 10 deletions

View File

@ -575,3 +575,7 @@ func (e *BodyExpr) Pos() token.Position {
}
func (e *BodyExpr) exprNode() {}
func (e *BodyExpr) IsArrayType() bool {
return e.LBrack != nil
}

View File

@ -268,14 +268,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
}
@ -404,8 +404,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("")

View File

@ -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"
@ -194,3 +187,19 @@ 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)
}