Skip to content

Commit

Permalink
Merge pull request #859 from tdakkota/fix/unused-query-decoder
Browse files Browse the repository at this point in the history
fix(gen): define form decoder variable only if necessary
  • Loading branch information
ernado authored Apr 12, 2023
2 parents ba39646 + 76d775d commit 847dd1e
Show file tree
Hide file tree
Showing 16 changed files with 1,271 additions and 30 deletions.
87 changes: 86 additions & 1 deletion _testdata/positive/http_requests.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,91 @@
}
}
}
},
"/onlyForm": {
"post": {
"operationId": "onlyForm",
"requestBody": {
"required": true,
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"type": "object",
"required": [
"field"
],
"properties": {
"field": {
"type": "integer"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Ok"
}
}
}
},
"/onlyMultipartForm": {
"post": {
"operationId": "onlyMultipartForm",
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"required": [
"field"
],
"properties": {
"field": {
"type": "integer"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Ok"
}
}
}
},
"/onlyMultipartFile": {
"post": {
"operationId": "onlyMultipartFile",
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"required": [
"file"
],
"properties": {
"file": {
"type": "string",
"format": "binary"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Ok"
}
}
}
}
},
"components": {
Expand Down Expand Up @@ -253,4 +338,4 @@
}
}
}
}
}
6 changes: 6 additions & 0 deletions examples/ex_2ch/oas_request_decoders_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 31 additions & 27 deletions gen/_template/request_decode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) decode{{ $op.Name }}Req
// Notice that the closers are called in reverse order, to match defer behavior, so
// any opened file will be closed before RemoveAll call.
closers = append(closers, r.MultipartForm.RemoveAll)
// Form values may be unused.
form := url.Values(r.MultipartForm.Value)
_ = form
{{- end }}

var request {{ $t.Go }}
Expand Down Expand Up @@ -192,36 +194,38 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) decode{{ $op.Name }}Req
}
}
{{- else if $t.IsStruct }}
{{- with $t.FormParameters }}
q := uri.NewQueryDecoder(form)
{{- range $p := $t.FormParameters }}
{
{{- $el := elem $p.Type (printf "%s.%s" $.Var $p.Name) }}
cfg := uri.QueryParameterDecodingConfig{
Name: {{ quote $p.Spec.Name }},
Style: uri.QueryStyle{{ capitalize $p.Spec.Style.String }},
Explode: {{ if $p.Spec.Explode }}true{{ else }}false{{ end }},
{{- if isObjectParam $p }}
Fields: {{ paramObjectFields $p.Type }},
{{- end }}
}
if err := q.HasParam(cfg); err == nil {
if err := q.DecodeParam(cfg, func(d uri.Decoder) error {
{{- template "uri/decode" $el }}
}); err != nil {
return req, close, errors.Wrap(err, {{ printf "decode %q" $p.Spec.Name | quote }})
{{- range $p := $t.FormParameters }}
{
{{- $el := elem $p.Type (printf "%s.%s" $.Var $p.Name) }}
cfg := uri.QueryParameterDecodingConfig{
Name: {{ quote $p.Spec.Name }},
Style: uri.QueryStyle{{ capitalize $p.Spec.Style.String }},
Explode: {{ if $p.Spec.Explode }}true{{ else }}false{{ end }},
{{- if isObjectParam $p }}
Fields: {{ paramObjectFields $p.Type }},
{{- end }}
}
if err := q.HasParam(cfg); err == nil {
if err := q.DecodeParam(cfg, func(d uri.Decoder) error {
{{- template "uri/decode" $el }}
}); err != nil {
return req, close, errors.Wrap(err, {{ printf "decode %q" $p.Spec.Name | quote }})
}

{{- if $p.Type.NeedValidation }}
if err := func() error {
{{- template "validate" $el }}
}(); err != nil {
return req, close, errors.Wrap(err, "validate")
}
{{- end }}
} {{- if $p.Spec.Required }} else {
return req, close, errors.Wrap(err, "query")
} {{- end }}
}
{{- if $p.Type.NeedValidation }}
if err := func() error {
{{- template "validate" $el }}
}(); err != nil {
return req, close, errors.Wrap(err, "validate")
}
{{- end }}
} {{- if $p.Spec.Required }} else {
return req, close, errors.Wrap(err, "query")
} {{- end }}
}
{{- end }}
{{- end }}
{{- range $p := $t.FileParameters }}
{
Expand Down
16 changes: 14 additions & 2 deletions internal/integration/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

"github.com/stretchr/testify/require"

ht "github.com/ogen-go/ogen/http"
api "github.com/ogen-go/ogen/internal/integration/test_http_requests"
)

type testHTTPRequests struct {
}
type testHTTPRequests struct{}

func (t testHTTPRequests) Base64Request(ctx context.Context, req api.Base64RequestReq) (api.Base64RequestOK, error) {
return api.Base64RequestOK{
Expand Down Expand Up @@ -101,6 +101,18 @@ func (t testHTTPRequests) MaskContentTypeOptional(ctx context.Context, req *api.
}, nil
}

func (t testHTTPRequests) OnlyForm(ctx context.Context, req *api.OnlyFormReq) error {
return ht.ErrNotImplemented
}

func (t testHTTPRequests) OnlyMultipartFile(ctx context.Context, req *api.OnlyMultipartFileReqForm) error {
return ht.ErrNotImplemented
}

func (t testHTTPRequests) OnlyMultipartForm(ctx context.Context, req *api.OnlyMultipartFormReq) error {
return ht.ErrNotImplemented
}

func (t testHTTPRequests) StreamJSON(ctx context.Context, req []float64) (v float64, _ error) {
for _, f := range req {
v += f
Expand Down
4 changes: 4 additions & 0 deletions internal/integration/test_allof/oas_request_decoders_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/integration/test_form/oas_request_decoders_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 847dd1e

Please sign in to comment.