Skip to content

Commit

Permalink
[Kotlin] fix 20228 - spring-kotlin insert override modifier to interf…
Browse files Browse the repository at this point in the history
…ace if it is needed (OpenAPITools#20246)
  • Loading branch information
holyjan3 authored Dec 5, 2024
1 parent 8035da8 commit f603f04
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
@get:Schema({{#example}}example = "{{{.}}}", {{/example}}{{#required}}requiredMode = Schema.RequiredMode.REQUIRED, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}description = "{{{description}}}"){{/swagger2AnnotationLibrary}}{{#swagger1AnnotationLibrary}}
@get:ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swagger1AnnotationLibrary}}{{#vendorExtensions.x-field-extra-annotation}}
{{{.}}}{{/vendorExtensions.x-field-extra-annotation}}
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInPascalCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? {{^discriminator}}= {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}{{/discriminator}}
{{#isInherited}}override {{/isInherited}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInPascalCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? {{^discriminator}}= {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}{{/discriminator}}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
@get:Schema({{#example}}example = "{{{.}}}", {{/example}}{{#required}}requiredMode = Schema.RequiredMode.REQUIRED, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}description = "{{{description}}}"){{/swagger2AnnotationLibrary}}{{#swagger1AnnotationLibrary}}
@get:ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swagger1AnnotationLibrary}}{{#vendorExtensions.x-field-extra-annotation}}
{{{.}}}{{/vendorExtensions.x-field-extra-annotation}}
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInPascalCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
{{#isInherited}}override {{/isInherited}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInPascalCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,36 @@ public void givenMultipartForm_whenGenerateReactiveServer_thenParameterAreCreate

}

@Test
public void overridePropertyFunction() throws IOException {

File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, true);

ClientOptInput input = new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/bugs/issue_20228.yaml"))
.config(codegen);
DefaultGenerator generator = new DefaultGenerator();

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

generator.opts(input).generate();

assertFileContains(
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Pony.kt"),
"override val nameOpt", "override val nameReq"
);
}

@Test
public void generateSerializableModel() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Expand Down
42 changes: 42 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_20228.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
openapi: 3.0.3
paths:
/api/kotlin-test:
post:
description: Example get
responses:
'200':
description: The request sent by the client was successful.
content:
application/json:
schema:
$ref: '#/components/schemas/Unicorn'
components:
schemas:
Animal:
type: "object"
properties:
nameOpt:
type: "string"
nameReq:
type: "string"
discriminator:
propertyName: animalType
mapping:
pegasi: "#/components/schemas/Pony"
Pony:
allOf:
- $ref: "#/components/schemas/Animal"
- type: "object"
discriminator:
propertyName: ponyType
mapping:
pegasi: "#/components/schemas/Pegasi"
unicorn: "#/components/schemas/Unicorn"
Pegasi:
allOf:
- $ref: "#/components/schemas/Pony"
- type: "object"
Unicorn:
allOf:
- $ref: "#/components/schemas/Pony"
- type: "object"

0 comments on commit f603f04

Please sign in to comment.