Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Go] Fix template for oneOfs with primitive types #11826

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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