Skip to content

Commit

Permalink
Fix Go template for oneOfs with primitive types (#11826)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
impl committed Mar 8, 2022
1 parent 6bc50ee commit a4e1717
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}})")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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");
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a4e1717

Please sign in to comment.