Skip to content

Commit f603f04

Browse files
authored
[Kotlin] fix 20228 - spring-kotlin insert override modifier to interface if it is needed (OpenAPITools#20246)
1 parent 8035da8 commit f603f04

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

modules/openapi-generator/src/main/resources/kotlin-spring/interfaceOptVar.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
@get:Schema({{#example}}example = "{{{.}}}", {{/example}}{{#required}}requiredMode = Schema.RequiredMode.REQUIRED, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}description = "{{{description}}}"){{/swagger2AnnotationLibrary}}{{#swagger1AnnotationLibrary}}
33
@get:ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swagger1AnnotationLibrary}}{{#vendorExtensions.x-field-extra-annotation}}
44
{{{.}}}{{/vendorExtensions.x-field-extra-annotation}}
5-
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInPascalCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? {{^discriminator}}= {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}{{/discriminator}}
5+
{{#isInherited}}override {{/isInherited}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInPascalCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? {{^discriminator}}= {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}{{/discriminator}}

modules/openapi-generator/src/main/resources/kotlin-spring/interfaceReqVar.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
@get:Schema({{#example}}example = "{{{.}}}", {{/example}}{{#required}}requiredMode = Schema.RequiredMode.REQUIRED, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}description = "{{{description}}}"){{/swagger2AnnotationLibrary}}{{#swagger1AnnotationLibrary}}
33
@get:ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swagger1AnnotationLibrary}}{{#vendorExtensions.x-field-extra-annotation}}
44
{{{.}}}{{/vendorExtensions.x-field-extra-annotation}}
5-
{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInPascalCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}
5+
{{#isInherited}}override {{/isInherited}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{classname}}.{{nameInPascalCase}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,36 @@ public void givenMultipartForm_whenGenerateReactiveServer_thenParameterAreCreate
851851

852852
}
853853

854+
@Test
855+
public void overridePropertyFunction() throws IOException {
856+
857+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
858+
output.deleteOnExit();
859+
String outputPath = output.getAbsolutePath().replace('\\', '/');
860+
861+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
862+
codegen.setOutputDir(output.getAbsolutePath());
863+
codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, true);
864+
865+
ClientOptInput input = new ClientOptInput()
866+
.openAPI(TestUtils.parseSpec("src/test/resources/bugs/issue_20228.yaml"))
867+
.config(codegen);
868+
DefaultGenerator generator = new DefaultGenerator();
869+
870+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
871+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
872+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
873+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
874+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
875+
876+
generator.opts(input).generate();
877+
878+
assertFileContains(
879+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Pony.kt"),
880+
"override val nameOpt", "override val nameReq"
881+
);
882+
}
883+
854884
@Test
855885
public void generateSerializableModel() throws Exception {
856886
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
openapi: 3.0.3
2+
paths:
3+
/api/kotlin-test:
4+
post:
5+
description: Example get
6+
responses:
7+
'200':
8+
description: The request sent by the client was successful.
9+
content:
10+
application/json:
11+
schema:
12+
$ref: '#/components/schemas/Unicorn'
13+
components:
14+
schemas:
15+
Animal:
16+
type: "object"
17+
properties:
18+
nameOpt:
19+
type: "string"
20+
nameReq:
21+
type: "string"
22+
discriminator:
23+
propertyName: animalType
24+
mapping:
25+
pegasi: "#/components/schemas/Pony"
26+
Pony:
27+
allOf:
28+
- $ref: "#/components/schemas/Animal"
29+
- type: "object"
30+
discriminator:
31+
propertyName: ponyType
32+
mapping:
33+
pegasi: "#/components/schemas/Pegasi"
34+
unicorn: "#/components/schemas/Unicorn"
35+
Pegasi:
36+
allOf:
37+
- $ref: "#/components/schemas/Pony"
38+
- type: "object"
39+
Unicorn:
40+
allOf:
41+
- $ref: "#/components/schemas/Pony"
42+
- type: "object"

0 commit comments

Comments
 (0)