-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Milestone
Description
When the DeserializationFeature.FAIL_ON_INVALID_SUBTYPE is disabled the com.fasterxml.jackson.databind.JsonMappingException is thrown instead of just a null added to collection.
Consider the following code:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertNotNull;
public class JacksonBugTest {
ObjectMapper mapper ;
@Before
public void configureMapper() {
mapper = new ObjectMapper()
.disable(
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE,
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES
) ;
}
@Test
public void test() throws IOException {
String json = "{\"many\":[{\"sub1\":{\"a\":\"foo\"}},{\"sub2\":{\"b\":\"bar\"}}]}" ;
Good goodResult = mapper.readValue(json, Good.class) ;
assertNotNull("failed conversion", goodResult) ;
Bad badResult = mapper.readValue(json, Bad.class) ;
}
public static class Bad {
@JsonProperty("many")
List<BadItem> many ;
}
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT
)
@JsonSubTypes(
@JsonSubTypes.Type(name="sub1", value = BadSub1.class)
)
public static class BadItem {}
public static class BadSub1 extends BadItem {
@JsonProperty("a")
String a ;
}
public static class Good {
@JsonProperty("many")
List<GoodItem> many ;
}
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT
)
@JsonSubTypes({
@JsonSubTypes.Type(name="sub1", value = GoodSub1.class),
@JsonSubTypes.Type(name="sub2", value = GoodSub2.class)
})
public static class GoodItem {}
public static class GoodSub1 extends GoodItem {
@JsonProperty("a")
String a ;
}
public static class GoodSub2 extends GoodItem {
@JsonProperty("b")
String b ;
}
}
Metadata
Metadata
Assignees
Labels
No labels