diff --git a/tools/goctl/api/gogen/gen_test.go b/tools/goctl/api/gogen/gen_test.go index 5ba5c3e1..4241e117 100644 --- a/tools/goctl/api/gogen/gen_test.go +++ b/tools/goctl/api/gogen/gen_test.go @@ -178,6 +178,13 @@ service A-api { } ` +const apiHasNoRequest = ` +service A-api { + @handler GreetHandler + post /greet/ping () +} +` + func TestParser(t *testing.T) { filename := "greet.api" err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm) @@ -311,6 +318,21 @@ func TestApiHasJwtAndMiddleware(t *testing.T) { validate(t, filename) } +func TestApiHasNoRequestBody(t *testing.T) { + filename := "greet.api" + err := ioutil.WriteFile(filename, []byte(apiHasNoRequest), os.ModePerm) + assert.Nil(t, err) + defer os.Remove(filename) + + parser, err := parser.NewParser(filename) + assert.Nil(t, err) + + _, err = parser.Parse() + assert.Nil(t, err) + + validate(t, filename) +} + func validate(t *testing.T, api string) { dir := "_go" err := DoGenProject(api, dir, true) diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index 5c7802fc..34a7649e 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -23,14 +23,14 @@ import ( func {{.HandlerName}}(ctx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.{{.RequestType}} + {{if .HasRequest}}var req types.{{.RequestType}} if err := httpx.Parse(r, &req); err != nil { httpx.Error(w, err) return - } + }{{end}} l := logic.New{{.LogicType}}(r.Context(), ctx) - {{if .HasResp}}resp, {{end}}err := l.{{.Call}}(req) + {{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}req{{end}}) if err != nil { httpx.Error(w, err) } else { @@ -47,6 +47,7 @@ type Handler struct { LogicType string Call string HasResp bool + HasRequest bool } func genHandler(dir string, group spec.Group, route spec.Route) error { @@ -71,6 +72,7 @@ func genHandler(dir string, group spec.Group, route spec.Route) error { LogicType: strings.TrimSuffix(strings.Title(handler), "Handler") + "Logic", Call: strings.Title(strings.TrimSuffix(handler, "Handler")), HasResp: len(route.ResponseType.Name) > 0, + HasRequest: len(route.RequestType.Name) > 0, }) }