Skip to content

Commit 117e511

Browse files
authored
[GO] Fix value formatting in url with slices (#15581)
* fix reflect value for Slice * Add test
1 parent cfc1456 commit 117e511

4 files changed

Lines changed: 32 additions & 14 deletions

File tree

modules/openapi-generator/src/main/resources/go/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
189189
if reflect.TypeOf(t).Kind() == reflect.Slice {
190190
s := reflect.ValueOf(t)
191191
for i := 0; i < s.Len(); i++ {
192-
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", s.Index(i), "{{collectionFormat}}")
192+
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", s.Index(i).Interface(), "{{collectionFormat}}")
193193
}
194194
} else {
195195
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", t, "{{collectionFormat}}")
@@ -207,7 +207,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
207207
if reflect.TypeOf(t).Kind() == reflect.Slice {
208208
s := reflect.ValueOf(t)
209209
for i := 0; i < s.Len(); i++ {
210-
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", s.Index(i), "{{collectionFormat}}")
210+
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", s.Index(i).Interface(), "{{collectionFormat}}")
211211
}
212212
} else {
213213
parameterAddToHeaderOrQuery(localVarQueryParams, "{{baseName}}", t, "{{collectionFormat}}")

samples/client/petstore/go/go-petstore/api_fake.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/fake_api_test.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
)
1010

1111
const (
12-
deepObjectURL = `/v2/fake/deep_object_test?inputOptions[F1]=1&inputOptions[F2]=teststring&inputOptions[F3]=null&inputOptions[id]=1&inputOptions[name]=TestCat&test_pet[F1]=1&test_pet[F2]=teststring&test_pet[F3]=null&test_pet[id]=1&test_pet[name]=Test&test_pet[photoUrls]=http%3A%2F%2Flocalhost&test_pet[tags][F1]=1&test_pet[tags][F2]=teststring&test_pet[tags][F3]=null&test_pet[tags][id]=2&test_pet[tags][name]=tag1`
12+
deepObjectURL = `/v2/fake/deep_object_test?inputOptions[F1]=1&inputOptions[F2]=teststring&inputOptions[F3]=null&inputOptions[id]=1&inputOptions[name]=TestCat&test_pet[F1]=1&test_pet[F2]=teststring&test_pet[F3]=null&test_pet[id]=1&test_pet[name]=Test&test_pet[photoUrls]=http%3A%2F%2Flocalhost&test_pet[tags][F1]=1&test_pet[tags][F2]=teststring&test_pet[tags][F3]=null&test_pet[tags][id]=2&test_pet[tags][name]=tag1`
13+
paramCollectionFormatURL = `/v2/fake/test-query-parameters?context=context&http=http&ioutil=ioutil&pipe=pipe&url=url`
1314
)
1415

1516
// TestPutBodyWithFileSchema ensures a model with the name 'File'
@@ -39,12 +40,12 @@ func TestQueryDeepObject(t *testing.T) {
3940
var idTag1 = int64(2)
4041
var nameTag1 = "tag1"
4142
req = req.TestPet(sw.Pet{
42-
Id: &id,
43-
Name: "Test",
44-
PhotoUrls: []string{ "http://localhost" },
43+
Id: &id,
44+
Name: "Test",
45+
PhotoUrls: []string{"http://localhost"},
4546
Tags: []sw.Tag{
4647
{
47-
Id: &idTag1,
48+
Id: &idTag1,
4849
Name: &nameTag1,
4950
AdditionalProperties: map[string]interface{}{
5051
"F1": 1,
@@ -61,7 +62,7 @@ func TestQueryDeepObject(t *testing.T) {
6162
})
6263
var idcat = int64(1)
6364
req = req.InputOptions(sw.Category{
64-
Id: &idcat,
65+
Id: &idcat,
6566
Name: "TestCat",
6667
AdditionalProperties: map[string]interface{}{
6768
"F1": 1,
@@ -76,5 +77,22 @@ func TestQueryDeepObject(t *testing.T) {
7677

7778
assert.Equal(t,
7879
expectedDeepObjectURL,
79-
r.Request.URL.String() )
80+
r.Request.URL.String())
81+
}
82+
83+
func TestQueryParameterCollectionFormat(t *testing.T) {
84+
req := client.FakeAPI.TestQueryParameterCollectionFormat(context.Background()).
85+
Pipe([]string{"pipe"}).
86+
Ioutil([]string{"ioutil"}).
87+
Http([]string{"http"}).
88+
Url([]string{"url"}).
89+
Context([]string{"context"})
90+
91+
r, _ := req.Execute()
92+
93+
var expectedParamCollectionFormatURL = testScheme + "://" + testHost + paramCollectionFormatURL
94+
95+
assert.Equal(t,
96+
expectedParamCollectionFormatURL,
97+
r.Request.URL.String())
8098
}

samples/openapi3/client/petstore/go/go-petstore/api_fake.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)