Skip to content

Commit 0ead050

Browse files
committed
AbstractJackson2Encoder uses private fields
Make the protected fields in AbstractJackson2Encoder private plus minor refactoring to the way streaming separators are applied. The current (5.0.3) behavior is to always use '\n', but in 5.0.4 the newly supported "application/stream+x-jackson-smile" needs to be excluded from that. For now, separator determination remains private in the abstract base class, but current behavior remains which is to apply '\n' by default. Issue: SPR-15424
1 parent 6d75732 commit 0ead050

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.Annotation;
2222
import java.util.ArrayList;
2323
import java.util.Collections;
24+
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
2627

@@ -58,9 +59,18 @@
5859
*/
5960
public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport implements HttpMessageEncoder<Object> {
6061

61-
protected final List<MediaType> streamingMediaTypes = new ArrayList<>(1);
62+
private static final byte[] NEWLINE_SEPARATOR = {'\n'};
6263

63-
protected boolean streamingLineSeparator = true;
64+
private static final Map<MediaType, byte[]> STREAM_SEPARATORS;
65+
66+
static {
67+
STREAM_SEPARATORS = new HashMap<>();
68+
STREAM_SEPARATORS.put(MediaType.APPLICATION_STREAM_JSON, NEWLINE_SEPARATOR);
69+
STREAM_SEPARATORS.put(MediaType.parseMediaType("application/stream+x-jackson-smile"), new byte[0]);
70+
}
71+
72+
73+
private final List<MediaType> streamingMediaTypes = new ArrayList<>(1);
6474

6575

6676
/**
@@ -103,20 +113,23 @@ public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory buffe
103113
return Flux.from(inputStream).map(value ->
104114
encodeValue(value, mimeType, bufferFactory, elementType, hints));
105115
}
106-
else if (this.streamingMediaTypes.stream().anyMatch(mediaType -> mediaType.isCompatibleWith(mimeType))) {
107-
return Flux.from(inputStream).map(value -> {
108-
DataBuffer buffer = encodeValue(value, mimeType, bufferFactory, elementType, hints);
109-
if (streamingLineSeparator) {
110-
buffer.write(new byte[]{'\n'});
111-
}
112-
return buffer;
113-
});
114-
}
115-
else {
116-
ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType);
117-
return Flux.from(inputStream).collectList().map(list ->
118-
encodeValue(list, mimeType, bufferFactory, listType, hints)).flux();
116+
117+
for (MediaType streamingMediaType : this.streamingMediaTypes) {
118+
if (streamingMediaType.isCompatibleWith(mimeType)) {
119+
byte[] separator = STREAM_SEPARATORS.getOrDefault(streamingMediaType, NEWLINE_SEPARATOR);
120+
return Flux.from(inputStream).map(value -> {
121+
DataBuffer buffer = encodeValue(value, mimeType, bufferFactory, elementType, hints);
122+
if (separator != null) {
123+
buffer.write(separator);
124+
}
125+
return buffer;
126+
});
127+
}
119128
}
129+
130+
ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType);
131+
return Flux.from(inputStream).collectList().map(list ->
132+
encodeValue(list, mimeType, bufferFactory, listType, hints)).flux();
120133
}
121134

122135
private DataBuffer encodeValue(Object value, @Nullable MimeType mimeType, DataBufferFactory bufferFactory,

spring-web/src/main/java/org/springframework/http/codec/json/Jackson2JsonEncoder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.http.codec.json;
1818

19+
import java.util.Collections;
1920
import java.util.List;
2021
import java.util.Map;
2122

@@ -55,7 +56,7 @@ public Jackson2JsonEncoder() {
5556

5657
public Jackson2JsonEncoder(ObjectMapper mapper, MimeType... mimeTypes) {
5758
super(mapper, mimeTypes);
58-
this.streamingMediaTypes.add(MediaType.APPLICATION_STREAM_JSON);
59+
setStreamingMediaTypes(Collections.singletonList(MediaType.APPLICATION_STREAM_JSON));
5960
this.ssePrettyPrinter = initSsePrettyPrinter();
6061
}
6162

spring-web/src/main/java/org/springframework/http/codec/json/Jackson2SmileEncoder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ public Jackson2SmileEncoder() {
5151
public Jackson2SmileEncoder(ObjectMapper mapper, MimeType... mimeTypes) {
5252
super(mapper, mimeTypes);
5353
Assert.isAssignable(SmileFactory.class, mapper.getFactory().getClass());
54-
this.streamingMediaTypes.add(new MediaType("application", "stream+x-jackson-smile"));
55-
this.streamingLineSeparator = false;
54+
setStreamingMediaTypes(Collections.singletonList(new MediaType("application", "stream+x-jackson-smile")));
5655
}
5756

5857
}

0 commit comments

Comments
 (0)