Skip to content

Commit

Permalink
[DI-2039] feat: support marshaling of "any" type parameters in Go (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
childish-sambino authored Apr 11, 2022
1 parent cf92838 commit 11aa70d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 12 deletions.
6 changes: 6 additions & 0 deletions examples/go-client/helper/rest/api/v2010/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func TestPost(t *testing.T) {
params.SetTestDate("2022-01-01")
params.SetTestEnum("consumer-checking")

// "Any" type should expect any type of value. We'll test with an array of maps, but any type should work.
params.SetTestAnyType([]map[string]interface{}{{
"type": "include",
"all": true,
}})

resp, err := testApiService.CreateCredentialAws(params)
assert.Nil(t, err)
assert.NotNil(t, resp)
Expand Down
15 changes: 15 additions & 0 deletions examples/go-client/helper/rest/api/v2010/credentials_aws.go

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

1 change: 1 addition & 0 deletions examples/go-client/terraform/resources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ Name | Type | Requirement | Description
**test_date** | string | Optional |
**test_enum** | string | Optional |
**test_object_array** | list(string) | Optional |
**test_any_type** | string | Optional |
**sid** | string | *Computed* |

1 change: 1 addition & 0 deletions examples/go-client/terraform/resources/api_default.go

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

9 changes: 9 additions & 0 deletions examples/java/rest/api/v2010/credential/AwsCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class AwsCreator extends Creator<Aws>{
private LocalDate TestDate;
private String TestEnum;
private List<Object> TestObjectArray;
private Object TestAnyType;
public AwsCreator(final String TestString) {

this.TestString = TestString;
Expand Down Expand Up @@ -115,6 +116,10 @@ public AwsCreator setTestObjectArray(final List<Object> TestObjectArray){
this.TestObjectArray = TestObjectArray;
return this;
}
public AwsCreator setTestAnyType(final Object TestAnyType){
this.TestAnyType = TestAnyType;
return this;
}

@Override
public Aws create(final TwilioRestClient client){
Expand Down Expand Up @@ -196,5 +201,9 @@ private void addPostParams(final Request request) {
request.addPostParam("TestObjectArray", TestObjectArray.toString());
}

if (TestAnyType != null) {
request.addPostParam("TestAnyType", TestAnyType.toString());
}

}
}
1 change: 1 addition & 0 deletions examples/twilio_api_v2010.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ paths:
items:
type: object
type: array
TestAnyType: { }
required:
- TestString
title: CreateCredentialAwsRequest
Expand Down
6 changes: 3 additions & 3 deletions prism.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ cd examples/prism
docker-compose build
docker-compose up -d --force-recreate --remove-orphans


echo -n "Waiting for tests to complete"
while [ "$(docker-compose ps -q go-client-test | xargs docker inspect -f "{{.State.Status}}")" != "exited" ]
do
echo " Waiting for tests to complete"
echo -n "."
sleep 10
done
echo

EXIT_CODE=0
function check_status() {
docker_test_services=("$@")
for docker_test_service in "${docker_test_services[@]}"
do
echo "$docker_test_service"
if [[ $(docker-compose ps -q $docker_test_service | xargs docker inspect -f "{{.State.ExitCode}}") -ne 0 ]]
then
EXIT_CODE=$(($EXIT_CODE || $(docker-compose ps -q $docker_test_service | xargs docker inspect -f "{{.State.ExitCode}}")))
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/twilio/oai/TwilioGoGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.openapitools.codegen.*;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.SupportingFile;

public class TwilioGoGenerator extends AbstractTwilioGoGenerator {

Expand Down Expand Up @@ -110,6 +114,16 @@ public void postProcessParameter(final CodegenParameter parameter) {
// Make sure required non-path params get into the options block.
parameter.required = parameter.isPathParam;
parameter.vendorExtensions.put("x-custom", parameter.baseName.equals("limit"));

// Parameters (and their items) need to be marshalled to a string for inclusion in the request payload when
// they are either free-form objects (type: object) or any type objects (type is absent).
if (parameter.isFreeFormObject || parameter.isAnyType) {
parameter.vendorExtensions.put("x-marshal", true);
}

if (parameter.isArray && (parameter.items.isFreeFormObject || parameter.items.isAnyType)) {
parameter.items.vendorExtensions.put("x-marshal", true);
}
}

@Override
Expand Down
16 changes: 8 additions & 8 deletions src/main/resources/twilio-go/partial_serialization.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,33 @@ headers := make(map[string]interface{})
if params != nil && params.{{paramName}} != nil {
{{#isArray}}
for _, item := range *params.{{paramName}} {
{{#items.isFreeFormObject}}
{{#items.vendorExtensions.x-marshal}}
v, err := json.Marshal(item)

if err != nil {
return nil, err
}

data.Add("{{{baseName}}}", string(v))
{{/items.isFreeFormObject}}
{{^items.isFreeFormObject}}
{{/items.vendorExtensions.x-marshal}}
{{^items.vendorExtensions.x-marshal}}
data.Add("{{{baseName}}}", item)
{{/items.isFreeFormObject}}
{{/items.vendorExtensions.x-marshal}}
}
{{/isArray}}
{{^isArray}}
{{#isFreeFormObject}}
{{#vendorExtensions.x-marshal}}
v, err := json.Marshal(params.{{paramName}})

if err != nil {
return nil, err
}

data.Set("{{{baseName}}}", string(v))
{{/isFreeFormObject}}
{{^isFreeFormObject}}
{{/vendorExtensions.x-marshal}}
{{^vendorExtensions.x-marshal}}
data.Set("{{{baseName}}}", {{^isString}}fmt.Sprint({{/isString}}{{#isDateTime}}({{/isDateTime}}*params.{{paramName}}{{^isString}}{{#isDateTime}}).Format(time.RFC3339){{/isDateTime}}){{/isString}})
{{/isFreeFormObject}}
{{/vendorExtensions.x-marshal}}
{{/isArray}}
}
{{/vendorExtensions.x-custom}}
Expand Down

0 comments on commit 11aa70d

Please sign in to comment.