Skip to content

Commit

Permalink
feat: update the api.mustache template and go generator to support …
Browse files Browse the repository at this point in the history
…JSON media type for the request body

The Go Generator has been updated to add a vendor extension to set `x-is-json` to true when the consume media type is `application/json` and `x-is-form` when the consumes media type is `application/x-www-form-urlencoded`

The `x-is-json` vendor extension is used to generate the code to handle calling the new PostJson function or the corresponding HTTP method function on the request handler. This is designed to only work with Post requests at the moment but could be extended in the future if needed.

This PR aids in resolving twilio#49 however once this PR is merged twilio/twilio-oai#36 will need to be finished to fully resolve the issue

This change relies on twilio/twilio-go#83 and the tests cannot be updated until this PR is merged and released

This change also fixes an issue with JSON struct tags for the Params structs being `html-escaped`. I have disabled the escaping by using `{{{}}}` as this was highlighted during linting the Go SDK repo
  • Loading branch information
RJPearson94 committed Jun 10, 2021
1 parent 3ed5c3a commit ef06419
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
35 changes: 32 additions & 3 deletions src/main/java/com/twilio/oai/TwilioGoGenerator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.twilio.oai;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.SupportingFile;

public class TwilioGoGenerator extends AbstractTwilioGoGenerator {
Expand All @@ -13,9 +17,33 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("README.mustache", "README.md"));
}

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> postProcessOperationsWithModels(final Map<String, Object> objs,
final List<Object> allModels) {
final Map<String, Object> results = super.postProcessOperationsWithModels(objs, allModels);

final Map<String, Object> ops = (Map<String, Object>) results.get("operations");
final List<CodegenOperation> opList = (List<CodegenOperation>) ops.get("operation");

for (final CodegenOperation co : opList) {
Optional.ofNullable(co.consumes).orElse(Collections.emptyList()).stream().forEach(consumes -> {
final String mediaType = consumes.get("mediaType");
if ("application/json".equals(mediaType)) {
co.vendorExtensions.put("x-is-json", true);
}
if ("application/x-www-form-urlencoded".equals(mediaType)) {
co.vendorExtensions.put("x-is-form", true);
}
});
}

return results;
}

/**
* Configures a friendly name for the generator. This will be used by the generator to select the library with the
* -g flag.
* Configures a friendly name for the generator. This will be used by the
* generator to select the library with the -g flag.
*
* @return the friendly name for the generator
*/
Expand All @@ -25,7 +53,8 @@ public String getName() {
}

/**
* Returns human-friendly help for the generator. Provide the consumer with help tips, parameters here
* Returns human-friendly help for the generator. Provide the consumer with help
* tips, parameters here
*
* @return A string value for the help message
*/
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 @@ -36,7 +36,7 @@ func New{{classname}}ServiceWithClient(client twilio.BaseClient) *{{classname}}S
type {{{nickname}}}Params struct {
{{#optionalParams}}
// {{{description}}}
{{paramName}} *{{{dataType}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}}{{/vendorExtensions.x-go-custom-tag}}`
{{paramName}} *{{{dataType}}} `json:"{{{baseName}}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}}{{/vendorExtensions.x-go-custom-tag}}`
{{/optionalParams}}
}

Expand Down Expand Up @@ -66,9 +66,21 @@ func (c *{{{classname}}}Service) {{{nickname}}}({{#allParams}}{{#required}}{{par
{{/vendorExtensions.x-is-account-sid}}
{{/pathParams}}

{{#vendorExtensions.x-is-json}}
{{#bodyParam.baseName}}
var data {{{bodyParam.baseName}}}
if params.{{{bodyParam.baseName}}} != nil {
data = *params.{{{bodyParam.baseName}}}
} else {
data = {{{bodyParam.baseName}}}{}
}
{{/bodyParam.baseName}}
{{^bodyParam.baseName}}
data := make(map[string]interface{})
{{/bodyParam.baseName}}
{{/vendorExtensions.x-is-json}}
{{^vendorExtensions.x-is-json}}
data := url.Values{}
headers := make(map[string]interface{})

{{#hasOptionalParams}}
{{#optionalParams}}
{{^vendorExtensions.x-is-account-sid}}
Expand Down Expand Up @@ -98,7 +110,8 @@ func (c *{{{classname}}}Service) {{{nickname}}}({{#allParams}}{{#required}}{{par
{{/vendorExtensions.x-is-account-sid}}
{{/optionalParams}}
{{/hasOptionalParams}}

{{/vendorExtensions.x-is-json}}
headers := make(map[string]interface{})
{{#hasHeaderParams}}
{{#headerParams}}
if params != nil && params.{{paramName}} != nil {
Expand All @@ -107,7 +120,7 @@ func (c *{{{classname}}}Service) {{{nickname}}}({{#allParams}}{{#required}}{{par
{{/headerParams}}
{{/hasHeaderParams}}

resp, err := c.requestHandler.{{httpMethod}}(c.baseURL+path, data, headers)
resp, err := c.requestHandler.{{httpMethod}}{{#vendorExtensions.x-is-json}}Json{{/vendorExtensions.x-is-json}}(c.baseURL+path, data, headers)
{{#returnType}}
if err != nil {
return nil, err
Expand Down

0 comments on commit ef06419

Please sign in to comment.