Refine Jackson canEncode() / canDecode() implementation
This commit refine Jackson canEncode() / canDecode() implementation by using ObjectMapper canSerialize() / canDeserialize() methods. Issue: SPR-14748
This commit is contained in:
@@ -42,7 +42,7 @@ public interface Decoder<T> {
|
|||||||
* Whether the decoder supports the given target element type and the MIME
|
* Whether the decoder supports the given target element type and the MIME
|
||||||
* type of the source stream.
|
* type of the source stream.
|
||||||
* @param elementType the target element type for the output stream
|
* @param elementType the target element type for the output stream
|
||||||
* @param mimeType the mime type associated with the stream to decode
|
* @param mimeType the mime type associated with the stream to decode, can be {@code null} if not specified.
|
||||||
* @return {@code true} if supported, {@code false} otherwise
|
* @return {@code true} if supported, {@code false} otherwise
|
||||||
*/
|
*/
|
||||||
boolean canDecode(ResolvableType elementType, MimeType mimeType);
|
boolean canDecode(ResolvableType elementType, MimeType mimeType);
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public interface Encoder<T> {
|
|||||||
* Whether the encoder supports the given source element type and the MIME
|
* Whether the encoder supports the given source element type and the MIME
|
||||||
* type for the output stream.
|
* type for the output stream.
|
||||||
* @param elementType the type of elements in the source stream
|
* @param elementType the type of elements in the source stream
|
||||||
* @param mimeType the MIME type for the output stream
|
* @param mimeType the MIME type for the output stream, can be {@code null} if not specified.
|
||||||
* @return {@code true} if supported, {@code false} otherwise
|
* @return {@code true} if supported, {@code false} otherwise
|
||||||
*/
|
*/
|
||||||
boolean canEncode(ResolvableType elementType, MimeType mimeType);
|
boolean canEncode(ResolvableType elementType, MimeType mimeType);
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import java.util.Map;
|
|||||||
import com.fasterxml.jackson.databind.JavaType;
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.ObjectReader;
|
import com.fasterxml.jackson.databind.ObjectReader;
|
||||||
|
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
@@ -63,10 +64,9 @@ public class Jackson2JsonDecoder extends AbstractJackson2Codec implements Decode
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canDecode(ResolvableType elementType, MimeType mimeType) {
|
public boolean canDecode(ResolvableType elementType, MimeType mimeType) {
|
||||||
if (mimeType == null) {
|
JavaType javaType = this.mapper.getTypeFactory().constructType(elementType.getType());
|
||||||
return true;
|
return this.mapper.canDeserialize(javaType) &&
|
||||||
}
|
(mimeType == null || JSON_MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
|
||||||
return JSON_MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -68,10 +68,8 @@ public class Jackson2JsonEncoder extends AbstractJackson2Codec implements Encode
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canEncode(ResolvableType elementType, MimeType mimeType) {
|
public boolean canEncode(ResolvableType elementType, MimeType mimeType) {
|
||||||
if (mimeType == null) {
|
return this.mapper.canSerialize(elementType.getRawClass()) &&
|
||||||
return true;
|
(mimeType == null || JSON_MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
|
||||||
}
|
|
||||||
return JSON_MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -46,9 +46,10 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
|
|||||||
@Test
|
@Test
|
||||||
public void canDecode() {
|
public void canDecode() {
|
||||||
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
|
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
|
||||||
|
ResolvableType type = ResolvableType.forClass(Pojo.class);
|
||||||
assertTrue(decoder.canDecode(null, MediaType.APPLICATION_JSON));
|
assertTrue(decoder.canDecode(type, MediaType.APPLICATION_JSON));
|
||||||
assertFalse(decoder.canDecode(null, MediaType.APPLICATION_XML));
|
assertTrue(decoder.canDecode(type, null));
|
||||||
|
assertFalse(decoder.canDecode(type, MediaType.APPLICATION_XML));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
|||||||
import org.springframework.core.io.buffer.DataBuffer;
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.codec.Pojo;
|
import org.springframework.http.codec.Pojo;
|
||||||
|
import org.springframework.http.codec.ServerSentEvent;
|
||||||
import org.springframework.tests.TestSubscriber;
|
import org.springframework.tests.TestSubscriber;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
@@ -45,8 +46,12 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canEncode() {
|
public void canEncode() {
|
||||||
assertTrue(this.encoder.canEncode(null, MediaType.APPLICATION_JSON));
|
ResolvableType pojoType = ResolvableType.forClass(Pojo.class);
|
||||||
assertFalse(this.encoder.canEncode(null, MediaType.APPLICATION_XML));
|
assertTrue(this.encoder.canEncode(pojoType, MediaType.APPLICATION_JSON));
|
||||||
|
assertTrue(this.encoder.canEncode(pojoType, null));
|
||||||
|
assertFalse(this.encoder.canEncode(pojoType, MediaType.APPLICATION_XML));
|
||||||
|
ResolvableType sseType = ResolvableType.forClass(ServerSentEvent.class);
|
||||||
|
assertFalse(this.encoder.canEncode(sseType, MediaType.APPLICATION_JSON));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user