Skip to content

Commit

Permalink
Merge pull request #1245 from tdakkota/fix/respect-array-parameter-se…
Browse files Browse the repository at this point in the history
…mantic

fix(gen): properly encode optional array parameter
  • Loading branch information
ernado authored May 20, 2024
2 parents d1f597e + dfdcdd7 commit 4e6d58f
Show file tree
Hide file tree
Showing 17 changed files with 736 additions and 117 deletions.
41 changes: 41 additions & 0 deletions _testdata/positive/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,47 @@
"version": "0.1.0"
},
"paths": {
"/optionalArrayParameter": {
"get": {
"operationId": "optionalArrayParameter",
"parameters": [
{
"name": "query",
"in": "query",
"required": false,
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
},
{
"name": "header",
"in": "header",
"required": false,
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
],
"responses": {
"200": {
"description": "Ok",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/objectQueryParameter": {
"get": {
"operationId": "objectQueryParameter",
Expand Down
21 changes: 12 additions & 9 deletions examples/ex_2ch/oas_request_encoders_gen.go

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

63 changes: 36 additions & 27 deletions examples/ex_github/oas_client_gen.go

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

21 changes: 12 additions & 9 deletions examples/ex_oauth2/oas_client_gen.go

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

21 changes: 12 additions & 9 deletions examples/ex_petstore_expanded/oas_client_gen.go

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

2 changes: 1 addition & 1 deletion gen/_template/json/encode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if {{ $.Var }}.Set {
{{- template "json/enc_array_elems" $ -}}
}
{{- else -}}
{{ errorf "unexpected nil semantic %s" $t.NilSemantic }}
{{ errorf "unexpected nil semantic %s" $t.NilSemantic }}
{{- end -}}
{{- end }}

Expand Down
29 changes: 21 additions & 8 deletions gen/_template/uri/encode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
{{- else if $t.IsEnum }}
return e.EncodeValue(conv.{{ $t.ToString }}({{ $t.Primitive.String }}({{ $var }})))
{{- else if $t.IsArray }}
return e.EncodeArray(func(e uri.Encoder) error {
for i, item := range {{ $var }} {
if err := func() error {
{{- template "uri/encode" elem $t.Item "item" }}
}(); err != nil {
return errors.Wrapf(err, "[%d]", i)
}
{{- if $t.NilSemantic.Invalid -}}
{{- template "uri/encode_array_elems" $ }}
{{- else if $t.NilSemantic.Optional -}}
if {{ $.Var }} != nil {
{{- template "uri/encode_array_elems" $ }}
}
return nil
})
{{- else -}}
{{ errorf "unexpected nil semantic %s" $t.NilSemantic }}
{{- end -}}
{{- else if $t.IsAlias }}
if unwrapped := {{ $t.AliasTo.Go }}({{ $var }}); true {
{{- template "uri/encode" elem $t.AliasTo "unwrapped" }}
Expand All @@ -38,3 +38,16 @@
{{ errorf "unexpected kind %s" $t.Kind }}
{{- end }}
{{- end }}

{{- define "uri/encode_array_elems" }}
return e.EncodeArray(func(e uri.Encoder) error {
for i, item := range {{ $.Var }} {
if err := func() error {
{{- template "uri/encode" elem $.Type.Item "item" }}
}(); err != nil {
return errors.Wrapf(err, "[%d]", i)
}
}
return nil
})
{{- end }}
35 changes: 35 additions & 0 deletions internal/integration/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package integration
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"

Expand All @@ -15,6 +17,12 @@ import (

type testParameters struct{}

var _ api.Handler = (*testParameters)(nil)

func (s *testParameters) OptionalArrayParameter(ctx context.Context, params api.OptionalArrayParameterParams) (string, error) {
return "", nil
}

func (s *testParameters) ObjectQueryParameter(ctx context.Context, params api.ObjectQueryParameterParams) (*api.ObjectQueryParameterOK, error) {
if param, ok := params.FormObject.Get(); ok {
return &api.ObjectQueryParameterOK{
Expand Down Expand Up @@ -175,3 +183,30 @@ func TestParameters(t *testing.T) {
assert.Equal(t, user, resp.Cookie)
})
}

func TestOptionalArrayParameter(t *testing.T) {
ctx := context.Background()

s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Ensure that client does not send these query parameters and headers.
if q := r.URL.Query(); q.Has("query") {
http.Error(w, "must have not query", http.StatusBadRequest)
return
}
if h := r.Header; len(h.Values("header")) > 0 {
http.Error(w, "must have not header", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = io.WriteString(w, `"ok"`)
}))
defer s.Close()

client, err := api.NewClient(s.URL, api.WithClient(s.Client()))
require.NoError(t, err)

resp, err := client.OptionalArrayParameter(ctx, api.OptionalArrayParameterParams{})
require.NoError(t, err)
require.Equal(t, "ok", resp)
}
42 changes: 24 additions & 18 deletions internal/integration/test_form/oas_request_encoders_gen.go

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

Loading

0 comments on commit 4e6d58f

Please sign in to comment.