Skip to content

Commit 428be16

Browse files
author
Benjamin Einaudi
committed
feature(jackson3) add jackson3 support for spring generator
* add 'useJackson3' option * add 'useSpringBoot4' option * add support for RestClient in spring-http-interfaces See #22294
1 parent 11e06d1 commit 428be16

File tree

35 files changed

+790
-92
lines changed

35 files changed

+790
-92
lines changed

docs/generators/java-camel.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,14 @@ These options may be applied as additional-properties (cli) or configOptions (pl
105105
|useEnumCaseInsensitive|Use `equalsIgnoreCase` when String for enum comparison| |false|
106106
|useFeignClientContextId|Whether to generate Feign client with contextId parameter.| |true|
107107
|useFeignClientUrl|Whether to generate Feign client with url parameter.| |true|
108+
|useJackson3|Set it in order to use jackson 3 dependencies (only allowed when `useSpringBoot4` is set and incompatible with `openApiNullable`).| |false|
108109
|useJakartaEe|whether to use Jakarta EE namespace instead of javax| |false|
109110
|useOneOfInterfaces|whether to use a java interface to describe a set of oneOf options, where each option is a class that implements the interface| |true|
110111
|useOptional|Use Optional container for optional parameters| |false|
111112
|useResponseEntity|Use the `ResponseEntity` type to wrap return values of generated API methods. If disabled, method are annotated using a `@ResponseStatus` annotation, which has the status of the first response declared in the Api definition| |true|
112113
|useSealed|Whether to generate sealed model interfaces and classes| |false|
113114
|useSpringBoot3|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`.| |false|
115+
|useSpringBoot4|Generate code and provide dependencies for use with Spring Boot 4.x. (Use jakarta instead of javax in imports). Enabling this option will also enable `useJakartaEe`.| |false|
114116
|useSpringBuiltInValidation|Disable `@Validated` at the class level when using built-in validation.| |false|
115117
|useSpringController|Annotate the generated API as a Spring Controller| |false|
116118
|useSwaggerUI|Open the OpenApi specification in swagger-ui. Will also import and configure needed dependencies| |true|

docs/generators/spring.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ These options may be applied as additional-properties (cli) or configOptions (pl
9898
|useEnumCaseInsensitive|Use `equalsIgnoreCase` when String for enum comparison| |false|
9999
|useFeignClientContextId|Whether to generate Feign client with contextId parameter.| |true|
100100
|useFeignClientUrl|Whether to generate Feign client with url parameter.| |true|
101+
|useJackson3|Set it in order to use jackson 3 dependencies (only allowed when `useSpringBoot4` is set and incompatible with `openApiNullable`).| |false|
101102
|useJakartaEe|whether to use Jakarta EE namespace instead of javax| |false|
102103
|useOneOfInterfaces|whether to use a java interface to describe a set of oneOf options, where each option is a class that implements the interface| |true|
103104
|useOptional|Use Optional container for optional parameters| |false|
104105
|useResponseEntity|Use the `ResponseEntity` type to wrap return values of generated API methods. If disabled, method are annotated using a `@ResponseStatus` annotation, which has the status of the first response declared in the Api definition| |true|
105106
|useSealed|Whether to generate sealed model interfaces and classes| |false|
106107
|useSpringBoot3|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`.| |false|
108+
|useSpringBoot4|Generate code and provide dependencies for use with Spring Boot 4.x. (Use jakarta instead of javax in imports). Enabling this option will also enable `useJakartaEe`.| |false|
107109
|useSpringBuiltInValidation|Disable `@Validated` at the class level when using built-in validation.| |false|
108110
|useSpringController|Annotate the generated API as a Spring Controller| |false|
109111
|useSwaggerUI|Open the OpenApi specification in swagger-ui. Will also import and configure needed dependencies| |true|

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public class SpringCodegen extends AbstractJavaCodegen
9393
public static final String GENERATE_GENERIC_RESPONSE_ENTITY = "generateGenericResponseEntity";
9494
public static final String USE_ENUM_CASE_INSENSITIVE = "useEnumCaseInsensitive";
9595
public static final String USE_SPRING_BOOT3 = "useSpringBoot3";
96+
public static final String USE_SPRING_BOOT4 = "useSpringBoot4";
9697
public static final String REQUEST_MAPPING_OPTION = "requestMappingMode";
9798
public static final String USE_REQUEST_MAPPING_ON_CONTROLLER = "useRequestMappingOnController";
9899
public static final String USE_REQUEST_MAPPING_ON_INTERFACE = "useRequestMappingOnInterface";
@@ -101,6 +102,7 @@ public class SpringCodegen extends AbstractJavaCodegen
101102
public static final String USE_SPRING_BUILT_IN_VALIDATION = "useSpringBuiltInValidation";
102103
public static final String USE_DEDUCTION_FOR_ONE_OF_INTERFACES = "useDeductionForOneOfInterfaces";
103104
public static final String SPRING_API_VERSION = "springApiVersion";
105+
public static final String USE_JACKSON_3 = "useJackson3";
104106

105107
@Getter
106108
public enum RequestMappingMode {
@@ -154,6 +156,8 @@ public enum RequestMappingMode {
154156
@Setter protected boolean useEnumCaseInsensitive = false;
155157
@Getter @Setter
156158
protected boolean useSpringBoot3 = false;
159+
@Getter @Setter
160+
protected boolean useSpringBoot4 = false;
157161
protected boolean generatedConstructorWithRequiredArgs = true;
158162
@Getter @Setter
159163
protected RequestMappingMode requestMappingMode = RequestMappingMode.controller;
@@ -163,6 +167,8 @@ public enum RequestMappingMode {
163167
protected boolean useSpringBuiltInValidation = false;
164168
@Getter @Setter
165169
protected boolean useDeductionForOneOfInterfaces = false;
170+
@Getter @Setter
171+
protected boolean useJackson3 = false;
166172

167173
public SpringCodegen() {
168174
super();
@@ -278,6 +284,10 @@ public SpringCodegen() {
278284
cliOptions.add(CliOption.newBoolean(USE_SPRING_BOOT3,
279285
"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`.",
280286
useSpringBoot3));
287+
cliOptions.add(CliOption.newBoolean(USE_SPRING_BOOT4,
288+
"Generate code and provide dependencies for use with Spring Boot 4.x. (Use jakarta instead of javax in imports). Enabling this option will also enable `useJakartaEe`.",
289+
useSpringBoot4));
290+
cliOptions.add(CliOption.newBoolean(USE_JACKSON_3, "Set it in order to use jackson 3 dependencies (only allowed when `" + USE_SPRING_BOOT4 + "` is set and incompatible with `"+OPENAPI_NULLABLE+"`).", useJackson3));
281291
cliOptions.add(CliOption.newBoolean(GENERATE_CONSTRUCTOR_WITH_REQUIRED_ARGS,
282292
"Whether to generate constructors with required args for models",
283293
generatedConstructorWithRequiredArgs));
@@ -460,8 +470,13 @@ public void processOpts() {
460470
additionalProperties.put("springHttpStatus", new SpringHttpStatusLambda());
461471

462472
convertPropertyToBooleanAndWriteBack(USE_ENUM_CASE_INSENSITIVE, this::setUseEnumCaseInsensitive);
473+
convertPropertyToBooleanAndWriteBack(USE_JACKSON_3, this::setUseJackson3);
463474
convertPropertyToBooleanAndWriteBack(USE_SPRING_BOOT3, this::setUseSpringBoot3);
464-
if (isUseSpringBoot3()) {
475+
convertPropertyToBooleanAndWriteBack(USE_SPRING_BOOT4, this::setUseSpringBoot4);
476+
if(isUseSpringBoot3() && isUseSpringBoot4()){
477+
throw new IllegalArgumentException("Choose between spring boot 3 and spring boot 4");
478+
}
479+
if (isUseSpringBoot3() || isUseSpringBoot4()) {
465480
if (DocumentationProvider.SPRINGFOX.equals(getDocumentationProvider())) {
466481
throw new IllegalArgumentException(DocumentationProvider.SPRINGFOX.getPropertyName() + " is not supported with Spring Boot > 3.x");
467482
}
@@ -471,15 +486,25 @@ public void processOpts() {
471486
useJakartaEe = true;
472487
applyJakartaPackage();
473488
}
489+
if(isUseJackson3() && !isUseSpringBoot4()){
490+
throw new IllegalArgumentException("useJackson3 is only available with Spring Boot > 4.x");
491+
}
492+
if(isUseJackson3() && isOpenApiNullable()){
493+
throw new IllegalArgumentException("openApiNullable cannot be set with useJackson3");
494+
}
474495
convertPropertyToStringAndWriteBack(RESOURCE_FOLDER, this::setResourceFolder);
475496

497+
498+
// override parent one
499+
importMapping.put("JsonDeserialize", useJackson3 ? "tools.jackson.databind.annotation.JsonDeserialize" : "com.fasterxml.jackson.databind.annotation.JsonDeserialize");
500+
476501
typeMapping.put("file", "org.springframework.core.io.Resource");
477502
importMapping.put("Nullable", "org.springframework.lang.Nullable");
478503
importMapping.put("org.springframework.core.io.Resource", "org.springframework.core.io.Resource");
479504
importMapping.put("DateTimeFormat", "org.springframework.format.annotation.DateTimeFormat");
480505
importMapping.put("ApiIgnore", "springfox.documentation.annotations.ApiIgnore");
481506
importMapping.put("ParameterObject", "org.springdoc.api.annotations.ParameterObject");
482-
if (isUseSpringBoot3()) {
507+
if (isUseSpringBoot3() || isUseSpringBoot4()) {
483508
importMapping.put("ParameterObject", "org.springdoc.core.annotations.ParameterObject");
484509
}
485510

@@ -488,7 +513,9 @@ public void processOpts() {
488513
additionalProperties.put("delegate-method", true);
489514
}
490515

491-
if (isUseSpringBoot3()) {
516+
if (isUseSpringBoot4()) {
517+
supportingFiles.add(new SupportingFile("pom-sb4.mustache", "", "pom.xml"));
518+
} else if (isUseSpringBoot3()) {
492519
supportingFiles.add(new SupportingFile("pom-sb3.mustache", "", "pom.xml"));
493520
} else {
494521
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));

modules/openapi-generator/src/main/resources/JavaSpring/homeController.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import org.springframework.context.annotation.Bean;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.web.bind.annotation.RequestMapping;
66
{{#sourceDocumentationProvider}}
7+
{{#useJackson3}}
8+
import tools.jackson.dataformat.yaml.YAMLMapper;
9+
{{/useJackson3}}
10+
{{^useJackson3}}
711
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
12+
{{/useJackson3}}
813
import org.springframework.beans.factory.annotation.Value;
914
import org.springframework.core.io.Resource;
1015
import org.springframework.util.StreamUtils;

modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-boot/RFC3339DateFormat.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package {{basePackage}};
22

3+
{{#useJackson3}}
4+
import tools.jackson.databind.util.StdDateFormat;
5+
{{/useJackson3}}
6+
{{^useJackson3}}
37
import com.fasterxml.jackson.databind.util.StdDateFormat;
8+
{{/useJackson3}}
49

510
import java.text.DateFormat;
611
import java.text.FieldPosition;

modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-boot/openapi2SpringBoot.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package {{basePackage}};
22

33
{{#openApiNullable}}
4+
{{#useJackson3}}
5+
import tools.jackson.databind.JacksonModule;
6+
{{/useJackson3}}
7+
{{^useJackson3}}
48
import com.fasterxml.jackson.databind.Module;
9+
{{/useJackson3}}
510
import org.openapitools.jackson.nullable.JsonNullableModule;
611
{{/openApiNullable}}
712
import org.springframework.boot.SpringApplication;
@@ -25,10 +30,12 @@ public class OpenApiGeneratorApplication {
2530
}
2631

2732
{{#openApiNullable}}
33+
{{^useJackson3}}
2834
@Bean(name = "{{basePackage}}.OpenApiGeneratorApplication.jsonNullableModule")
2935
public Module jsonNullableModule() {
3036
return new JsonNullableModule();
3137
}
38+
{{/useJackson3}}
3239
{{/openApiNullable}}
3340

3441
}

modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-boot/pom-sb3.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<parent>
3737
<groupId>org.springframework.boot</groupId>
3838
<artifactId>spring-boot-starter-parent</artifactId>
39-
<version>3.1.3</version>
39+
<version>3.2.0</version>
4040
<relativePath/> <!-- lookup parent from repository -->
4141
</parent>
4242
{{/parentOverridden}}

0 commit comments

Comments
 (0)