Skip to content

Commit

Permalink
Fixed #2392
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 1, 2019
1 parent 2d572a8 commit 712745c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Project: jackson-databind

#2331: `JsonMappingException` through nested getter with generic wildcard return type
(reported by sunchezz89@github)
#2392: `BeanDeserializerModifier.modifyDeserializer()` not applied to custom bean deserializers
(reported by andreasbaus@github)
#2393: `TreeTraversingParser.getLongValue()` incorrectly checks `canConvertToInt()`
(reported by RabbidDog@github)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ public JsonDeserializer<?> createArrayDeserializer(DeserializationContext ctxt,
}
deser = new ObjectArrayDeserializer(type, contentDeser, elemTypeDeser);
}
// and then new with 2.2: ability to post-process it too (Issue#120)
// and then new with 2.2: ability to post-process it too (databind#120)
if (_factoryConfig.hasDeserializerModifiers()) {
for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) {
deser = mod.modifyArrayDeserializer(config, type, beanDesc, deser);
Expand All @@ -1112,9 +1112,9 @@ public JsonDeserializer<?> createArrayDeserializer(DeserializationContext ctxt,
}

/*
/**********************************************************
/**********************************************************************
/* JsonDeserializerFactory impl: Collection(-like) deserializers
/**********************************************************
/**********************************************************************
*/

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,23 @@ public DeserializerFactory withConfig(DeserializerFactoryConfig config)
* deserializer for types other than Collections, Maps, arrays and
* enums.
*/
@SuppressWarnings("unchecked")
@Override
public JsonDeserializer<Object> createBeanDeserializer(DeserializationContext ctxt,
JavaType type, BeanDescription beanDesc)
throws JsonMappingException
{
final DeserializationConfig config = ctxt.getConfig();
// We may also have custom overrides:
JsonDeserializer<Object> custom = _findCustomBeanDeserializer(type, config, beanDesc);
if (custom != null) {
return custom;
JsonDeserializer<?> deser = _findCustomBeanDeserializer(type, config, beanDesc);
if (deser != null) {
// [databind#2392]
if (_factoryConfig.hasDeserializerModifiers()) {
for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) {
deser = mod.modifyDeserializer(ctxt.getConfig(), beanDesc, deser);
}
}
return (JsonDeserializer<Object>) deser;
}
/* One more thing to check: do we have an exception type
* (Throwable or its sub-classes)? If so, need slightly
Expand All @@ -121,10 +128,9 @@ public JsonDeserializer<Object> createBeanDeserializer(DeserializationContext ct
}
}
// Otherwise, may want to check handlers for standard types, from superclass:
@SuppressWarnings("unchecked")
JsonDeserializer<Object> deser = (JsonDeserializer<Object>) findStdDeserializer(ctxt, type, beanDesc);
deser = findStdDeserializer(ctxt, type, beanDesc);
if (deser != null) {
return deser;
return (JsonDeserializer<Object>)deser;
}

// Otherwise: could the class be a Bean class? If not, bail out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ public BeanDeserializerBuilder updateBuilder(DeserializationConfig config,
* Method called by {@link BeanDeserializerFactory} after constructing default
* bean deserializer instance with properties collected and ordered earlier.
* Implementations can modify or replace given deserializer and return deserializer
* to use. Note that although initial deserializer being passed is of type
* to use. Note that although initial deserializer being passed is usually of type
* {@link BeanDeserializer}, modifiers may return deserializers of other types;
* and this is why implementations must check for type before casting.
*<p>
* Since 2.10 this is also called for custom deserializers for types not deemed to
* be of any more specific (reference, enum, array, collection(-like), map(-like),
* node type)
*/
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
Expand All @@ -97,6 +101,9 @@ public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
*/

/**
* Method called by {@link BeanDeserializerFactory} after constructing default
* enum type deserializer instance.
*
* @since 2.2
*/
public JsonDeserializer<?> modifyEnumDeserializer(DeserializationConfig config,
Expand All @@ -105,6 +112,9 @@ public JsonDeserializer<?> modifyEnumDeserializer(DeserializationConfig config,
}

/**
* Method called by {@link BeanDeserializerFactory} after constructing default
* {@link ReferenceType} deserializer instance.
*
* @since 2.7
*/
public JsonDeserializer<?> modifyReferenceDeserializer(DeserializationConfig config,
Expand Down Expand Up @@ -135,6 +145,9 @@ public JsonDeserializer<?> modifyArrayDeserializer(DeserializationConfig config,
}

/**
* Method called by {@link BeanDeserializerFactory} after constructing default
* {@link CollectionType} deserializer instance.
*
* @since 2.2
*/
public JsonDeserializer<?> modifyCollectionDeserializer(DeserializationConfig config,
Expand All @@ -143,6 +156,9 @@ public JsonDeserializer<?> modifyCollectionDeserializer(DeserializationConfig co
}

/**
* Method called by {@link BeanDeserializerFactory} after constructing default
* {@link CollectionLikeType} deserializer instance.
*
* @since 2.2
*/
public JsonDeserializer<?> modifyCollectionLikeDeserializer(DeserializationConfig config,
Expand All @@ -151,6 +167,9 @@ public JsonDeserializer<?> modifyCollectionLikeDeserializer(DeserializationConfi
}

/**
* Method called by {@link BeanDeserializerFactory} after constructing default
* {@link MapType} deserializer instance.
*
* @since 2.2
*/
public JsonDeserializer<?> modifyMapDeserializer(DeserializationConfig config,
Expand All @@ -159,6 +178,9 @@ public JsonDeserializer<?> modifyMapDeserializer(DeserializationConfig config,
}

/**
* Method called by {@link BeanDeserializerFactory} after constructing default
* {@link MapLikeType} deserializer instance.
*
* @since 2.2
*/
public JsonDeserializer<?> modifyMapLikeDeserializer(DeserializationConfig config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,25 @@ public void testDelegatingDeserializer() throws Exception
String str = mapper.readValue(quote("foo"), String.class);
assertEquals("MY:foo", str);
}

// [databind#2392]
public void testModifyingCustomDeserializer() throws Exception
{
ObjectMapper mapper = jsonMapperBuilder()
.addModule(new SimpleModule()
.setDeserializerModifier(new BeanDeserializerModifier() {
@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
if (deserializer instanceof DummyDeserializer<?>) {
return new DummyDeserializer<String>("FOOBAR", String.class);
}
return deserializer;
}
})
.addDeserializer(String.class, new DummyDeserializer<String>("dummy", String.class))
).build();
String str = mapper.readValue(quote("foo"), String.class);
assertEquals("FOOBAR", str);
}
}

0 comments on commit 712745c

Please sign in to comment.