Skip to content

NPE in FeignResponseAdapter in case Content-Length header is not provided and response status is not 1XX, NO_CONTENT and NOT_MODIFIED #972

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
martinsefcik opened this issue Jan 26, 2024 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@martinsefcik
Copy link

Describe the bug
If HTTP response has no Content-Legnth header defined and response status code is not 1XX, NO_CONTENT and NOT_MODIFIED (implemented in org.springframework.web.client.IntrospectingClientHttpResponse#hasMessageBody) and response has no body then we are getting the following NPE in org.springframework.cloud.openfeign.support.SpringDecoder.FeignResponseAdapter#getBody.

java.lang.NullPointerException: Cannot invoke "feign.Response$Body.asInputStream()" because the return value of "feign.Response.body()" is null
	at org.springframework.cloud.openfeign.support.SpringDecoder$FeignResponseAdapter.getBody(SpringDecoder.java:116)
	at org.springframework.web.client.IntrospectingClientHttpResponse.hasEmptyMessageBody(IntrospectingClientHttpResponse.java:83)
	at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:90)
	at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:75)
	at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:53)
	at feign.optionals.OptionalDecoder.decode(OptionalDecoder.java:36)
	at feign.InvocationContext.proceed(InvocationContext.java:36)
	at feign.ResponseHandler.decode(ResponseHandler.java:122)
	at feign.ResponseHandler.handleResponse(ResponseHandler.java:73)
	at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:114)
	at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:70)
	at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:96)

It's happening because org.springframework.web.client.HttpMessageConverterExtractor used in SpringDecoder in extractData method calls org.springframework.web.client.IntrospectingClientHttpResponse#hasMessageBody method which identifies that response has message and because of that also org.springframework.web.client.IntrospectingClientHttpResponse#hasEmptyMessageBody is executed which tries to get response body input stream by executing org.springframework.cloud.openfeign.support.SpringDecoder.FeignResponseAdapter#getBody but this method does not check if body is null and tries to get input stream from it.

So changing this in org.springframework.cloud.openfeign.support.SpringDecoder.FeignResponseAdapter#getBody:

return response.body().asInputStream();

to this:

return response.body() != null ? response.body().asInputStream() : null;

helped in our case.

Sample
Just call any endpoint which is not returning Content-Length header and body and response status is not 1XX, NO_CONTENT and NOT_MODIFIED.
I our case it is simple HEAD call with no body and 200 response status code which is doing that.

Versions used:
spring-cloud-openfeign-core = 4.0.4
spring-boot = 3.1.5

@igorbljahhin
Copy link

Same issue for spring-cloud-openfeign-core = 3.0.1.

@OlgaMaciaszek
Copy link
Collaborator

Hello @martinsefcik, thanks for pointing this out. Makes sense. Will fix it.

@OlgaMaciaszek OlgaMaciaszek self-assigned this Feb 16, 2024
@OlgaMaciaszek OlgaMaciaszek added bug Something isn't working and removed waiting-for-triage labels Feb 16, 2024
@OlgaMaciaszek OlgaMaciaszek moved this to In Progress in 2023.0.1 Feb 16, 2024
@OlgaMaciaszek OlgaMaciaszek added this to the 4.1.1 milestone Feb 16, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in 2023.0.1 Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Status: Done
Development

No branches or pull requests

4 participants