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
Fix NullPointerException with empty X-Forwarded-For header #16046
Conversation
@@ -45,8 +48,9 @@ | |||
this.method = request.getMethodValue(); | |||
this.headers = request.getHeaders(); | |||
this.uri = request.getURI(); | |||
this.remoteAddress = (request.getRemoteAddress() != null) | |||
? request.getRemoteAddress().getAddress().toString() : null; | |||
this.remoteAddress = Optional.ofNullable(request.getRemoteAddress()) |
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.
We prefer not to use Optional
for internal null
checks such as this. If the readability of the ternary is a problem it could be pulled out into a method that's something like this instead:
private static String asString(InetSocketAddress socketAddress) {
return (socketAddress != null && socketAddress.getAddress() != null)
? socketAddress.getAddress().toString() : null;
}
It should be possible to test it using an |
@wilkinsona Thank you so much! |
Thanks for the updates, particularly the two new tests. Did you consider adding a new test class for |
@wilkinsona sure, I need some time. I let you know. |
8b7167b
to
d1e2df7
Compare
@wilkinsona |
…lved, and add a class for testing ServerWebExchangeTraceableRequest. closes spring-projectsgh-16044
* pr/16046: Polish "Fix NullPointerException with empty X-Forwarded-For header" Fix NullPointerException with empty X-Forwarded-For header
Thank you @nosan. Please review the polish commit when you get a chance. We prefer to use |
@snicoll I did not know about this, I will keep in mind. |
gh-16044
@snicoll
I have not found a way how to test this, if you have an idea how to test, please let me know.