Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Go json content type support #530

Merged
merged 12 commits into from
Oct 27, 2023
4 changes: 4 additions & 0 deletions src/main/java/com/twilio/oai/TwilioGoGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public OperationsMap postProcessOperationsWithModels(final OperationsMap objs, L

for (final CodegenOperation co : opList) {
Utility.populateCrudOperations(co);
Utility.resolveContentType(co);

if (co.nickname.startsWith("List")) {
// make sure the format matches the other methods
Expand Down Expand Up @@ -167,6 +168,9 @@ public void postProcessParameter(final CodegenParameter parameter) {

// 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.isBodyParam){
parameter.vendorExtensions.put("x-is-body-param",true);
}
if (parameter.isFreeFormObject || parameter.isAnyType) {
parameter.vendorExtensions.put("x-marshal", true);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/twilio/oai/common/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -130,4 +131,13 @@ public Optional<CodegenModel> getModelByClassname(final List<CodegenModel> model
public Optional<CodegenModel> getModelByName(final List<CodegenModel> models, final String modelname) {
return models.stream().filter(model -> model.name.equals(modelname)).findFirst();
}

public static void resolveContentType(CodegenOperation co) {
if(co.bodyParam != null) {
LinkedHashMap conType = co.bodyParam.getContent();
if (conType != null && conType.containsKey("application/json")) {
co.vendorExtensions.put("x-is-json-type", true);
}
}
}
}
94 changes: 79 additions & 15 deletions src/main/resources/twilio-go/partial_serialization.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,112 @@ if params != nil && params.PathAccountSid != nil {
path = strings.Replace(path, "{"+"{{paramName}}"+"}", {{^isString}}fmt.Sprint({{/isString}}{{paramName}}{{^isString}}){{/isString}}, -1)
{{/pathParams}}

data := url.Values{}
headers := make(map[string]interface{})
data := url.Values{}
{{^vendorExtensions.x-is-json-type}}
headers := make(map[string]interface{})

{{/vendorExtensions.x-is-json-type}}
{{#vendorExtensions.x-is-json-type}}
headers := map[string]interface{}{
"Content-Type": "application/json",
}

{{#hasOptionalParams}}
{{#optionalParams}}
{{#vendorExtensions.x-is-body-param}}
if params != nil && params.{{paramName}} != nil {
v, err := json.Marshal(*params.{{paramName}})
if err != nil {
return nil, err
}
urlEncodedData := url.QueryEscape(string(v))
data.Set("{{baseName}}", urlEncodedData)
}
{{/vendorExtensions.x-is-body-param}}
{{^vendorExtensions.x-is-body-param}}
{{^vendorExtensions.x-is-account-sid}}
{{^isHeaderParam}}
{{^vendorExtensions.x-custom}}
if params != nil && params.{{paramName}} != nil {
if params != nil && params.{{paramName}} != nil {
{{#isArray}}
for _, item := range *params.{{paramName}} {
for _, item := range *params.{{paramName}} {
{{#items.vendorExtensions.x-marshal}}
v, err := json.Marshal(item)
v, err := json.Marshal(item)

if err != nil {
if err != nil {
return nil, err
}
}

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

if err != nil {
if err != nil {
return nil, err
}

data.Set("{{{baseName}}}", string(v))
{{/vendorExtensions.x-marshal}}
{{^vendorExtensions.x-marshal}}
data.Set("{{{baseName}}}", {{^isString}}fmt.Sprint({{/isString}}{{#isDateTime}}({{/isDateTime}}*params.{{paramName}}{{^isString}}{{#isDateTime}}).Format(time.RFC3339){{/isDateTime}}){{/isString}})
{{/vendorExtensions.x-marshal}}
{{/isArray}}
}
{{/vendorExtensions.x-custom}}
{{/isHeaderParam}}
{{/vendorExtensions.x-is-account-sid}}
{{/vendorExtensions.x-is-body-param}}
{{/optionalParams}}
{{/hasOptionalParams}}
{{/vendorExtensions.x-is-json-type}}
{{^vendorExtensions.x-is-json-type}}
{{#hasOptionalParams}}
{{#optionalParams}}
{{^vendorExtensions.x-is-account-sid}}
{{^isHeaderParam}}
{{^vendorExtensions.x-custom}}
if params != nil && params.{{paramName}} != nil {
{{#isArray}}
for _, item := range *params.{{paramName}} {
{{#items.vendorExtensions.x-marshal}}
v, err := json.Marshal(item)

if err != nil {
return nil, err
}

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

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

data.Set("{{{baseName}}}", string(v))
{{/vendorExtensions.x-marshal}}
{{^vendorExtensions.x-marshal}}
data.Set("{{{baseName}}}", {{^isString}}fmt.Sprint({{/isString}}{{#isDateTime}}({{/isDateTime}}*params.{{paramName}}{{^isString}}{{#isDateTime}}).Format(time.RFC3339){{/isDateTime}}){{/isString}})
data.Set("{{{baseName}}}", {{^isString}}fmt.Sprint({{/isString}}{{#isDateTime}}({{/isDateTime}}*params.{{paramName}}{{^isString}}{{#isDateTime}}).Format(time.RFC3339){{/isDateTime}}){{/isString}})
{{/vendorExtensions.x-marshal}}
{{/isArray}}
}
}
{{/vendorExtensions.x-custom}}
{{/isHeaderParam}}
{{/vendorExtensions.x-is-account-sid}}
{{/optionalParams}}
{{/hasOptionalParams}}
{{/vendorExtensions.x-is-json-type}}