Skip to content

Commit

Permalink
fix: list of stringified json marshaling (twilio#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
eshanholtz authored Jul 6, 2021
1 parent 5b57605 commit fba5a1d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
36 changes: 36 additions & 0 deletions examples/go/unit/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,39 @@ func TestArrayTypeParam(t *testing.T) {
twilio := openapi.NewApiServiceWithClient(testClient)
twilio.CreateCallRecording("CA1234", &params)
}

func TestObjectArrayTypeParam(t *testing.T) {
item1 := map[string]interface{}{
"key1": "value1",
"key2": 2,
}
item2 := map[string]interface{}{
"key1": "value3",
"key2": 4,
}
testObjectArrayParam := []map[string]interface{}{item1, item2}
params := openapi.CreateCredentialAwsParams{}
params.SetTestObjectArray(testObjectArrayParam)

expectedData := url.Values{}
for _, item := range testObjectArrayParam {
obj, _ := json.Marshal(item)
expectedData.Add("TestObjectArray", string(obj))
}

mockCtrl := gomock.NewController(t)
testClient := NewMockBaseClient(mockCtrl)
testClient.EXPECT().SendRequest(
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any()).
DoAndReturn(func(method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
assert.Equal(t, expectedData, data)
return &http.Response{Body: ioutil.NopCloser(bytes.NewReader(nil))}, nil
},
)
twilio := openapi.NewApiServiceWithClient(testClient)
twilio.CreateCredentialAws(&params)
}
5 changes: 5 additions & 0 deletions examples/twilio_api_v2010.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ paths:
- consumer-checking
- consumer-savings
- commercial-checking
TestObjectArray:
description: test array of object transformation
items:
type: object
type: array
required:
- Credentials
title: CreateCredentialAwsRequest
Expand Down
23 changes: 18 additions & 5 deletions src/main/resources/twilio-go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,26 @@ func (c *ApiService) {{{nickname}}}({{#allParams}}{{#required}}{{paramName}} {{{
{{^isHeaderParam}}
if params != nil && params.{{paramName}} != nil {
{{#isFreeFormObject}}
v, err := json.Marshal(params.{{paramName}})
{{#isArray}}
for _, item := range *params.{{paramName}} {
v, err := json.Marshal(item)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}

data.Add("{{{baseName}}}", string(v))
}
{{/isArray}}
{{^isArray}}
v, err := json.Marshal(params.{{paramName}})

data.Set("{{{baseName}}}", string(v))
if err != nil {
return nil, err
}

data.Set("{{{baseName}}}", string(v))
{{/isArray}}
{{/isFreeFormObject}}
{{^isFreeFormObject}}
{{#isArray}}
Expand Down

0 comments on commit fba5a1d

Please sign in to comment.