Skip to content

Commit 6e51370

Browse files
committed
AbstractJackson2Encoder support for MappingJacksonValue
Closes gh-26035
1 parent bcd2b9a commit 6e51370

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.fasterxml.jackson.databind.ObjectWriter;
3535
import com.fasterxml.jackson.databind.SequenceWriter;
3636
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
37+
import com.fasterxml.jackson.databind.ser.FilterProvider;
3738
import org.reactivestreams.Publisher;
3839
import reactor.core.publisher.Flux;
3940
import reactor.core.publisher.Mono;
@@ -48,6 +49,7 @@
4849
import org.springframework.core.log.LogFormatUtils;
4950
import org.springframework.http.MediaType;
5051
import org.springframework.http.codec.HttpMessageEncoder;
52+
import org.springframework.http.converter.json.MappingJacksonValue;
5153
import org.springframework.http.server.reactive.ServerHttpRequest;
5254
import org.springframework.http.server.reactive.ServerHttpResponse;
5355
import org.springframework.lang.Nullable;
@@ -148,7 +150,7 @@ public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory buffe
148150
byte[] separator = getStreamingMediaTypeSeparator(mimeType);
149151
if (separator != null) { // streaming
150152
try {
151-
ObjectWriter writer = createObjectWriter(elementType, mimeType, hints);
153+
ObjectWriter writer = createObjectWriter(elementType, mimeType, null, hints);
152154
ByteArrayBuilder byteBuilder = new ByteArrayBuilder(writer.getFactory()._getBufferRecycler());
153155
JsonEncoding encoding = getJsonEncoding(mimeType);
154156
JsonGenerator generator = getObjectMapper().getFactory().createGenerator(byteBuilder, encoding);
@@ -186,7 +188,18 @@ public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory buffe
186188
public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
187189
ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
188190

189-
ObjectWriter writer = createObjectWriter(valueType, mimeType, hints);
191+
Class<?> jsonView = null;
192+
FilterProvider filters = null;
193+
if (value instanceof MappingJacksonValue) {
194+
MappingJacksonValue container = (MappingJacksonValue) value;
195+
value = container.getValue();
196+
jsonView = container.getSerializationView();
197+
filters = container.getFilters();
198+
}
199+
ObjectWriter writer = createObjectWriter(valueType, mimeType, jsonView, hints);
200+
if (filters != null) {
201+
writer = writer.with(filters);
202+
}
190203
ByteArrayBuilder byteBuilder = new ByteArrayBuilder(writer.getFactory()._getBufferRecycler());
191204
try {
192205
JsonEncoding encoding = getJsonEncoding(mimeType);
@@ -268,10 +281,12 @@ private void logValue(@Nullable Map<String, Object> hints, Object value) {
268281
}
269282

270283
private ObjectWriter createObjectWriter(ResolvableType valueType, @Nullable MimeType mimeType,
271-
@Nullable Map<String, Object> hints) {
284+
@Nullable Class<?> jsonView, @Nullable Map<String, Object> hints) {
272285

273286
JavaType javaType = getJavaType(valueType.getType(), null);
274-
Class<?> jsonView = (hints != null ? (Class<?>) hints.get(Jackson2CodecSupport.JSON_VIEW_HINT) : null);
287+
if (jsonView == null && hints != null) {
288+
jsonView = (Class<?>) hints.get(Jackson2CodecSupport.JSON_VIEW_HINT);
289+
}
275290
ObjectWriter writer = (jsonView != null ?
276291
getObjectMapper().writerWithView(jsonView) : getObjectMapper().writer());
277292

spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonEncoderTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.http.codec.ServerSentEvent;
4040
import org.springframework.http.codec.json.JacksonViewBean.MyJacksonView1;
4141
import org.springframework.http.codec.json.JacksonViewBean.MyJacksonView3;
42+
import org.springframework.http.converter.json.MappingJacksonValue;
4243
import org.springframework.util.MimeType;
4344
import org.springframework.util.MimeTypeUtils;
4445
import org.springframework.web.testfixture.xml.Pojo;
@@ -214,6 +215,25 @@ public void classLevelJsonView() {
214215
null, hints);
215216
}
216217

218+
@Test
219+
public void jacksonValue() {
220+
JacksonViewBean bean = new JacksonViewBean();
221+
bean.setWithView1("with");
222+
bean.setWithView2("with");
223+
bean.setWithoutView("without");
224+
225+
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
226+
jacksonValue.setSerializationView(MyJacksonView1.class);
227+
228+
ResolvableType type = ResolvableType.forClass(MappingJacksonValue.class);
229+
230+
testEncode(Mono.just(jacksonValue), type, step -> step
231+
.consumeNextWith(expectString("{\"withView1\":\"with\"}")
232+
.andThen(DataBufferUtils::release))
233+
.verifyComplete(),
234+
null, Collections.emptyMap());
235+
}
236+
217237
@Test // gh-22771
218238
public void encodeWithFlushAfterWriteOff() {
219239
ObjectMapper mapper = new ObjectMapper();

0 commit comments

Comments
 (0)