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 Jul 9, 2021
1 parent e3f6a17 commit 40c5236
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/main/java/com/twilio/oai/TwilioGoGenerator.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.twilio.oai;

import io.swagger.v3.oas.models.OpenAPI;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.SupportingFile;

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.CodegenParameter;
import org.openapitools.codegen.SupportingFile;

public class TwilioGoGenerator extends AbstractTwilioGoGenerator {

@Override
Expand All @@ -20,10 +20,23 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("README.mustache", "README.md"));
}

@Override
public String toApiFilename(String name) {
// Drop the "api_" prefix.
return super.toApiFilename(name).replaceAll("^api_", "");
}

@Override
public void postProcessParameter(final CodegenParameter parameter) {
super.postProcessParameter(parameter);

// Make sure required non-path params get into the options block.
parameter.required = parameter.isPathParam;
}

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> postProcessOperationsWithModels(final Map<String, Object> objs,
final List<Object> allModels) {
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");
Expand All @@ -44,19 +57,6 @@ public Map<String, Object> postProcessOperationsWithModels(final Map<String, Obj
return results;
}

@Override
public String toApiFilename(String name) {
// Drop the "api_" prefix.
return super.toApiFilename(name).replaceAll("^api_", "");
}

@Override
public void postProcessParameter(final CodegenParameter parameter) {
super.postProcessParameter(parameter);

// Make sure required non-path params get into the options block.
parameter.required = parameter.isPathParam;
}

@Override
public void processOpenAPI(final OpenAPI openAPI) {
Expand Down

0 comments on commit 40c5236

Please sign in to comment.