Skip to content
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 response body missing 1st byte inside UnknownContentTypeException #27374

Conversation

xak2000
Copy link
Contributor

@xak2000 xak2000 commented Sep 8, 2021

This commit fixes the creation of UnknownContentTypeException when it is thrown from HttpMessageConverterExtractor.extractData.

This exception is usually thrown from RestTemplate when HttpMessageConverterExtractor can't extract response body.
In this case HttpMessageConverterExtractor throws UnknownContentTypeException with response body as byte array.

Before this change the returned exception contained incorrect responseBody when InputStream from ClientHttpResponse was not markable (markSupported() is false). This is the usual case for HTTP response's InputStream implementations.

The first byte of response body inside UnknownContentTypeException was missing because it was already read from InputStream.

The responseBody was read from original InputStream, while it should be read from the wrapper of original InputStream.


To a person who will merge: please improve the commit message if possible as I feel difficulties in attempt to correctly and briefly describe the change, that is essentially a bug fix (not a native English speaker).

This commit fixes the creation of UnknownContentTypeException when
it is thrown from HttpMessageConverterExtractor.extractData.

This exception is usually thrown from RestTemplate when
HttpMessageConverterExtractor can't extract response body.
In this case HttpMessageConverterExtractor throws
UnknownContentTypeException with response body as byte array.

Before this change the returned exception contained incorrect
responseBody when InputStream from ClientHttpResponse was not
markable (markSupported is false). This is the usual case for
HTTP response's InputStream implementations.

The first byte of response body inside UnknownContentTypeException
was missing because it was already read from InputStream.

The responseBody was read from original InputStream, while it should
be read from the wrapper of original InputStream.

This commit fixes this.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 8, 2021
@rstoyanchev rstoyanchev self-assigned this Sep 10, 2021
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 10, 2021
@rstoyanchev rstoyanchev added this to the 5.3.10 milestone Sep 10, 2021
rstoyanchev added a commit that referenced this pull request Sep 10, 2021
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this pull request Mar 26, 2022
HttpMessageConverterExtractor uses MessageBodyClientHttpResponseWrapper
which may read the first byte of the response stream to check if there
is content. After that it is necessary to use the wrapper to get the
full body.

This commit ensures that when UnknownContentTypeException is raised
it gets the body through the wrapper, or otherwise the first byte is
missed if the InputStream is not markable.

Closes spring-projectsgh-27374
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this pull request Mar 26, 2022
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) type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants