Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
{{/vars}}
})
{{#isClassnameSanitized}}
{{^hasDiscriminatorWithNonEmptyMapping}}
@JsonTypeName("{{name}}")
{{/hasDiscriminatorWithNonEmptyMapping}}
{{/isClassnameSanitized}}
{{/jackson}}
{{>additionalModelTypeAnnotations}}{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
{{/discriminator}}
{{#jackson}}
{{#isClassnameSanitized}}
{{^hasDiscriminatorWithNonEmptyMapping}}
@JsonTypeName("{{name}}")
{{/hasDiscriminatorWithNonEmptyMapping}}
{{/isClassnameSanitized}}
{{/jackson}}
{{#withXml}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openapitools.codegen.java;

import com.google.common.collect.ImmutableMap;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.ArraySchema;
Expand All @@ -30,6 +31,7 @@
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
Expand Down Expand Up @@ -75,6 +77,8 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.openapitools.codegen.TestUtils.assertFileContains;
import static org.openapitools.codegen.TestUtils.assertFileNotContains;
import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -1752,4 +1756,82 @@ public void testJdkHttpClientWithAndWithoutParentExtension() throws Exception {
TestUtils.assertFileContains(Paths.get(output + "/src/main/java/xyz/abcdef/model/AnotherChild.java"),
"public class AnotherChild {");
}

@Test
public void testDiscriminatorWithMappingIssue14731() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');
OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/bugs/issue_14731.yaml", null, new ParseOptions()).getOpenAPI();

JavaClientCodegen codegen = new JavaClientCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
codegen.setUseOneOfInterfaces(true);

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "false");


codegen.setUseOneOfInterfaces(true);
codegen.setLegacyDiscriminatorBehavior(false);
codegen.setUseJakartaEe(true);
codegen.setModelNameSuffix("DTO");

generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

generator.opts(input).generate();

assertFileNotContains(Paths.get(outputPath + "/src/main/java/org/openapitools/client/model/ChildWithMappingADTO.java"), "@JsonTypeName");
assertFileNotContains(Paths.get(outputPath + "/src/main/java/org/openapitools/client/model/ChildWithMappingBDTO.java"), "@JsonTypeName");
}

@Test
public void testDiscriminatorWithoutMappingIssue14731() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');
OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/bugs/issue_14731.yaml", null, new ParseOptions()).getOpenAPI();

JavaClientCodegen codegen = new JavaClientCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
codegen.setUseOneOfInterfaces(true);

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "false");


codegen.setUseOneOfInterfaces(true);
codegen.setLegacyDiscriminatorBehavior(false);
codegen.setUseJakartaEe(true);
codegen.setModelNameSuffix("DTO");
codegen.setLibrary(JavaClientCodegen.RESTTEMPLATE);


generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

generator.opts(input).generate();

assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/client/model/ChildWithoutMappingADTO.java"), "@JsonTypeName");
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/client/model/ChildWithoutMappingBDTO.java"), "@JsonTypeName");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,85 @@ public void testOneOfAndAllOf() throws IOException {
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/PizzaSpeziale.java"), "import java.math.BigDecimal");
}

@Test
public void testDiscriminatorWithMappingIssue14731() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');
OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/bugs/issue_14731.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
codegen.setUseOneOfInterfaces(true);

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
codegen.setHateoas(true);
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "false");


codegen.setUseOneOfInterfaces(true);
codegen.setLegacyDiscriminatorBehavior(false);
codegen.setUseSpringBoot3(true);
codegen.setModelNameSuffix("DTO");

generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

generator.opts(input).generate();

assertFileNotContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/ChildWithMappingADTO.java"), "@JsonTypeName");
assertFileNotContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/ChildWithMappingBDTO.java"), "@JsonTypeName");
}

@Test
public void testDiscriminatorWithoutMappingIssue14731() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');
OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/bugs/issue_14731.yaml", null, new ParseOptions()).getOpenAPI();

SpringCodegen codegen = new SpringCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
codegen.setUseOneOfInterfaces(true);

ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
codegen.setHateoas(true);
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "false");


codegen.setUseOneOfInterfaces(true);
codegen.setLegacyDiscriminatorBehavior(false);
codegen.setUseSpringBoot3(true);
codegen.setModelNameSuffix("DTO");

generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");

generator.opts(input).generate();


assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/ChildWithoutMappingADTO.java"), "@JsonTypeName");
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/ChildWithoutMappingBDTO.java"), "@JsonTypeName");
}

@Test
public void testTypeMappings() {
final SpringCodegen codegen = new SpringCodegen();
Expand Down
109 changes: 109 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_14731.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
openapi: '3.0.0'
info:
version: '1.0.0'
title: 'FooService'
paths:
/parentWithMapping:
put:
tags:
- pet
summary: put parent
operationId: putParentWithMapping
requestBody:
description: The updated account definition to save.
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ParentWithMapping'
responses:
'200':
$ref: '#/components/responses/ParentWithMapping'
/parentWithoutMapping:
put:
tags:
- pet
summary: put parent
operationId: putParentWithoutMapping
requestBody:
description: The updated account definition to save.
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ParentWithoutMapping'
responses:
'200':
$ref: '#/components/responses/ParentWithoutMapping'
components:
schemas:
ParentWithMapping:
type: object
description: Defines an account by name.
properties:
childType:
$ref: '#/components/schemas/ChildType'
required:
- type
discriminator:
propertyName: childType
mapping:
child_a: '#/components/schemas/ChildWithMappingA'
child_b: '#/components/schemas/ChildWithMappingB'

ChildWithMappingA:
allOf:
- $ref: "#/components/schemas/ParentWithMapping"
- type: object
properties:
nameA:
type: string
ChildWithMappingB:
allOf:
- $ref: "#/components/schemas/ParentWithMapping"
- type: object
properties:
nameB:
type: string
ChildType:
type: string
x-extensible-enum:
- child_a
- child_b
ParentWithoutMapping:
type: object
description: Defines an account by name.
properties:
childType:
$ref: '#/components/schemas/ChildType'
required:
- type
discriminator:
propertyName: childType
ChildWithoutMappingA:
allOf:
- $ref: "#/components/schemas/ParentWithoutMapping"
- type: object
properties:
nameA:
type: string
ChildWithoutMappingB:
allOf:
- $ref: "#/components/schemas/ParentWithoutMapping"
- type: object
properties:
nameB:
type: string
responses:
ParentWithMapping:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ParentWithMapping'
ParentWithoutMapping:
description: The saved account definition.
content:
application/json:
schema:
$ref: '#/components/schemas/ParentWithoutMapping'
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
@JsonSubTypes.Type(value = DogDto.class, name = "Dog")
})

@JsonTypeName("Animal")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class AnimalDto {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
@JsonSubTypes.Type(value = BigCatDto.class, name = "BigCat")
})

@JsonTypeName("Cat")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class CatDto extends AnimalDto {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
@JsonSubTypes.Type(value = DogDto.class, name = "Dog")
})

@JsonTypeName("Animal")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class AnimalDto {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
@JsonSubTypes.Type(value = BigCatDto.class, name = "BigCat")
})

@JsonTypeName("Cat")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class CatDto extends AnimalDto {

Expand Down