Skip to content

Commit b2b3076

Browse files
committed
fixes(spring) Fixes @nullable annotation
* remove findbug dependency as spring generator set its import to 'org.springframework.lang.Nullable' * for springboot 4, use 'org.jspecify.annotations.Nullable' as spring one is Deprecated
1 parent 03c13fb commit b2b3076

135 files changed

Lines changed: 105 additions & 485 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.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,11 @@ public void processOpts() {
553553
importMapping.put("JsonDeserialize", (useJackson3 ? JACKSON3_PACKAGE : JACKSON2_PACKAGE) + ".databind.annotation.JsonDeserialize");
554554

555555
typeMapping.put("file", "org.springframework.core.io.Resource");
556-
importMapping.put("Nullable", "org.springframework.lang.Nullable");
556+
if(isUseSpringBoot4()){
557+
importMapping.put("Nullable", "org.jspecify.annotations.Nullable");
558+
} else {
559+
importMapping.put("Nullable", "org.springframework.lang.Nullable");
560+
}
557561
importMapping.put("org.springframework.core.io.Resource", "org.springframework.core.io.Resource");
558562
importMapping.put("DateTimeFormat", "org.springframework.format.annotation.DateTimeFormat");
559563
importMapping.put("ParameterObject", "org.springdoc.api.annotations.ParameterObject");

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,6 @@
158158
</dependency>
159159
{{/swagger2AnnotationLibrary}}
160160
{{/springDocDocumentationProvider}}
161-
<!-- @Nullable annotation -->
162-
<dependency>
163-
<groupId>com.google.code.findbugs</groupId>
164-
<artifactId>jsr305</artifactId>
165-
<version>3.0.2</version>
166-
</dependency>
167161
{{#withXml}}
168162
<!-- XML processing: Jackson -->
169163
<dependency>

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,6 @@
162162
</dependency>
163163
{{/swagger2AnnotationLibrary}}
164164
{{/springDocDocumentationProvider}}
165-
<!-- @Nullable annotation -->
166-
<dependency>
167-
<groupId>com.google.code.findbugs</groupId>
168-
<artifactId>jsr305</artifactId>
169-
<version>3.0.2</version>
170-
</dependency>
171165
{{#withXml}}
172166
<!-- XML processing: Jackson -->
173167
<dependency>

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,6 @@
173173
</dependency>
174174
{{/swagger2AnnotationLibrary}}
175175
{{/springDocDocumentationProvider}}
176-
<!-- @Nullable annotation -->
177-
<dependency>
178-
<groupId>com.google.code.findbugs</groupId>
179-
<artifactId>jsr305</artifactId>
180-
<version>3.0.2</version>
181-
</dependency>
182176
{{#withXml}}
183177
<!-- XML processing: Jackson -->
184178
<dependency>

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,6 @@
8484
</dependency>
8585
{{/swagger2AnnotationLibrary}}
8686
{{/springDocDocumentationProvider}}
87-
<!-- @Nullable annotation -->
88-
<dependency>
89-
<groupId>com.google.code.findbugs</groupId>
90-
<artifactId>jsr305</artifactId>
91-
{{^parentOverridden}}
92-
<version>3.0.2</version>
93-
{{/parentOverridden}}
94-
</dependency>
9587
<dependency>
9688
<groupId>org.springframework.cloud</groupId>
9789
<artifactId>spring-cloud-starter-openfeign</artifactId>

modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-cloud/pom-sb4.mustache

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,6 @@
8484
</dependency>
8585
{{/swagger2AnnotationLibrary}}
8686
{{/springDocDocumentationProvider}}
87-
<!-- @Nullable annotation -->
88-
<dependency>
89-
<groupId>com.google.code.findbugs</groupId>
90-
<artifactId>jsr305</artifactId>
91-
{{^parentOverridden}}
92-
<version>3.0.2</version>
93-
{{/parentOverridden}}
94-
</dependency>
9587
<dependency>
9688
<groupId>org.springframework.cloud</groupId>
9789
<artifactId>spring-cloud-starter-openfeign</artifactId>

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,6 @@
9999
</dependency>
100100
{{/swagger2AnnotationLibrary}}
101101
{{/springDocDocumentationProvider}}
102-
<!-- @Nullable annotation -->
103-
<dependency>
104-
<groupId>com.google.code.findbugs</groupId>
105-
<artifactId>jsr305</artifactId>
106-
{{^parentOverridden}}
107-
<version>3.0.2</version>
108-
{{/parentOverridden}}
109-
</dependency>
110102
<dependency>
111103
<groupId>org.springframework.cloud</groupId>
112104
<artifactId>spring-cloud-starter-openfeign</artifactId>

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@
5757
<groupId>org.springframework.boot</groupId>
5858
<artifactId>spring-boot-starter-{{#reactive}}webflux{{/reactive}}{{^reactive}}web{{/reactive}}</artifactId>
5959
</dependency>
60-
<!-- @Nullable annotation -->
61-
<dependency>
62-
<groupId>com.google.code.findbugs</groupId>
63-
<artifactId>jsr305</artifactId>
64-
<version>3.0.2</version>
65-
</dependency>
6660
<dependency>
6761
<groupId>jakarta.validation</groupId>
6862
<artifactId>jakarta.validation-api</artifactId>

modules/openapi-generator/src/main/resources/JavaSpring/libraries/spring-http-interface/pom-sb4.mustache

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@
5757
<groupId>org.springframework.boot</groupId>
5858
<artifactId>spring-boot-starter-{{#reactive}}web{{/reactive}}{{^reactive}}rest{{/reactive}}client</artifactId>
5959
</dependency>
60-
<!-- @Nullable annotation -->
61-
<dependency>
62-
<groupId>com.google.code.findbugs</groupId>
63-
<artifactId>jsr305</artifactId>
64-
<version>3.0.2</version>
65-
</dependency>
6660
<dependency>
6761
<groupId>jakarta.validation</groupId>
6862
<artifactId>jakarta.validation-api</artifactId>

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6488,6 +6488,42 @@ public void shouldAddNullableImportForArrayTypeModels() throws IOException {
64886488
.hasImports("org.springframework.lang.Nullable");
64896489
}
64906490

6491+
@Test
6492+
public void shouldAddNullableImportForArrayTypeModelsForSpringBoot4() throws IOException {
6493+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
6494+
output.deleteOnExit();
6495+
6496+
final OpenAPI openAPI = TestUtils.parseFlattenSpec(
6497+
"src/test/resources/3_0/spring/petstore-with-fake-endpoints-models-for-testing-with-spring-pageable.yaml");
6498+
final SpringCodegen codegen = new SpringCodegen();
6499+
codegen.setOpenAPI(openAPI);
6500+
codegen.setOutputDir(output.getAbsolutePath());
6501+
codegen.additionalProperties().put(INTERFACE_ONLY, "true");
6502+
codegen.additionalProperties().put(CodegenConstants.GENERATE_ALIAS_AS_MODEL, "true");
6503+
codegen.additionalProperties().put(SpringCodegen.USE_SPRING_BOOT4, "true");
6504+
codegen.additionalProperties().put(SpringCodegen.USE_JACKSON_3, "true");
6505+
codegen.additionalProperties().put(SpringCodegen.OPENAPI_NULLABLE, "false");
6506+
6507+
ClientOptInput input = new ClientOptInput();
6508+
input.openAPI(openAPI);
6509+
input.config(codegen);
6510+
6511+
DefaultGenerator generator = new DefaultGenerator();
6512+
generator.setGenerateMetadata(false);
6513+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
6514+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
6515+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
6516+
6517+
Map<String, File> files = generator.opts(input).generate().stream()
6518+
.collect(Collectors.toMap(File::getName, Function.identity()));
6519+
6520+
// AnimalFarm is an array-type model with no properties (issue #22788)
6521+
JavaFileAssert.assertThat(files.get("AnimalFarm.java"))
6522+
.hasImports("org.jspecify.annotations.Nullable");
6523+
JavaFileAssert.assertThat(files.get("Pet.java"))
6524+
.hasImports("org.jspecify.annotations.Nullable");
6525+
}
6526+
64916527
@Test
64926528
public void shouldRefuseJackson3WithoutSpringboot4() throws IOException {
64936529
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();

0 commit comments

Comments
 (0)