Skip to content

Commit 319c257

Browse files
committed
feat(kotlin-spring): default to Jackson 3 when Spring Boot 4 is enabled
Spring Boot 4 ships with Jackson 3 out of the box, so useJackson3 now defaults to true when useSpringBoot4 is enabled and the user hasn't explicitly set useJackson3.
1 parent 43e3fae commit 319c257

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

docs/generators/kotlin-spring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5959
|useBeanValidation|Use BeanValidation API annotations to validate data types| |true|
6060
|useFeignClientUrl|Whether to generate Feign client with url parameter.| |true|
6161
|useFlowForArrayReturnType|Whether to use Flow for array/collection return types when reactive is enabled. If false, will use List instead.| |true|
62-
|useJackson3|Use Jackson 3 dependencies (tools.jackson package). Only available with `useSpringBoot4`. Incompatible with `openApiNullable`.| |false|
62+
|useJackson3|Use Jackson 3 dependencies (tools.jackson package). Only available with `useSpringBoot4`. Defaults to true when `useSpringBoot4` is enabled. Incompatible with `openApiNullable`.| |false|
6363
|useResponseEntity|Whether (when false) to return actual type (e.g. List<Fruit>) and handle non-happy path responses via exceptions flow or (when true) return entire ResponseEntity (e.g. ResponseEntity<List<Fruit>>). If disabled, method are annotated using a @ResponseStatus annotation, which has the status of the first response declared in the Api definition| |true|
6464
|useSealedResponseInterfaces|Generate sealed interfaces for endpoint responses that all possible response types implement. Allows controllers to return any valid response type in a type-safe manner (e.g., sealed interface CreateUserResponse implemented by User, ConflictResponse, ErrorResponse)| |false|
6565
|useSpringBoot3|Generate code and provide dependencies for use with Spring Boot ≥ 3 (use jakarta instead of javax in imports). Enabling this option will also enable `useJakartaEe`.| |false|

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public KotlinSpringServerCodegen() {
258258
" (contexts) added to single project.", beanQualifiers);
259259
addSwitch(USE_SPRING_BOOT3, "Generate code and provide dependencies for use with Spring Boot ≥ 3 (use jakarta instead of javax in imports). Enabling this option will also enable `useJakartaEe`.", useSpringBoot3);
260260
addSwitch(USE_SPRING_BOOT4, "Generate code and provide dependencies for use with Spring Boot 4.x. Enabling this option will also enable `useJakartaEe`.", useSpringBoot4);
261-
addSwitch(USE_JACKSON_3, "Use Jackson 3 dependencies (tools.jackson package). Only available with `useSpringBoot4`. Incompatible with `openApiNullable`.", useJackson3);
261+
addSwitch(USE_JACKSON_3, "Use Jackson 3 dependencies (tools.jackson package). Only available with `useSpringBoot4`. Defaults to true when `useSpringBoot4` is enabled. Incompatible with `openApiNullable`.", useJackson3);
262262
addSwitch(USE_FLOW_FOR_ARRAY_RETURN_TYPE, "Whether to use Flow for array/collection return types when reactive is enabled. If false, will use List instead.", useFlowForArrayReturnType);
263263
addSwitch(INCLUDE_HTTP_REQUEST_CONTEXT, "Whether to include HttpServletRequest (blocking) or ServerWebExchange (reactive) as additional parameter in generated methods.", includeHttpRequestContext);
264264
addSwitch(USE_RESPONSE_ENTITY,
@@ -424,6 +424,12 @@ public String getHelp() {
424424

425425
@Override
426426
public void processOpts() {
427+
if (additionalProperties.containsKey(USE_SPRING_BOOT4)
428+
&& Boolean.parseBoolean(additionalProperties.get(USE_SPRING_BOOT4).toString())
429+
&& !additionalProperties.containsKey(USE_JACKSON_3)) {
430+
additionalProperties.put(USE_JACKSON_3, "true");
431+
}
432+
427433
super.processOpts();
428434

429435
if (DocumentationProvider.SPRINGFOX.equals(getDocumentationProvider())) {

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4903,6 +4903,38 @@ public void shouldGenerateSpringBoot4PomWithJackson2Deps() throws IOException {
49034903
assertFileNotContains(pomPath, "tools.jackson.dataformat");
49044904
assertFileNotContains(pomPath, "tools.jackson.module");
49054905
}
4906+
4907+
@Test
4908+
public void shouldDefaultToJackson3WhenSpringBoot4Enabled() throws IOException {
4909+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
4910+
output.deleteOnExit();
4911+
String outputPath = output.getAbsolutePath().replace('\\', '/');
4912+
4913+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore.yaml");
4914+
final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
4915+
codegen.setOpenAPI(openAPI);
4916+
codegen.setOutputDir(output.getAbsolutePath());
4917+
4918+
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_SPRING_BOOT4, "true");
4919+
// useJackson3 is NOT set — should default to true
4920+
codegen.additionalProperties().put(DOCUMENTATION_PROVIDER, DocumentationProvider.NONE.toCliOptValue());
4921+
codegen.additionalProperties().put(ANNOTATION_LIBRARY, AnnotationLibrary.NONE.toCliOptValue());
4922+
4923+
ClientOptInput input = new ClientOptInput();
4924+
input.openAPI(openAPI);
4925+
input.config(codegen);
4926+
4927+
DefaultGenerator generator = new DefaultGenerator();
4928+
generator.setGenerateMetadata(false);
4929+
generator.opts(input).generate();
4930+
4931+
Path pomPath = Paths.get(outputPath + "/pom.xml");
4932+
assertFileContains(pomPath, "tools.jackson.dataformat");
4933+
assertFileContains(pomPath, "tools.jackson.module");
4934+
assertFileNotContains(pomPath, "com.fasterxml.jackson.dataformat");
4935+
assertFileNotContains(pomPath, "com.fasterxml.jackson.module");
4936+
assertFileNotContains(pomPath, "jackson-datatype-jsr310");
4937+
}
49064938
}
49074939

49084940

0 commit comments

Comments
 (0)