Skip to content

Commit f91b68d

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

87 files changed

Lines changed: 10196 additions & 49 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
- Search the [open issue](https://github.com/openapitools/openapi-generator/issues) to ensure no one else has reported something similar and no one is actively working on similar proposed change.
1414
- If no one has suggested something similar, open an ["issue"](https://github.com/openapitools/openapi-generator/issues) with your suggestion to gather feedback from the community.
15-
- If you're adding a new option to a generator, please consider using the `-t` option with customized templates instead or start a discussion first by opening an issue as we want to avoid adding too many options to the generator.
15+
- If you're adding a new option to a generator, please consider using the `-t` option with customized templates instead or staonrt a discussi first by opening an issue as we want to avoid adding too many options to the generator.
1616
- It's recommended to **create a new git branch** for the change so that the merge commit message looks nicer in the commit history.
1717

1818
## How to contribute
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
generatorName: spring
2+
library: spring-http-interface
3+
outputDir: samples/client/petstore/spring-http-interface-noResponseEntity-jackson-3
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/spring/petstore-with-fake-endpoints-models-for-testing.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
6+
additionalProperties:
7+
artifactId: spring-http-interface-noResponseEntity-jackson-3
8+
snapshotVersion: "true"
9+
hideGenerationTimestamp: "true"
10+
modelNameSuffix: 'Dto'
11+
generatedConstructorWithRequiredArgs: "false"
12+
# documentation provider should be ignored
13+
documentationProvider: "springdoc"
14+
# annotation provider should be ignored
15+
annotationLibrary: "swagger2"
16+
# validation should be ignored
17+
useBeanValidation: "true"
18+
performBeanValidation: "true"
19+
useResponseEntity: "false"
20+
useJackson3: "true"

docs/generators/java-camel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ 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| |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|

docs/generators/spring.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ 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| |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|

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public class SpringCodegen extends AbstractJavaCodegen
101101
public static final String USE_SPRING_BUILT_IN_VALIDATION = "useSpringBuiltInValidation";
102102
public static final String USE_DEDUCTION_FOR_ONE_OF_INTERFACES = "useDeductionForOneOfInterfaces";
103103
public static final String SPRING_API_VERSION = "springApiVersion";
104+
public static final String USE_JACKSON_3 = "useJackson3";
104105

105106
@Getter
106107
public enum RequestMappingMode {
@@ -163,6 +164,8 @@ public enum RequestMappingMode {
163164
protected boolean useSpringBuiltInValidation = false;
164165
@Getter @Setter
165166
protected boolean useDeductionForOneOfInterfaces = false;
167+
@Getter @Setter
168+
protected boolean useJackson3 = false;
166169

167170
public SpringCodegen() {
168171
super();
@@ -249,6 +252,7 @@ public SpringCodegen() {
249252
cliOptions
250253
.add(CliOption.newBoolean(RETURN_SUCCESS_CODE, "Generated server returns 2xx code", returnSuccessCode));
251254
cliOptions.add(CliOption.newBoolean(SPRING_CONTROLLER, "Annotate the generated API as a Spring Controller", useSpringController));
255+
cliOptions.add(CliOption.newBoolean(USE_JACKSON_3, "Set it in order to use jackson 3 dependencies", useJackson3));
252256

253257
CliOption requestMappingOpt = new CliOption(REQUEST_MAPPING_OPTION,
254258
"Where to generate the class level @RequestMapping annotation.")
@@ -472,6 +476,10 @@ public void processOpts() {
472476
applyJakartaPackage();
473477
}
474478
convertPropertyToStringAndWriteBack(RESOURCE_FOLDER, this::setResourceFolder);
479+
convertPropertyToBooleanAndWriteBack(USE_JACKSON_3, this::setUseJackson3);
480+
481+
// override parent one
482+
importMapping.put("JsonDeserialize", useJackson3 ? "tools.jackson.databind.annotation.JsonDeserialize" : "com.fasterxml.jackson.databind.annotation.JsonDeserialize");
475483

476484
typeMapping.put("file", "org.springframework.core.io.Resource");
477485
importMapping.put("Nullable", "org.springframework.lang.Nullable");

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: 6 additions & 1 deletion
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;
@@ -26,7 +31,7 @@ public class OpenApiGeneratorApplication {
2631

2732
{{#openApiNullable}}
2833
@Bean(name = "{{basePackage}}.OpenApiGeneratorApplication.jsonNullableModule")
29-
public Module jsonNullableModule() {
34+
public {{#useJackson3}}JacksonModule{{/useJackson3}}{{^useJackson3}}Module{{/useJackson3}} jsonNullableModule() {
3035
return new JsonNullableModule();
3136
}
3237
{{/openApiNullable}}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@
180180
<version>3.0.2</version>
181181
</dependency>
182182
<dependency>
183+
{{#useJackson3}}
184+
<groupId>tools.jackson.dataformat</groupId>
185+
{{/useJackson3}}
186+
{{^useJackson3}}
183187
<groupId>com.fasterxml.jackson.dataformat</groupId>
188+
{{/useJackson3}}
184189
<artifactId>jackson-dataformat-yaml</artifactId>
185190
</dependency>
186191
{{#withXml}}
@@ -190,17 +195,32 @@
190195
<artifactId>jakarta.xml.bind-api</artifactId>
191196
</dependency>
192197
<dependency>
198+
{{#useJackson3}}
199+
<groupId>tools.jackson.dataformat</groupId>
200+
{{/useJackson3}}
201+
{{^useJackson3}}
193202
<groupId>com.fasterxml.jackson.dataformat</groupId>
203+
{{/useJackson3}}
194204
<artifactId>jackson-dataformat-xml</artifactId>
195205
</dependency>
196206
{{/withXml}}
197207
<dependency>
208+
{{#useJackson3}}
209+
<groupId>tools.jackson.datatype</groupId>
210+
{{/useJackson3}}
211+
{{^useJackson3}}
198212
<groupId>com.fasterxml.jackson.datatype</groupId>
213+
{{/useJackson3}}
199214
<artifactId>jackson-datatype-jsr310</artifactId>
200215
</dependency>
201216
{{#joda}}
202217
<dependency>
218+
{{#useJackson3}}
219+
<groupId>tools.jackson.datatype</groupId>
220+
{{/useJackson3}}
221+
{{^useJackson3}}
203222
<groupId>com.fasterxml.jackson.datatype</groupId>
223+
{{/useJackson3}}
204224
<artifactId>jackson-datatype-joda</artifactId>
205225
</dependency>
206226
{{/joda}}
@@ -247,7 +267,12 @@
247267
</dependency>
248268
{{/lombok}}
249269
<dependency>
270+
{{#useJackson3}}
271+
<groupId>tools.jackson.core</groupId>
272+
{{/useJackson3}}
273+
{{^useJackson3}}
250274
<groupId>com.fasterxml.jackson.core</groupId>
275+
{{/useJackson3}}
251276
<artifactId>jackson-databind</artifactId>
252277
</dependency>
253278
<dependency>

0 commit comments

Comments
 (0)