Skip to content

Commit 402546a

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 c5e87eb commit 402546a

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
@@ -58,7 +58,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5858
|useBeanValidation|Use BeanValidation API annotations to validate data types| |true|
5959
|useFeignClientUrl|Whether to generate Feign client with url parameter.| |true|
6060
|useFlowForArrayReturnType|Whether to use Flow for array/collection return types when reactive is enabled. If false, will use List instead.| |true|
61-
|useJackson3|Use Jackson 3 dependencies (tools.jackson package). Only available with `useSpringBoot4`. Incompatible with `openApiNullable`.| |false|
61+
|useJackson3|Use Jackson 3 dependencies (tools.jackson package). Only available with `useSpringBoot4`. Defaults to true when `useSpringBoot4` is enabled. Incompatible with `openApiNullable`.| |false|
6262
|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|
6363
|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|
6464
|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
@@ -256,7 +256,7 @@ public KotlinSpringServerCodegen() {
256256
" (contexts) added to single project.", beanQualifiers);
257257
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);
258258
addSwitch(USE_SPRING_BOOT4, "Generate code and provide dependencies for use with Spring Boot 4.x. Enabling this option will also enable `useJakartaEe`.", useSpringBoot4);
259-
addSwitch(USE_JACKSON_3, "Use Jackson 3 dependencies (tools.jackson package). Only available with `useSpringBoot4`. Incompatible with `openApiNullable`.", useJackson3);
259+
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);
260260
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);
261261
addSwitch(INCLUDE_HTTP_REQUEST_CONTEXT, "Whether to include HttpServletRequest (blocking) or ServerWebExchange (reactive) as additional parameter in generated methods.", includeHttpRequestContext);
262262
addSwitch(USE_RESPONSE_ENTITY,
@@ -421,6 +421,12 @@ public String getHelp() {
421421

422422
@Override
423423
public void processOpts() {
424+
if (additionalProperties.containsKey(USE_SPRING_BOOT4)
425+
&& Boolean.parseBoolean(additionalProperties.get(USE_SPRING_BOOT4).toString())
426+
&& !additionalProperties.containsKey(USE_JACKSON_3)) {
427+
additionalProperties.put(USE_JACKSON_3, "true");
428+
}
429+
424430
super.processOpts();
425431

426432
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
@@ -4874,6 +4874,38 @@ public void shouldGenerateSpringBoot4PomWithJackson2Deps() throws IOException {
48744874
assertFileNotContains(pomPath, "tools.jackson.dataformat");
48754875
assertFileNotContains(pomPath, "tools.jackson.module");
48764876
}
4877+
4878+
@Test
4879+
public void shouldDefaultToJackson3WhenSpringBoot4Enabled() throws IOException {
4880+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
4881+
output.deleteOnExit();
4882+
String outputPath = output.getAbsolutePath().replace('\\', '/');
4883+
4884+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore.yaml");
4885+
final KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
4886+
codegen.setOpenAPI(openAPI);
4887+
codegen.setOutputDir(output.getAbsolutePath());
4888+
4889+
codegen.additionalProperties().put(KotlinSpringServerCodegen.USE_SPRING_BOOT4, "true");
4890+
// useJackson3 is NOT set — should default to true
4891+
codegen.additionalProperties().put(DOCUMENTATION_PROVIDER, DocumentationProvider.NONE.toCliOptValue());
4892+
codegen.additionalProperties().put(ANNOTATION_LIBRARY, AnnotationLibrary.NONE.toCliOptValue());
4893+
4894+
ClientOptInput input = new ClientOptInput();
4895+
input.openAPI(openAPI);
4896+
input.config(codegen);
4897+
4898+
DefaultGenerator generator = new DefaultGenerator();
4899+
generator.setGenerateMetadata(false);
4900+
generator.opts(input).generate();
4901+
4902+
Path pomPath = Paths.get(outputPath + "/pom.xml");
4903+
assertFileContains(pomPath, "tools.jackson.dataformat");
4904+
assertFileContains(pomPath, "tools.jackson.module");
4905+
assertFileNotContains(pomPath, "com.fasterxml.jackson.dataformat");
4906+
assertFileNotContains(pomPath, "com.fasterxml.jackson.module");
4907+
assertFileNotContains(pomPath, "jackson-datatype-jsr310");
4908+
}
48774909
}
48784910

48794911

0 commit comments

Comments
 (0)