Handle setHeader null values in response wrappers#18971
Open
therepanic wants to merge 1 commit intospring-projects:mainfrom
Open
Handle setHeader null values in response wrappers#18971therepanic wants to merge 1 commit intospring-projects:mainfrom
therepanic wants to merge 1 commit intospring-projects:mainfrom
Conversation
The main problem is that `HttpServletResponse`, according to its contract, accepts null values in `setHeader`. When an argument is null, we must remove the header. So, the main idea is to make these arguments `@Nullable` in our implementations and handle cases where they are null as described in the contract. By the way, some methods, such as `addHeader` in the same interface, can also accept null by contract, but in that case, we don't have to do anything. I think we're handling this correctly now, and we can afford to make their arguments nonnullable, as is now. Closes: spring-projectsgh-18970 Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
therepanic
commented
Mar 23, 2026
Comment on lines
+54
to
57
| public void setHeader(String name, @Nullable String value) { | ||
| validateCrlf(name, value); | ||
| super.setHeader(name, value); | ||
| } |
Contributor
Author
There was a problem hiding this comment.
We just need to mark it as @Nullable because if value is null, validateCrlf will work as expected anyway.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The main problem is that
HttpServletResponse, according to its contract, accepts null values insetHeader. When an argument is null, we must remove the header. So, the main idea is to make these arguments@Nullablein our implementations and handle cases where they are null as described in the contract.By the way, some methods, such as
addHeaderin the same interface, can also accept null by contract, but in that case, we don't have to do anything. I think we're handling this correctly now, and we can afford to make their arguments nonnullable, as is now.https://github.com/jakartaee/servlet/blob/main/api/src/main/java/jakarta/servlet/http/HttpServletResponse.java#L306
Closes: gh-18970