diff --git a/cmd/internal/svc/codegen/httphandlerimpl.go b/cmd/internal/svc/codegen/httphandlerimpl.go index 53836360..0255c8f2 100644 --- a/cmd/internal/svc/codegen/httphandlerimpl.go +++ b/cmd/internal/svc/codegen/httphandlerimpl.go @@ -25,7 +25,7 @@ var appendHttpHandlerImplTmpl = ` {{ $p.Name }} = new({{ $p.Type | toSlice }}) {{- else if eq $p.Type "context.Context"}} {{ $p.Name }} {{ $p.Type }} - {{- else if and (eq $m.HttpMethod "GET") (not (isBuiltin $p)) }} + {{- else if and (and (eq $m.HttpMethod "GET") (not (isBuiltin $p))) (gt (len $m.QueryVars) 1) }} {{ $p.Name }}Wrapper struct { {{ $p.Name | title }} {{ $p.Type }} ` + "`" + `json:"{{ $p.Name }}"` + "`" + ` } @@ -143,6 +143,7 @@ var appendHttpHandlerImplTmpl = ` } {{- $formParsed = true }} {{- end }} + {{- if gt (len $m.QueryVars) 1 }} if _err := rest.DecodeForm(&{{ $p.Name }}Wrapper, _req.Form); _err != nil { rest.HandleBadRequestErr(_err) } else { @@ -157,6 +158,21 @@ var appendHttpHandlerImplTmpl = ` {{- end }} } {{- else }} + if _err := rest.DecodeForm(&{{ $p.Name }}, _req.Form); _err != nil { + rest.HandleBadRequestErr(_err) + } else { + {{- if isStruct $p }} + if _err := rest.ValidateStruct({{ $p.Name }}); _err != nil { + rest.HandleBadRequestErr(_err) + } + {{- else }} + if _err := rest.ValidateVar({{ $p.Name }}, "{{$p.ValidateTag}}", ""); _err != nil { + rest.HandleBadRequestErr(_err) + } + {{- end }} + } + {{- end }} + {{- else }} {{- if isOptional $p.Type }} if _err := json.NewDecoder(_req.Body).Decode(&{{$p.Name}}); _err != nil { if _err != io.EOF { @@ -320,7 +336,7 @@ var appendHttpHandlerImplTmpl = ` *{{ $p.Name }}..., {{- else if eq $p.Type "context.Context"}} {{ $p.Name }}, - {{- else if and (eq $m.HttpMethod "GET") (not (isBuiltin $p)) }} + {{- else if and (and (eq $m.HttpMethod "GET") (not (isBuiltin $p))) (gt (len $m.QueryVars) 1) }} {{ $p.Name }}Wrapper.{{ $p.Name | title }}, {{- else }} {{ $p.Name }}, diff --git a/framework/rest/server.go b/framework/rest/server.go index 19863fb2..fc256a45 100644 --- a/framework/rest/server.go +++ b/framework/rest/server.go @@ -403,16 +403,14 @@ func (srv *RestServer) Serve(ln net.Listener) { docs := mapset.NewSet[DocItem]() for _, r := range all { if strings.Contains(r.Pattern, gddPathPrefix+"doc") { - resources := strings.Split(strings.TrimPrefix(strings.TrimSuffix(r.Pattern, gddPathPrefix+"doc"), "/"), "/") - if len(resources) > 0 { - module := resources[len(resources)-1] - if stringutils.IsNotEmpty(module) { - docs.Add(DocItem{ - Label: module, - Value: r.Pattern, - }) - } + module := strings.TrimSuffix(r.Pattern, gddPathPrefix+"doc") + if stringutils.IsEmpty(module) { + module = "/" } + docs.Add(DocItem{ + Label: module, + Value: r.Pattern, + }) } } Docs = docs.ToSlice() diff --git a/toolkit/astutils/funcs.go b/toolkit/astutils/funcs.go index 2884f8ba..562367a9 100644 --- a/toolkit/astutils/funcs.go +++ b/toolkit/astutils/funcs.go @@ -386,7 +386,8 @@ type MethodMeta struct { // all will be put into request body as multipart/form-data data. Params []FieldMeta // Results response - Results []FieldMeta + Results []FieldMeta + QueryVars []FieldMeta // PathVars not support when generate client code from service interface in svc.go file // when generate client code from openapi3 spec json file, PathVars is parameters in url as path variable. PathVars []FieldMeta diff --git a/toolkit/astutils/interfacecollector.go b/toolkit/astutils/interfacecollector.go index 20096070..1ea4db58 100644 --- a/toolkit/astutils/interfacecollector.go +++ b/toolkit/astutils/interfacecollector.go @@ -116,12 +116,16 @@ func (ic *InterfaceCollector) field2Methods(list []*ast.Field) []MethodMeta { params = ic.field2Params(ft.Params.List) } + queryVars := make([]FieldMeta, 0) httpMethod, endpoint := Pattern(mn) pvs := pathVariables(endpoint) for i := range params { if sliceutils.StringContains(pvs, params[i].Name) { params[i].IsPathVariable = true } + if httpMethod == http.MethodGet && !params[i].IsPathVariable && params[i].Type != "context.Context" { + queryVars = append(queryVars, params[i]) + } } var results []FieldMeta @@ -136,6 +140,7 @@ func (ic *InterfaceCollector) field2Methods(list []*ast.Field) []MethodMeta { Annotations: annotations, HasPathVariable: len(pvs) > 0, HttpMethod: httpMethod, + QueryVars: queryVars, }) } return methods