Skip to content

Commit

Permalink
Unwrap MappingJacksonValue before selecting ObjectMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Mar 16, 2022
1 parent cb39b07 commit 21d6131
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Expand Up @@ -196,22 +196,26 @@ public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory buffe
public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

ObjectMapper mapper = selectObjectMapper(valueType, mimeType);
if (mapper == null) {
throw new IllegalStateException("No ObjectMapper for " + valueType);
}
Class<?> jsonView = null;
FilterProvider filters = null;
if (value instanceof MappingJacksonValue) {
MappingJacksonValue container = (MappingJacksonValue) value;
value = container.getValue();
valueType = ResolvableType.forInstance(value);
jsonView = container.getSerializationView();
filters = container.getFilters();
}

ObjectMapper mapper = selectObjectMapper(valueType, mimeType);
if (mapper == null) {
throw new IllegalStateException("No ObjectMapper for " + valueType);
}

ObjectWriter writer = createObjectWriter(mapper, valueType, mimeType, jsonView, hints);
if (filters != null) {
writer = writer.with(filters);
}

ByteArrayBuilder byteBuilder = new ByteArrayBuilder(writer.getFactory()._getBufferRecycler());
try {
JsonEncoding encoding = getJsonEncoding(mimeType);
Expand Down
Expand Up @@ -231,6 +231,29 @@ public void jacksonValue() {
);
}

@Test // gh-28045
public void jacksonValueUnwrappedBeforeObjectMapperSelection() {

JacksonViewBean bean = new JacksonViewBean();
bean.setWithView1("with");
bean.setWithView2("with");
bean.setWithoutView("without");

MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
jacksonValue.setSerializationView(MyJacksonView1.class);

ResolvableType type = ResolvableType.forClass(MappingJacksonValue.class);

MediaType halMediaType = MediaType.parseMediaType("application/hal+json");
ObjectMapper mapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true);
this.encoder.registerObjectMappersForType(JacksonViewBean.class, map -> map.put(halMediaType, mapper));

testEncode(Mono.just(jacksonValue), type, halMediaType, Collections.emptyMap(), step -> step
.consumeNextWith(expectString("{\n \"withView1\" : \"with\"\n}").andThen(DataBufferUtils::release))
.verifyComplete()
);
}

@Test // gh-22771
public void encodeWithFlushAfterWriteOff() {
ObjectMapper mapper = new ObjectMapper();
Expand Down

0 comments on commit 21d6131

Please sign in to comment.