Repository: zeromicro/go-zero
Pull request #4220 (2024-06-28 11:14:12)

fix: disable array request body

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/4220?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 95.10%. Comparing base [(`8690859`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/8690859c7d1c9461bdc422c3c65608bde30904db?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) to head [(`46a4367`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/46a4367d251af539ed37079bcf648efd4f2cdd2c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro).
> Report is 71 commits behind head on master.

> :exclamation: **Current head 46a4367 differs from pull request most recent head d745111**
> 
> Please [upload](https://docs.codecov.com/docs/codecov-uploader) reports for the commit d745111 to get more accurate results.

<details><summary>Additional details and impacted files</summary>


[see 255 files with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/4220/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro)

</details>

Patch:
diff --git a/tools/goctl/pkg/parser/api/ast/servicestatement.go b/tools/goctl/pkg/parser/api/ast/servicestatement.go
index 05b6d3ae..ba0b704d 100644
--- a/tools/goctl/pkg/parser/api/ast/servicestatement.go
+++ b/tools/goctl/pkg/parser/api/ast/servicestatement.go
@@ -575,3 +575,7 @@ func (e *BodyExpr) Pos() token.Position {
 }
 
 func (e *BodyExpr) exprNode() {}
+
+func (e *BodyExpr) IsArrayType() bool {
+	return e.LBrack != nil
+}
diff --git a/tools/goctl/pkg/parser/api/parser/analyzer.go b/tools/goctl/pkg/parser/api/parser/analyzer.go
index b47e7039..2b1c302a 100644
--- a/tools/goctl/pkg/parser/api/parser/analyzer.go
+++ b/tools/goctl/pkg/parser/api/parser/analyzer.go
@@ -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("")


Test patch:
diff --git a/tools/goctl/pkg/parser/api/parser/testdata/invalid.api b/tools/goctl/pkg/parser/api/parser/testdata/invalid.api
index db57c6f4..f14f2f67 100644
--- a/tools/goctl/pkg/parser/api/parser/testdata/invalid.api
+++ b/tools/goctl/pkg/parser/api/parser/testdata/invalid.api
@@ -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"
@@ -193,4 +186,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)
 }
\ No newline at end of file