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.
This commit is contained in:
Noah Fontes
2022-03-08 09:56:53 -08:00
committed by GitHub
parent 6bc50ee57f
commit a4e1717fd2
3 changed files with 69 additions and 6 deletions

View File

@@ -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}})")

View File

@@ -19,10 +19,20 @@ package org.openapitools.codegen.go;
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 class GoClientCodegenTest {
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<File> 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");
}
}

View File

@@ -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