Skip to content

Commit 3bbc993

Browse files
authored
[Spring] Remove @nullable annotation for NativeWebRequest in Spring controller (#23576)
* Remove @nullable annotation for NativeWebRequest in Spring controller * Fix test * Commit generate file * rebuild * undo generate controllers * undo generate controllers * Better unit tests * Improve pattern maching to avoid confusion with JsonNullable * check for import on short on fully qualified class name * Merge master
1 parent f1935af commit 3bbc993

4 files changed

Lines changed: 56 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,10 @@ public class {{classname}}Controller implements {{classname}} {
8181
{{^isDelegate}}
8282
{{^reactive}}
8383

84-
@Nullable
8584
private final NativeWebRequest request;
8685

8786
@Autowired
88-
public {{classname}}Controller(@Nullable NativeWebRequest request) {
87+
public {{classname}}Controller(NativeWebRequest request) {
8988
this.request = request;
9089
}
9190

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/assertions/JavaFileAssert.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public JavaFileAssert fileContains(final String... lines) {
195195
.toString();
196196
Assertions.assertThat(actualBody)
197197
.withFailMessage(
198-
"File should contains lines\n====\n%s\n====\nbut actually was\n====\n%s\n====",
198+
"File should contain lines\n====\n%s\n====\nbut actually was\n====\n%s\n====",
199199
Arrays.stream(lines).collect(Collectors.joining(System.lineSeparator())), actualBody
200200
)
201201
.contains(lines);
@@ -209,7 +209,7 @@ public JavaFileAssert fileDoesNotContain(final String... lines) {
209209
.toString();
210210
Assertions.assertThat(actualBody)
211211
.withFailMessage(
212-
"File should not contains lines\n====\n%s\n====\nbut actually was\n====\n%s\n====",
212+
"File should not contain lines\n====\n%s\n====\nbut actually was\n====\n%s\n====",
213213
Arrays.stream(lines).collect(Collectors.joining(System.lineSeparator())), actualBody
214214
)
215215
.doesNotContain(lines);
@@ -227,11 +227,25 @@ public JavaFileAssert fileContainsPattern(final String pattern) {
227227
.toString();
228228
Assertions.assertThat(actualBody)
229229
.withFailMessage(
230-
"File should contains pattern\n====\n%s\n====\nbut actually was\n====\n%s\n====",
230+
"File should contain pattern\n====\n%s\n====\nbut actually was\n====\n%s\n====",
231231
pattern, actualBody
232232
)
233233
.containsPattern(pattern);
234234

235235
return this;
236236
}
237+
238+
public JavaFileAssert fileDoesNotContainPattern(final String pattern) {
239+
final String actualBody = actual.getTokenRange()
240+
.orElseThrow(() -> new IllegalStateException("Empty file"))
241+
.toString();
242+
Assertions.assertThat(actualBody)
243+
.withFailMessage(
244+
"File should not contain pattern\n====\n%s\n====\nbut actually was\n====\n%s\n====",
245+
pattern, actualBody
246+
)
247+
.doesNotContainPattern(pattern);
248+
249+
return this;
250+
}
237251
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/assertions/TypeAnnotationsAssert.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.assertj.core.util.CanIgnoreReturnValue;
55

66
import java.util.List;
7+
import java.util.regex.Pattern;
78

89
@CanIgnoreReturnValue
910
public class TypeAnnotationsAssert extends AbstractAnnotationsAssert<TypeAnnotationsAssert> {
@@ -18,4 +19,30 @@ protected TypeAnnotationsAssert(final JavaFileAssert fileAssert, final List<Anno
1819
public JavaFileAssert toType() {
1920
return fileAssert;
2021
}
22+
23+
/**
24+
* assert that the annotation is not specifed in an import.
25+
*
26+
* @param name classname of the annotation. For example "Nullable" or a full qualified class name like "java.util.List"
27+
*/
28+
public TypeAnnotationsAssert doesNotImportAnnotation(final String name) {
29+
String pattern = "import\\s+" +
30+
(name.contains(".")?"" : "[\\w.]+\\.") +
31+
Pattern.quote(name) + ";";
32+
this.toType().fileDoesNotContainPattern(pattern);
33+
return this;
34+
}
35+
36+
/**
37+
* assert that the annotation is imported.
38+
*
39+
* @param name clasname of the annotation. For example "Nullable" or a full qualified class name like "java.util.List"
40+
*/
41+
public TypeAnnotationsAssert doesImportAnnotation(final String name) {
42+
String pattern = "import\\s+" +
43+
(name.contains(".")?"" : "[\\w.]+\\.") +
44+
Pattern.quote(name) + ";";
45+
this.toType().fileContainsPattern(pattern);
46+
return this;
47+
}
2148
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6629,6 +6629,7 @@ public void testJspecify(String library, int springBootVersion, String fooApiFil
66296629
CONTAINER_DEFAULT_TO_NULL, true,
66306630
OPENAPI_NULLABLE, false,
66316631
USE_BEANVALIDATION, true,
6632+
INTERFACE_ONLY, false,
66326633
springVersionProperty, springBootVersion > 2
66336634
),
66346635
codegenConfigurator ->
@@ -6648,12 +6649,14 @@ public void testJspecify(String library, int springBootVersion, String fooApiFil
66486649
.doesNotContain("findbugs");
66496650
}
66506651
JavaFileAssert.assertThat(files.get("Foo.java"))
6652+
.assertTypeAnnotations().doesImportAnnotation("org.jspecify.annotations.Nullable").toType()
66516653
.fileContains(
66526654
"private java.time.@Nullable Instant dt;",
66536655
"private org.springframework.core.io.@Nullable Resource binary",
66546656
"setBinary(org.springframework.core.io.@Nullable Resource binary)"
66556657
);
66566658
JavaFileAssert.assertThat(files.get(fooApiFilename))
6659+
.assertTypeAnnotations().doesImportAnnotation("org.jspecify.annotations.Nullable").toType()
66576660
.fileContains(
66586661
"java.time.@Nullable Instant dtParam",
66596662
"java.time.@Nullable Instant dtQuery",
@@ -6663,6 +6666,14 @@ public void testJspecify(String library, int springBootVersion, String fooApiFil
66636666
.fileContains("@org.jspecify.annotations.NullMarked");
66646667
JavaFileAssert.assertThat(files.get("model/package-info.java"))
66656668
.fileContains("@org.jspecify.annotations.NullMarked");
6669+
6670+
if (SPRING_BOOT.equals(library)) {
6671+
// Nullable annotation is not (yet) put on NativeWebRequest, but still present as import when useJspecify=true
6672+
JavaFileAssert.assertThat(files.get("UploadApiController.java").toPath())
6673+
.assertTypeAnnotations()
6674+
.doesNotContainWithName("Nullable")
6675+
.doesImportAnnotation("org.jspecify.annotations.Nullable");
6676+
}
66666677
}
66676678

66686679
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)