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
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) 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 { 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 { if err != nil {
return err return err
} }
route.RequestType = requestType route.RequestType = requestType
} }
if astRoute.Route.Response != nil && astRoute.Route.Response.Body != nil { 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 { if err != nil {
return err return err
} }
@@ -404,8 +404,12 @@ func (a *Analyzer) findDefinedType(name string) (spec.Type, error) {
return nil, fmt.Errorf("type %s not defined", name) 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 body := expr.Body
if req && body.IsArrayType() {
return nil, ast.SyntaxError(body.Pos(), "request body must be struct")
}
var tp spec.Type var tp spec.Type
var err error var err error
var rawText = body.Format("") var rawText = body.Format("")

View File

@@ -95,13 +95,6 @@ type Foo {
Bar *map[[]int]string `json:"bar"` 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 // test case: invalid slice
syntax = "v1" syntax = "v1"
@@ -193,4 +186,20 @@ syntax = "v1"
service example { service example {
@handler nestDemo @handler nestDemo
post /example/nest (NestDemoReq) returns (NestDemoResp) 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)
} }