You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Avoid closing Jackson JsonGenerator for error cases
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
Fixesgh-26246
Copy file name to clipboardExpand all lines: spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java
Copy file name to clipboardExpand all lines: spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java
+25-14Lines changed: 25 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -16,11 +16,13 @@
16
16
17
17
packageorg.springframework.http.converter.json;
18
18
19
+
importjava.io.ByteArrayOutputStream;
19
20
importjava.io.IOException;
20
21
importjava.lang.reflect.Type;
21
22
importjava.nio.charset.Charset;
22
23
importjava.nio.charset.StandardCharsets;
23
24
importjava.util.ArrayList;
25
+
importjava.util.Arrays;
24
26
importjava.util.HashMap;
25
27
importjava.util.List;
26
28
importjava.util.Map;
@@ -135,13 +137,7 @@ public void readUntyped() throws IOException {
0 commit comments