Skip to content

Commit 73de774

Browse files
[kotlin] implement serializable data classes (#8317)
1 parent 81f576c commit 73de774

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public KotlinSpringServerCodegen() {
208208
" (contexts) added to single project.", beanQualifiers);
209209
addSwitch(USE_SPRING_BOOT3, "Generate code and provide dependencies for use with Spring Boot 3.x. (Use jakarta instead of javax in imports). Enabling this option will also enable `useJakartaEe`.", useSpringBoot3);
210210
addSwitch(APPEND_REQUEST_TO_HANDLER, "Append ServerHttpRequest to handler method for getting request stuff", false);
211+
addSwitch(CodegenConstants.SERIALIZABLE_MODEL, "toggle \"implements Serializable\" for generated models", null);
211212
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
212213
supportedLibraries.put(SPRING_CLOUD_LIBRARY,
213214
"Spring-Cloud-Feign client with Spring-Boot auto-configured settings.");

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
1515
{{/hasOptional}}{{/hasRequired}}{{#optionalVars}}{{>dataClassOptVar}}{{^-last}},
1616
{{/-last}}{{/optionalVars}}
17-
) {{/discriminator}}{{#parent}}: {{{.}}}{{/parent}}{
17+
) {{/discriminator}}{{#parent}}: {{{.}}}{{#serializableModel}}, Serializable{{/serializableModel}}{{/parent}}{{#serializableModel}}: Serializable{{/serializableModel}} {
1818
{{#discriminator}}
1919
{{#requiredVars}}
2020
{{>interfaceReqVar}}
@@ -33,4 +33,9 @@
3333
@JsonProperty({{{value}}}) {{{name}}}({{{value}}}){{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
3434
}
3535
{{/isEnum}}{{/vars}}{{/hasEnums}}
36+
{{#serializableModel}}
37+
companion object {
38+
private const val serialVersionUID: kotlin.Long = 1
39+
}
40+
{{/serializableModel}}
3641
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package {{package}}
33
import java.util.Objects
44
{{#imports}}import {{import}}
55
{{/imports}}
6+
{{#serializableModel}}
7+
import java.io.Serializable
8+
{{/serializableModel}}
69
{{#useBeanValidation}}
710
import {{javaxPackage}}.validation.constraints.DecimalMax
811
import {{javaxPackage}}.validation.constraints.DecimalMin

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public void testSettersForConfigValues() throws Exception {
115115
codegen.setServiceImplementation(true);
116116
codegen.setUseBeanValidation(false);
117117
codegen.setReactive(false);
118+
codegen.setSerializableModel(true);
118119
codegen.processOpts();
119120

120121
Assert.assertEquals(codegen.modelPackage(), "xx.yyyyyyyy.model");
@@ -137,6 +138,8 @@ public void testSettersForConfigValues() throws Exception {
137138
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.USE_BEANVALIDATION), false);
138139
Assert.assertFalse(codegen.isReactive());
139140
Assert.assertEquals(codegen.additionalProperties().get(KotlinSpringServerCodegen.REACTIVE), false);
141+
Assert.assertTrue(codegen.isSerializableModel());
142+
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.SERIALIZABLE_MODEL), true);
140143
}
141144

142145
@Test
@@ -678,4 +681,35 @@ public void givenMultipartForm_whenGenerateReactiveServer_thenParameterAreCreate
678681
"@Parameter(description = \"image to upload\") @Valid @RequestPart(\"image\", required = false) image: org.springframework.core.io.Resource?");
679682

680683
}
681-
}
684+
685+
@Test
686+
public void generateSerializableModel() throws Exception {
687+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
688+
output.deleteOnExit();
689+
String outputPath = output.getAbsolutePath().replace('\\', '/');
690+
691+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
692+
codegen.setOutputDir(output.getAbsolutePath());
693+
codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, true);
694+
695+
ClientOptInput input = new ClientOptInput()
696+
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/petstore.yaml"))
697+
.config(codegen);
698+
DefaultGenerator generator = new DefaultGenerator();
699+
700+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
701+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
702+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
703+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "false");
704+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
705+
706+
generator.opts(input).generate();
707+
708+
assertFileContains(
709+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Pet.kt"),
710+
"import java.io.Serializable",
711+
") : Serializable {",
712+
"private const val serialVersionUID: kotlin.Long = 1"
713+
);
714+
}
715+
}

0 commit comments

Comments
 (0)