From fba5a1df62c25839e65dd2f0dec216a097a27a2c Mon Sep 17 00:00:00 2001 From: Elise Shanholtz Date: Tue, 6 Jul 2021 16:56:59 -0700 Subject: [PATCH] fix: list of stringified json marshaling (#82) --- examples/go/unit/unit_test.go | 36 +++++++++++++++++++++++ examples/twilio_api_v2010.yaml | 5 ++++ src/main/resources/twilio-go/api.mustache | 23 +++++++++++---- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/examples/go/unit/unit_test.go b/examples/go/unit/unit_test.go index e9790514e..e72084ef0 100644 --- a/examples/go/unit/unit_test.go +++ b/examples/go/unit/unit_test.go @@ -163,3 +163,39 @@ func TestArrayTypeParam(t *testing.T) { twilio := openapi.NewApiServiceWithClient(testClient) twilio.CreateCallRecording("CA1234", ¶ms) } + +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(¶ms) +} diff --git a/examples/twilio_api_v2010.yaml b/examples/twilio_api_v2010.yaml index 82f3c01fb..a7f7ff7de 100644 --- a/examples/twilio_api_v2010.yaml +++ b/examples/twilio_api_v2010.yaml @@ -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 diff --git a/src/main/resources/twilio-go/api.mustache b/src/main/resources/twilio-go/api.mustache index 047d1daa8..04326bad8 100644 --- a/src/main/resources/twilio-go/api.mustache +++ b/src/main/resources/twilio-go/api.mustache @@ -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}}