Skip to content

Ignore null HttpMessageConverter in RestTemplate and HttpMessageConverterExtractor #23140

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

Closed
wants to merge 1 commit into from

Conversation

fodil-a
Copy link
Contributor

@fodil-a fodil-a commented Jun 15, 2019

Prior to this commit, if a null HttpMessageConverter was configured
in the RestTemplate, this would lead to a NullPointerException once
the list of converters was accessed.

This commit avoids such exceptions by ignoring null converters.

See gh-23123

If a null message converter is passed, restTemplate.postForObject will
throw a NullPointerException.
Checks are done to ensure it does not.

Solves spring-projects#23123

Signed-off-by: RustyTheClone <[email protected]>
@fodil-a
Copy link
Contributor Author

fodil-a commented Jun 15, 2019

This is an edit from pull request: #23132
I needed to change my fork's branch.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 15, 2019
@sbrannen sbrannen added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 16, 2019
@sbrannen
Copy link
Member

Tentatively slated for 5.2 RC1 and considering whether to backport to 5.1.x.

@sbrannen sbrannen added this to the 5.2 RC1 milestone Jun 16, 2019
@sbrannen sbrannen self-assigned this Jun 16, 2019
@sbrannen
Copy link
Member

@rstoyanchev and @jhoeller, shall we consider this a bug instead of an enhancement and assign it to 5.1.x?

@rstoyanchev
Copy link
Contributor

Having null converter is something we don't even expect and seeing the proposed checks makes it even stranger. Why would there be a null converter in the first place? I think the solution should be to reject these earlier where converters are configured.

@rstoyanchev
Copy link
Contributor

@sbrannen I don't consider it a bug. It's simply a missing validation.

@sbrannen
Copy link
Member

@sbrannen I don't consider it a bug. It's simply a missing validation.

Thanks for the feedback.

I actually came to the same conclusion myself.

Having null converter is something we don't even expect and seeing the proposed checks makes it even stranger. Why would there be a null converter in the first place?

I agree that it is a user error if a null converter is present.

I think the solution should be to reject these earlier where converters are configured.

The challenge is that RestTemplate#getMessageConverters() is documented as follows.

Return the list of message body converters.

The returned List is active and may get appended to.

Thus, users can modify the list (e.g., by appending/inserting null) at any time. In other words, RestTemplate returns a modifiable ArrayList of converters, and we probably shouldn't change that at this point.

I would rather perform the non-null validation every time the converters are accessed in RestTemplate and HttpMessageConverterExtractor and throw an IllegalStateException if we encounter a null reference, as opposed to simply ignoring them as in this PR.

@rstoyanchev, what do you think about that?

@rstoyanchev
Copy link
Contributor

users can modify the list (e.g., by appending/inserting null) at any time.

I would definitely add checks in the constructor and setter since that would provide the benefit of an early warning. Beyond that I think it's not worth repeatedly checking for something that should not be happening in the first place. At that point it almost doesn't matter how it fails, NPE or IAE. Either way it fails it needs to be fixed in the application.

@sbrannen
Copy link
Member

Superseded by #23151

@sbrannen sbrannen removed this from the 5.2 RC1 milestone Jun 18, 2019
@sbrannen sbrannen added the status: declined A suggestion or change that we don't feel we should currently apply label Jun 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants