Skip to content

Commit 80d4c32

Browse files
authored
fix(kotlin-spring): use correct Jackson 3 property path for WRITE_DATES_AS_TIMESTAMPS (#23384)
* fix(kotlin-spring): use correct Jackson 3 property path for WRITE_DATES_AS_TIMESTAMPS When using Spring Boot 4 with Jackson 3, the property path for WRITE_DATES_AS_TIMESTAMPS changed from: spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS to: spring.jackson.datatype.datetime.WRITE_DATES_AS_TIMESTAMPS This fix adds conditional logic to the kotlin-spring generator template to use the correct property path based on useSpringBoot4 flag, matching the behavior already implemented in the JavaSpring generator. Fixes binding error: 'Failed to bind properties under spring.jackson.serialization to java.util.Map<tools.jackson.databind.SerializationFeature, java.lang.Boolean>: No enum constant tools.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS' * test(kotlin-spring): add tests for Jackson datetime property path Add tests to verify that: - Spring Boot 4 generates application.yaml with spring.jackson.datatype.datetime.WRITE_DATES_AS_TIMESTAMPS - Spring Boot 3 generates application.yaml with spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS
1 parent e1c4fdb commit 80d4c32

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

modules/openapi-generator/src/main/resources/kotlin-spring/libraries/spring-boot/application.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ spring:
33
name: {{title}}
44

55
jackson:
6+
{{#useSpringBoot4}}
7+
datatype:
8+
datetime:
9+
WRITE_DATES_AS_TIMESTAMPS: false
10+
{{/useSpringBoot4}}
11+
{{^useSpringBoot4}}
612
serialization:
713
WRITE_DATES_AS_TIMESTAMPS: false
14+
{{/useSpringBoot4}}
815

916
server:
1017
port: {{serverPort}}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,53 @@ public void useSpringBoot3() throws Exception {
676676
);
677677
}
678678

679+
@Test(description = "Spring Boot 4 should use Jackson 3 datetime property path")
680+
public void useSpringBoot4JacksonDateTimeProperty() throws Exception {
681+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
682+
output.deleteOnExit();
683+
String outputPath = output.getAbsolutePath().replace('\\', '/');
684+
685+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
686+
codegen.setOutputDir(output.getAbsolutePath());
687+
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_SPRING_BOOT4, true);
688+
689+
new DefaultGenerator().opts(new ClientOptInput()
690+
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/petstore.yaml"))
691+
.config(codegen))
692+
.generate();
693+
694+
// Spring Boot 4 uses Jackson 3, which moved WRITE_DATES_AS_TIMESTAMPS to
695+
// spring.jackson.datatype.datetime instead of spring.jackson.serialization
696+
Path applicationYaml = Paths.get(outputPath + "/src/main/resources/application.yaml");
697+
assertFileContains(applicationYaml, "datatype:");
698+
assertFileContains(applicationYaml, "datetime:");
699+
assertFileContains(applicationYaml, "WRITE_DATES_AS_TIMESTAMPS: false");
700+
assertFileNotContains(applicationYaml, "serialization:");
701+
}
702+
703+
@Test(description = "Spring Boot 3 should use Jackson 2 serialization property path")
704+
public void useSpringBoot3JacksonSerializationProperty() throws Exception {
705+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
706+
output.deleteOnExit();
707+
String outputPath = output.getAbsolutePath().replace('\\', '/');
708+
709+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
710+
codegen.setOutputDir(output.getAbsolutePath());
711+
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_SPRING_BOOT3, true);
712+
713+
new DefaultGenerator().opts(new ClientOptInput()
714+
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/petstore.yaml"))
715+
.config(codegen))
716+
.generate();
717+
718+
// Spring Boot 3 uses Jackson 2, which has WRITE_DATES_AS_TIMESTAMPS under
719+
// spring.jackson.serialization
720+
Path applicationYaml = Paths.get(outputPath + "/src/main/resources/application.yaml");
721+
assertFileContains(applicationYaml, "serialization:");
722+
assertFileContains(applicationYaml, "WRITE_DATES_AS_TIMESTAMPS: false");
723+
assertFileNotContains(applicationYaml, "datatype:");
724+
}
725+
679726
@Test(description = "multi-line descriptions should be supported for operations")
680727
public void multiLineOperationDescription() throws IOException {
681728
testMultiLineOperationDescription(false);

samples/server/petstore/kotlin-springboot-4/src/main/resources/application.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ spring:
33
name: openAPIPetstore
44

55
jackson:
6-
serialization:
7-
WRITE_DATES_AS_TIMESTAMPS: false
6+
datatype:
7+
datetime:
8+
WRITE_DATES_AS_TIMESTAMPS: false
89

910
server:
1011
port: 8080

0 commit comments

Comments
 (0)