Browse Source

Fix AbstractJackson2Encoder#canEncode

This commit ensures that the Jackson encoder is selected only
with supported mime types.

Issue: SPR-15910
pull/1507/merge
Sebastien Deleuze 7 years ago
parent
commit
5d4ee09d53
  1. 5
      spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java
  2. 3
      spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonEncoderTests.java

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

@ -79,9 +79,8 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple @@ -79,9 +79,8 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
@Override
public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) {
Class<?> clazz = elementType.resolve(Object.class);
return (Object.class == clazz) ||
!String.class.isAssignableFrom(elementType.resolve(clazz)) &&
getObjectMapper().canSerialize(clazz) && supportsMimeType(mimeType);
return supportsMimeType(mimeType) && (Object.class == clazz ||
(!String.class.isAssignableFrom(elementType.resolve(clazz)) && getObjectMapper().canSerialize(clazz)));
}
@Override

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

@ -61,6 +61,9 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa @@ -61,6 +61,9 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
// SPR-15464
assertTrue(this.encoder.canEncode(ResolvableType.NONE, null));
// SPR-15910
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Object.class), APPLICATION_OCTET_STREAM));
}
@Test // SPR-15866

Loading…
Cancel
Save