-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Revise nullability in HttpEntity and ResponseEntity #36238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ngocnhan-tran1996
wants to merge
6
commits into
spring-projects:main
Choose a base branch
from
ngocnhan-tran1996:gh-36236
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+132
−82
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc3a495
Add `@Nullable` to ResponseEntity generic type
ngocnhan-tran1996 49f2805
Leverage the nullability defined at type level
ngocnhan-tran1996 4e738da
Leverage the nullability defined at type level
ngocnhan-tran1996 4fa027d
Leverage the nullability defined at type level
ngocnhan-tran1996 9e89481
Fix unit test
ngocnhan-tran1996 1a6926e
Remove unused `@Nullable`
ngocnhan-tran1996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,7 +78,7 @@ | |
| * @see org.springframework.web.client.RestOperations#getForEntity(URI, Class) | ||
| * @see RequestEntity | ||
| */ | ||
| public class ResponseEntity<T> extends HttpEntity<T> { | ||
| public class ResponseEntity<T extends @Nullable Object> extends HttpEntity<T> { | ||
|
|
||
| private final HttpStatusCode status; | ||
|
|
||
|
|
@@ -87,6 +87,7 @@ public class ResponseEntity<T> extends HttpEntity<T> { | |
| * Create a {@code ResponseEntity} with a status code only. | ||
| * @param status the status code | ||
| */ | ||
| @SuppressWarnings("NullAway") | ||
| public ResponseEntity(HttpStatusCode status) { | ||
| this(null, (HttpHeaders) null, status); | ||
| } | ||
|
|
@@ -96,7 +97,7 @@ public ResponseEntity(HttpStatusCode status) { | |
| * @param body the entity body | ||
| * @param status the status code | ||
| */ | ||
| public ResponseEntity(@Nullable T body, HttpStatusCode status) { | ||
| public ResponseEntity(T body, HttpStatusCode status) { | ||
| this(body, (HttpHeaders) null, status); | ||
| } | ||
|
|
||
|
|
@@ -106,6 +107,7 @@ public ResponseEntity(@Nullable T body, HttpStatusCode status) { | |
| * @param status the status code | ||
| * @since 7.0 | ||
| */ | ||
| @SuppressWarnings("NullAway") | ||
| public ResponseEntity(HttpHeaders headers, HttpStatusCode status) { | ||
| this(null, headers, status); | ||
| } | ||
|
|
@@ -117,7 +119,7 @@ public ResponseEntity(HttpHeaders headers, HttpStatusCode status) { | |
| * @param rawStatus the status code value | ||
| * @since 7.0 | ||
| */ | ||
| public ResponseEntity(@Nullable T body, @Nullable HttpHeaders headers, int rawStatus) { | ||
| public ResponseEntity(T body, @Nullable HttpHeaders headers, int rawStatus) { | ||
sdeleuze marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please correct me if i'm wrong |
||
| this(body, headers, HttpStatusCode.valueOf(rawStatus)); | ||
| } | ||
|
|
||
|
|
@@ -128,7 +130,7 @@ public ResponseEntity(@Nullable T body, @Nullable HttpHeaders headers, int rawSt | |
| * @param statusCode the status code | ||
| * @since 7.0 | ||
| */ | ||
| public ResponseEntity(@Nullable T body, @Nullable HttpHeaders headers, HttpStatusCode statusCode) { | ||
| public ResponseEntity(T body, @Nullable HttpHeaders headers, HttpStatusCode statusCode) { | ||
| super(body, headers); | ||
| Assert.notNull(statusCode, "HttpStatusCode must not be null"); | ||
|
|
||
|
|
@@ -141,6 +143,7 @@ public ResponseEntity(@Nullable T body, @Nullable HttpHeaders headers, HttpStatu | |
| * @param status the status code | ||
| * @deprecated in favor of {@link #ResponseEntity(HttpHeaders, HttpStatusCode)} | ||
| */ | ||
| @SuppressWarnings("NullAway") | ||
| @Deprecated(since = "7.0", forRemoval = true) | ||
| public ResponseEntity(MultiValueMap<String, String> headers, HttpStatusCode status) { | ||
| this(null, headers, status); | ||
|
|
@@ -155,7 +158,7 @@ public ResponseEntity(MultiValueMap<String, String> headers, HttpStatusCode stat | |
| * @deprecated in favor of {@link #ResponseEntity(Object, HttpHeaders, int)} | ||
| */ | ||
| @Deprecated(since = "7.0", forRemoval = true) | ||
| public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, int rawStatus) { | ||
| public ResponseEntity(T body, @Nullable MultiValueMap<String, String> headers, int rawStatus) { | ||
| this(body, headers, HttpStatusCode.valueOf(rawStatus)); | ||
| } | ||
|
|
||
|
|
@@ -168,7 +171,7 @@ public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> | |
| */ | ||
| @SuppressWarnings("removal") | ||
| @Deprecated(since = "7.0", forRemoval = true) | ||
| public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatusCode statusCode) { | ||
| public ResponseEntity(T body, @Nullable MultiValueMap<String, String> headers, HttpStatusCode statusCode) { | ||
| super(body, headers); | ||
| Assert.notNull(statusCode, "HttpStatusCode must not be null"); | ||
|
|
||
|
|
@@ -261,7 +264,7 @@ public static BodyBuilder ok() { | |
| * @return the created {@code ResponseEntity} | ||
| * @since 4.1 | ||
| */ | ||
| public static <T> ResponseEntity<T> ok(@Nullable T body) { | ||
| public static <T extends @Nullable Object> ResponseEntity<T> ok(T body) { | ||
| return ok().body(body); | ||
| } | ||
|
|
||
|
|
@@ -294,7 +297,7 @@ public static HeadersBuilder<?> of(ProblemDetail body) { | |
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public <T> ResponseEntity<T> build() { | ||
| public <T extends @Nullable Object> ResponseEntity<T> build() { | ||
| return (ResponseEntity<T>) body(body); | ||
| } | ||
| }; | ||
|
|
@@ -308,7 +311,7 @@ public <T> ResponseEntity<T> build() { | |
| * @return the created {@code ResponseEntity} | ||
| * @since 6.0.5 | ||
| */ | ||
| public static <T> ResponseEntity<T> ofNullable(@Nullable T body) { | ||
| public static <T extends @Nullable Object> ResponseEntity<T> ofNullable(T body) { | ||
| if (body == null) { | ||
| return notFound().build(); | ||
| } | ||
|
|
@@ -516,7 +519,7 @@ public interface HeadersBuilder<B extends HeadersBuilder<B>> { | |
| * @return the response entity | ||
| * @see BodyBuilder#body(Object) | ||
| */ | ||
| <T> ResponseEntity<T> build(); | ||
| <T extends @Nullable Object> ResponseEntity<T> build(); | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -550,7 +553,7 @@ public interface BodyBuilder extends HeadersBuilder<BodyBuilder> { | |
| * @param body the body of the response entity | ||
| * @return the built response entity | ||
| */ | ||
| <T> ResponseEntity<T> body(@Nullable T body); | ||
| <T extends @Nullable Object> ResponseEntity<T> body(T body); | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -652,13 +655,14 @@ public BodyBuilder varyBy(String... requestHeaders) { | |
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("NullAway") | ||
| @Override | ||
| public <T> ResponseEntity<T> build() { | ||
| public <T extends @Nullable Object> ResponseEntity<T> build() { | ||
| return body(null); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> ResponseEntity<T> body(@Nullable T body) { | ||
| public <T extends @Nullable Object> ResponseEntity<T> body(T body) { | ||
| return new ResponseEntity<>(body, this.headers, this.statusCode); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you check how
ResponseEntityof "empty value" translates in a sample application checked with NullAway, using https://github.com/sdeleuze/jspecify-nullaway-demo as a basis maybe?I guess that would be
ResponseEntity<@Nullable Void>based on jspecify/jspecify#51 but worth to check.