fix: [JAVA/SPRING] [#12692] fixed optional config property legacyDisc… (#12713)

* fix: [JAVA/SPRING] [#12692] fixed optional config property legacyDiscriminatorBehavior always being overwritten to false in spring codegen

* feat: [JAVA/SPRING] [#12692] spaces instead of tabs

* feat: [JAVA/SPRING] [#12692] spaces instead of tabs in test

* fix: [JAVA/SPRING] [#12692] added comment

* fix: [JAVA/SPRING] [#12692] spaces instead of tabs

* fix: [JAVA/SPRING] [#12692] spaces instead of tabs in test
This commit is contained in:
lsalgo 2022-08-08 18:06:28 +02:00 committed by GitHub
parent d3dd676960
commit 186ad25cee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 1 deletions

View File

@ -297,10 +297,12 @@ public class SpringCodegen extends AbstractJavaCodegen
LOGGER.info("Set base package to invoker package ({})", basePackage); LOGGER.info("Set base package to invoker package ({})", basePackage);
} }
super.processOpts();
useOneOfInterfaces = true; useOneOfInterfaces = true;
legacyDiscriminatorBehavior = false; legacyDiscriminatorBehavior = false;
// Please refrain from updating values of Config Options after super.ProcessOpts() is called
super.processOpts();
if (DocumentationProvider.SPRINGFOX.equals(getDocumentationProvider())) { if (DocumentationProvider.SPRINGFOX.equals(getDocumentationProvider())) {
LOGGER.warn("The springfox documentation provider is deprecated for removal. Use the springdoc provider instead."); LOGGER.warn("The springfox documentation provider is deprecated for removal. Use the springdoc provider instead.");
} }

View File

@ -1473,4 +1473,30 @@ public class SpringCodegenTest {
"consumes", "{ \"application/octet-stream\", \"application/*\" }" "consumes", "{ \"application/octet-stream\", \"application/*\" }"
)); ));
} }
@Test
public void shouldGenerateDiscriminatorFromAllOfWhenUsingLegacyDiscriminatorBehaviour_issue12692() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/bugs/issue_12692.yml", null, new ParseOptions()).getOpenAPI();
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_BOOT);
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "true");
ClientOptInput input = new ClientOptInput()
.openAPI(openAPI)
.config(codegen);
DefaultGenerator generator = new DefaultGenerator();
generator.opts(input).generate();
String jsonTypeInfo = "@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = \"type\", visible = true)";
String jsonSubType = "@JsonSubTypes({\n" +
" @JsonSubTypes.Type(value = Cat.class, name = \"cat\")" +
"})";
assertFileContains(Paths.get(output.getAbsolutePath() + "/src/main/java/org/openapitools/model/Pet.java"), jsonTypeInfo, jsonSubType);
}
} }

View File

@ -0,0 +1,45 @@
openapi: 3.0.1
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
paths:
"/pets":
get:
operationId: listPets
responses:
'200':
description: description
content:
application/json:
schema:
type: array
items:
"$ref": "#/components/schemas/Pet"
components:
schemas:
Pet:
type: object
required:
- type
properties:
type:
type: string
discriminator:
propertyName: type
mapping:
cat: "#/components/schemas/Cat"
Cat:
allOf:
- "$ref": "#/components/schemas/Pet"
- type: object
required:
- type
properties:
type:
type: string
discriminator:
propertyName: type
mapping:
cat: "#/components/schemas/Cat"