From a4e1717fd20bd014c9ff488842752a5fe1250ceb Mon Sep 17 00:00:00 2001 From: Noah Fontes Date: Tue, 8 Mar 2022 09:56:53 -0800 Subject: [PATCH] Fix Go template for oneOfs with primitive types (#11826) A recent enhancement to the template made these primitive types usable as property names, but a small section of the template wasn't updated, leading to compilation problems. --- .../main/resources/go/model_oneof.mustache | 12 +++---- .../codegen/go/GoClientCodegenTest.java | 32 +++++++++++++++++++ .../test/resources/3_0/oneOf_primitive.yaml | 31 ++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/oneOf_primitive.yaml diff --git a/modules/openapi-generator/src/main/resources/go/model_oneof.mustache b/modules/openapi-generator/src/main/resources/go/model_oneof.mustache index dc85c438f100..6722492abda5 100644 --- a/modules/openapi-generator/src/main/resources/go/model_oneof.mustache +++ b/modules/openapi-generator/src/main/resources/go/model_oneof.mustache @@ -55,24 +55,24 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error { {{^discriminator}} match := 0 {{#oneOf}} - // try to unmarshal data into {{{.}}} - err = json.Unmarshal(data, &dst.{{{.}}}) + // try to unmarshal data into {{#lambda.titlecase}}{{{.}}}{{/lambda.titlecase}} + err = json.Unmarshal(data, &dst.{{#lambda.titlecase}}{{{.}}}{{/lambda.titlecase}}) if err == nil { - json{{{.}}}, _ := json.Marshal(dst.{{{.}}}) + json{{{.}}}, _ := json.Marshal(dst.{{#lambda.titlecase}}{{{.}}}{{/lambda.titlecase}}) if string(json{{{.}}}) == "{}" { // empty struct - dst.{{{.}}} = nil + dst.{{#lambda.titlecase}}{{{.}}}{{/lambda.titlecase}} = nil } else { match++ } } else { - dst.{{{.}}} = nil + dst.{{#lambda.titlecase}}{{{.}}}{{/lambda.titlecase}} = nil } {{/oneOf}} if match > 1 { // more than 1 match // reset to nil {{#oneOf}} - dst.{{{.}}} = nil + dst.{{#lambda.titlecase}}{{{.}}}{{/lambda.titlecase}} = nil {{/oneOf}} return fmt.Errorf("Data matches more than one schema in oneOf({{classname}})") diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java index ca5271746d47..2412d5fdbb8b 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java @@ -19,10 +19,20 @@ import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.CodegenParameter; +import org.openapitools.codegen.DefaultGenerator; import org.openapitools.codegen.TestUtils; +import org.openapitools.codegen.config.CodegenConfigurator; import org.openapitools.codegen.languages.GoClientCodegen; import org.testng.Assert; import org.testng.annotations.Test; @@ -109,4 +119,26 @@ public void testFilenames() throws Exception { Assert.assertEquals(codegen.toApiFilename("Animal Farm Test"), "api_animal_farm_test_"); } + @Test + public void testPrimitiveTypeInOneOf() throws IOException { + File output = Files.createTempDirectory("test").toFile(); + output.deleteOnExit(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("go") + .setInputSpec("src/test/resources/3_0/oneOf_primitive.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + DefaultGenerator generator = new DefaultGenerator(); + List files = generator.opts(configurator.toClientOptInput()).generate(); + System.out.println(files); + files.forEach(File::deleteOnExit); + + Path modelFile = Paths.get(output + "/model_example.go"); + TestUtils.assertFileContains(modelFile, "Child *Child"); + TestUtils.assertFileContains(modelFile, "Int32 *int32"); + TestUtils.assertFileContains(modelFile, "dst.Int32"); + TestUtils.assertFileNotContains(modelFile, "int32 *int32"); + TestUtils.assertFileNotContains(modelFile, "dst.int32"); + } } diff --git a/modules/openapi-generator/src/test/resources/3_0/oneOf_primitive.yaml b/modules/openapi-generator/src/test/resources/3_0/oneOf_primitive.yaml new file mode 100644 index 000000000000..9f60dbe79d2e --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/oneOf_primitive.yaml @@ -0,0 +1,31 @@ +openapi: 3.0.1 +info: + version: 1.0.0 + title: Example + license: + name: MIT +servers: + - url: http://api.example.xyz/v1 +paths: + /example: + get: + operationId: list + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/Example" +components: + schemas: + Child: + type: object + properties: + name: + type: string + Example: + oneOf: + - $ref: '#/components/schemas/Child' + - type: integer + format: int32