From 40c5236a3b46ffdd1c9b0b6bc84124c54e78fff2 Mon Sep 17 00:00:00 2001 From: RJPearson94 Date: Thu, 10 Jun 2021 21:57:47 +0100 Subject: [PATCH] feat: update the `api.mustache` template and go generator to support 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 https://github.com/twilio/twilio-oai-generator/issues/49 however once this PR is merged https://github.com/twilio/twilio-oai/issues/36 will need to be finished to fully resolve the issue This change relies on https://github.com/twilio/twilio-go/pull/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 --- .../com/twilio/oai/TwilioGoGenerator.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/twilio/oai/TwilioGoGenerator.java b/src/main/java/com/twilio/oai/TwilioGoGenerator.java index 873e69af3..c3a1706bb 100644 --- a/src/main/java/com/twilio/oai/TwilioGoGenerator.java +++ b/src/main/java/com/twilio/oai/TwilioGoGenerator.java @@ -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 @@ -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 postProcessOperationsWithModels(final Map objs, - final List allModels) { + public Map postProcessOperationsWithModels(final Map objs, final List allModels) { final Map results = super.postProcessOperationsWithModels(objs, allModels); final Map ops = (Map) results.get("operations"); @@ -44,19 +57,6 @@ public Map postProcessOperationsWithModels(final Map