Allow customization for ObjectReader and ObjectWriter
Allows customization from subclasses of AbstractJackson2HttpMessageConverter, AbstractJackson2Decoder, and AbstractJackson2Encoder See gh-28401
This commit is contained in:
committed by
rstoyanchev
parent
74df50c906
commit
27b5d141e2
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.codec.json;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectReader;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.testfixture.codec.AbstractDecoderTests;
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
/**
|
||||
* Unit tests for a customized {@link Jackson2JsonDecoder}.
|
||||
*
|
||||
* @author Jason Laber
|
||||
*/
|
||||
public class CustomizedJackson2JsonDecoderTests extends AbstractDecoderTests<Jackson2JsonDecoder> {
|
||||
|
||||
public CustomizedJackson2JsonDecoderTests() {
|
||||
super(new Jackson2JsonDecoderWithCustomization());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void canDecode() throws Exception {
|
||||
// Not Testing, covered under Jackson2JsonDecoderTests
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void decode() throws Exception {
|
||||
Flux<DataBuffer> input = Flux.concat(stringBuffer("{\"property\":\"Value1\"}"));
|
||||
|
||||
testDecodeAll(input, MyCustomizedDecoderBean.class, step -> step
|
||||
.expectNextMatches(obj -> obj.getProperty() == MyCustomDecoderEnum.VAL1)
|
||||
.verifyComplete());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void decodeToMono() throws Exception {
|
||||
Mono<DataBuffer> input = stringBuffer("{\"property\":\"Value2\"}");
|
||||
|
||||
ResolvableType elementType = ResolvableType.forClass(MyCustomizedDecoderBean.class);
|
||||
|
||||
testDecodeToMono(input, elementType, step -> step
|
||||
.expectNextMatches(obj -> ((MyCustomizedDecoderBean)obj).getProperty() == MyCustomDecoderEnum.VAL2)
|
||||
.expectComplete()
|
||||
.verify(), null, null);
|
||||
}
|
||||
|
||||
private Mono<DataBuffer> stringBuffer(String value) {
|
||||
return stringBuffer(value, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private Mono<DataBuffer> stringBuffer(String value, Charset charset) {
|
||||
return Mono.defer(() -> {
|
||||
byte[] bytes = value.getBytes(charset);
|
||||
DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
|
||||
buffer.write(bytes);
|
||||
return Mono.just(buffer);
|
||||
});
|
||||
}
|
||||
|
||||
public static class MyCustomizedDecoderBean {
|
||||
|
||||
private MyCustomDecoderEnum property;
|
||||
|
||||
public MyCustomDecoderEnum getProperty() {
|
||||
return property;
|
||||
}
|
||||
|
||||
public void setProperty(MyCustomDecoderEnum property) {
|
||||
this.property = property;
|
||||
}
|
||||
}
|
||||
|
||||
public enum MyCustomDecoderEnum {
|
||||
VAL1,
|
||||
VAL2;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this == VAL1 ? "Value1" : "Value2";
|
||||
}
|
||||
}
|
||||
|
||||
private static class Jackson2JsonDecoderWithCustomization extends Jackson2JsonDecoder {
|
||||
|
||||
@Override
|
||||
protected Mono<ObjectReader> customizeReaderFromStream(ObjectReader reader, MimeType mimeType, ResolvableType elementType, Map<String, Object> hints) {
|
||||
return Mono.just(reader.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ObjectReader customizeReader(ObjectReader reader, MimeType mimeType, ResolvableType elementType, Map<String, Object> hints) {
|
||||
return reader.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.codec.json;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.core.testfixture.codec.AbstractEncoderTests;
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
import static org.springframework.http.MediaType.APPLICATION_NDJSON;
|
||||
|
||||
/**
|
||||
* Unit tests for a customized {@link Jackson2JsonEncoder}.
|
||||
*
|
||||
* @author Jason Laber
|
||||
*/
|
||||
public class CustomizedJackson2JsonEncoderTests extends AbstractEncoderTests<Jackson2JsonEncoder> {
|
||||
|
||||
public CustomizedJackson2JsonEncoderTests() {
|
||||
super(new Jackson2JsonEncoderWithCustomization());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void canEncode() throws Exception {
|
||||
// Not Testing, covered under Jackson2JsonEncoderTests
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void encode() throws Exception {
|
||||
Flux<MyCustomizedEncoderBean> input = Flux.just(
|
||||
new MyCustomizedEncoderBean(MyCustomEncoderEnum.VAL1),
|
||||
new MyCustomizedEncoderBean(MyCustomEncoderEnum.VAL2)
|
||||
);
|
||||
|
||||
testEncodeAll(input, ResolvableType.forClass(MyCustomizedEncoderBean.class), APPLICATION_NDJSON, null, step -> step
|
||||
.consumeNextWith(expectString("{\"property\":\"Value1\"}\n"))
|
||||
.consumeNextWith(expectString("{\"property\":\"Value2\"}\n"))
|
||||
.verifyComplete()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeNonStream() {
|
||||
Flux<MyCustomizedEncoderBean> input = Flux.just(
|
||||
new MyCustomizedEncoderBean(MyCustomEncoderEnum.VAL1),
|
||||
new MyCustomizedEncoderBean(MyCustomEncoderEnum.VAL2)
|
||||
);
|
||||
|
||||
testEncode(input, MyCustomizedEncoderBean.class, step -> step
|
||||
.consumeNextWith(expectString("[" +
|
||||
"{\"property\":\"Value1\"}," +
|
||||
"{\"property\":\"Value2\"}]")
|
||||
.andThen(DataBufferUtils::release))
|
||||
.verifyComplete());
|
||||
}
|
||||
|
||||
public static class MyCustomizedEncoderBean {
|
||||
|
||||
private MyCustomEncoderEnum property;
|
||||
|
||||
public MyCustomizedEncoderBean(MyCustomEncoderEnum property) {
|
||||
this.property = property;
|
||||
}
|
||||
|
||||
public MyCustomEncoderEnum getProperty() {
|
||||
return property;
|
||||
}
|
||||
|
||||
public void setProperty(MyCustomEncoderEnum property) {
|
||||
this.property = property;
|
||||
}
|
||||
}
|
||||
|
||||
public enum MyCustomEncoderEnum {
|
||||
VAL1,
|
||||
VAL2;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this == VAL1 ? "Value1" : "Value2";
|
||||
}
|
||||
}
|
||||
|
||||
private static class Jackson2JsonEncoderWithCustomization extends Jackson2JsonEncoder {
|
||||
|
||||
@Override
|
||||
protected Mono<ObjectWriter> customizeWriterFromStream(ObjectWriter writer, MimeType mimeType, ResolvableType elementType, Map<String, Object> hints) {
|
||||
return Mono.just(writer.with(SerializationFeature.WRITE_ENUMS_USING_TO_STRING));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ObjectWriter customizeWriter(ObjectWriter writer, MimeType mimeType, ResolvableType elementType, Map<String, Object> hints) {
|
||||
return writer.with(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,12 @@ import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectReader;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
@@ -528,6 +532,39 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content-type").isEqualTo(contentType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWithCustomized() throws IOException {
|
||||
MappingJackson2HttpMessageConverterWithCustomization customizedConverter =
|
||||
new MappingJackson2HttpMessageConverterWithCustomization();
|
||||
String body = "{\"property\":\"Value1\"}";
|
||||
MockHttpInputMessage inputMessage1 = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
MockHttpInputMessage inputMessage2 = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
|
||||
inputMessage1.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
inputMessage2.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
|
||||
assertThatExceptionOfType(HttpMessageNotReadableException.class)
|
||||
.isThrownBy(() -> converter.read(MyCustomizedBean.class, inputMessage1));
|
||||
|
||||
MyCustomizedBean customizedResult = (MyCustomizedBean) customizedConverter.read(MyCustomizedBean.class, inputMessage2);
|
||||
assertThat(customizedResult.getProperty()).isEqualTo(MyCustomEnum.VAL1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeWithCustomized() throws IOException {
|
||||
MappingJackson2HttpMessageConverterWithCustomization customizedConverter =
|
||||
new MappingJackson2HttpMessageConverterWithCustomization();
|
||||
MockHttpOutputMessage outputMessage1 = new MockHttpOutputMessage();
|
||||
MockHttpOutputMessage outputMessage2 = new MockHttpOutputMessage();
|
||||
MyCustomizedBean body = new MyCustomizedBean();
|
||||
body.setProperty(MyCustomEnum.VAL2);
|
||||
converter.write(body, null, outputMessage1);
|
||||
customizedConverter.write(body, null, outputMessage2);
|
||||
String result1 = outputMessage1.getBodyAsString(StandardCharsets.UTF_8);
|
||||
assertThat(result1.contains("\"property\":\"VAL2\"")).isTrue();
|
||||
String result2 = outputMessage2.getBodyAsString(StandardCharsets.UTF_8);
|
||||
assertThat(result2.contains("\"property\":\"Value2\"")).isTrue();
|
||||
}
|
||||
|
||||
|
||||
interface MyInterface {
|
||||
|
||||
@@ -537,7 +574,7 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
|
||||
|
||||
public static class MyBase implements MyInterface{
|
||||
public static class MyBase implements MyInterface {
|
||||
|
||||
private String string;
|
||||
|
||||
@@ -713,4 +750,40 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
}
|
||||
|
||||
public static class MyCustomizedBean {
|
||||
|
||||
private MyCustomEnum property;
|
||||
|
||||
public MyCustomEnum getProperty() {
|
||||
return property;
|
||||
}
|
||||
|
||||
public void setProperty(MyCustomEnum property) {
|
||||
this.property = property;
|
||||
}
|
||||
}
|
||||
|
||||
public enum MyCustomEnum {
|
||||
VAL1,
|
||||
VAL2;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this == VAL1 ? "Value1" : "Value2";
|
||||
}
|
||||
}
|
||||
|
||||
private static class MappingJackson2HttpMessageConverterWithCustomization extends MappingJackson2HttpMessageConverter {
|
||||
|
||||
@Override
|
||||
protected ObjectReader customizeReader(ObjectReader reader, JavaType javaType) {
|
||||
return reader.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ObjectWriter customizeWriter(ObjectWriter writer, JavaType javaType, MediaType contentType) {
|
||||
return writer.with(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user