mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
fix: disable array request body (#4220)
This commit is contained in:
parent
2f9b6cf8ec
commit
4a62d084a9
@ -575,3 +575,7 @@ func (e *BodyExpr) Pos() token.Position {
|
||||
}
|
||||
|
||||
func (e *BodyExpr) exprNode() {}
|
||||
|
||||
func (e *BodyExpr) IsArrayType() bool {
|
||||
return e.LBrack != nil
|
||||
}
|
||||
|
@ -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("")
|
||||
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user