-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
AbstractJackson2HttpMessageConverter writes partial data when exception occurs during write #26246
Comments
Prior to this commit, a change introduced in gh-25910 would close the `JsonGenerator` after it's been used for JSON serialization. This would not only close it and recycle resources, but also flush the underlyning buffer to the output. In a case where the JSON serialization process would throw an exception, the buffer would be still flushed to the response output. Before the change introduced in gh-25910, the response body could be still empty at that point and error handling could write an error body instead. This commits only closes the `JsonGenerator` when serialization has been successful. Note that we're changing this in the spirit of backwards compatibility in the 5.2.x line, but change this won't be merged forward on the 5.3.x line, for several reasons: * this behavior is not consistent. If the JSON output exceeds a certain size, or if Jackson has been configured to flush after each write, the response output might still contain an incomplete JSON payload (just like before this change) * this behavior is not consistent with the WebFlux and Messaging codecs, which are flushing or closing the generator * not closing the generator for error cases prevents resources from being recycled as expected by Jackson Fixes gh-26246
Fixed via a8091b9 |
Thanks for this report @jdelong747 ! We've reverted this behavior change with a8091b9. There are still a few things to consider here. The fact that the JSON payload might not be written to the response output in case of serialization errors is not a feature: this is really about the fact that the underlying buffer is not full yet or that Jackson didn't decide to flush it at that point. This is really accidental. In general, you should not rely on an Note that we won't be merging forward this commit, as:
I'm afraid I don't see a way to reproduce this former behavior on your side in Framework 5.3, besides getting full control and serializing to some We've added a note in the migration wiki in the MVC section of the 5.3 migration guide. |
Affects: 5.2.10.RELEASE and later
The AbstractJackson2HttpMessageConverter.writeInteral method was modified to use a try with resources block to optimize Jackson resource management gh-25910. This now calls close on the JsonGenerator in the event of an error such as a JsonProcessingException, which flushes what was written before the error occurred to the response body. When attempting to return a customized error response from a ControllerAdvice, the response body will contain the data written before the exception was thrown concatenated with the data written by the ControllerAdvice. This leads to an invalid response being returned to the client.
The text was updated successfully, but these errors were encountered: