Skip to content

Commit

Permalink
Remove one unnecessary change (wrt Map defaulting), add a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 12, 2024
1 parent 8efc34d commit 7f985f0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -966,12 +966,18 @@ public JsonDeserializer<?> createMapDeserializer(DeserializationContext ctxt,
*/
if (deser == null) {
if (type.isInterface() || type.isAbstract()) {
MapType fallback = _mapAbstractMapType(type, config);
if (fallback != null) {
type = (MapType) fallback;
MapType implType = _mapAbstractMapType(type, config);
if (implType != null) {
type = (MapType) implType;
mapClass = type.getRawClass();
// But if so, also need to re-check creators...
beanDesc = config.introspectForCreation(type);
} else {
// [databind#292]: Actually, may be fine, but only if polymorphic deser enabled
if (type.getTypeHandler() == null) {
throw new IllegalArgumentException("Cannot find a deserializer for non-concrete Map type "+type);
}
deser = AbstractDeserializer.constructForNonPOJO(beanDesc);
}
} else {
// 10-Jan-2017, tatu: `java.util.Collections` types need help:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// [databind#4783] Test to verify that JsonMerge also works for custom list
@SuppressWarnings("serial")
public class CustomCollectionMerge4783Test
extends DatabindTestUtil
extends DatabindTestUtil
{
static class MyArrayListJDK<T> extends ArrayList<T> { }

Expand All @@ -33,6 +33,11 @@ interface MyListCustom<T> extends List<T> { }

static class MyArrayListCustom<T> extends ArrayList<T> implements MyListCustom<T> { }

static abstract class MyAbstractStringList extends ArrayList<String> {
MyAbstractStringList() { super(); }
MyAbstractStringList(int i) { super(); }
}

static class MergeCustomStringList {
@JsonMerge
@JsonProperty
Expand Down Expand Up @@ -61,7 +66,7 @@ static class MergeMyCustomPojoList {
static class NonMergeCustomStringList {
public MyListCustom<String> values;
}

public static class CustomPojo {
public String name;
public int age;
Expand Down Expand Up @@ -113,9 +118,11 @@ void testCustomMapperReadingPojoArrayList() throws Exception {
assertEquals(3, result.values.size());
}

// Failure-handling testing important too
// // // And then failure cases

// Fail can't construct Collection interface unless there's maaping
@Test
void testNonMergeAbstractListFail() throws Exception {
void failNonMergeInterfaceList() throws Exception {
try {
MAPPER.readValue("{\"values\":[\"x\"]}", NonMergeCustomStringList.class);
fail("Should not pass");
Expand All @@ -126,4 +133,16 @@ void testNonMergeAbstractListFail() throws Exception {
}
}

// Fail can't construct abstract types
@Test
void failNonMergeAbstractList() throws Exception {
try {
MAPPER.readValue("[]", MyAbstractStringList.class);
fail("Should not pass");
} catch (InvalidDefinitionException e) {
verifyException(e, String.format(
"Cannot construct instance of `%s` (no Creators",
MyAbstractStringList.class.getName()));
}
}
}

0 comments on commit 7f985f0

Please sign in to comment.