Skip to content

Commit 3ee81a6

Browse files
committed
Further cleanup of #1551
1 parent 791de8f commit 3ee81a6

File tree

5 files changed

+39
-33
lines changed

5 files changed

+39
-33
lines changed

src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ public Object handleUnexpectedToken(Class<?> instClass, JsonToken t,
11231123
* actual type; usually since there is no mapping defined.
11241124
* Default implementation will try to call {@link DeserializationProblemHandler#handleUnknownTypeId}
11251125
* on configured handlers, if any, to allow for recovery; if recovery does not
1126-
* succeed, will throw exception constructed with {@link #unknownTypeIdException}.
1126+
* succeed, will throw exception constructed with {@link #invalidTypeIdException}.
11271127
*
11281128
* @param baseType Base type from which resolution starts
11291129
* @param id Type id that could not be converted

src/main/java/com/fasterxml/jackson/databind/deser/AbstractDeserializer.java

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,32 @@ public static AbstractDeserializer constructForNonPOJO(BeanDescription beanDesc)
103103
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
104104
BeanProperty property) throws JsonMappingException
105105
{
106-
// First: may have an override for Object Id:
107106
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
108-
final AnnotatedMember accessor = (property == null || intr == null)
109-
? null : property.getMember();
110-
if (accessor != null && intr != null) {
111-
ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
112-
if (objectIdInfo != null) { // some code duplication here as well (from BeanDeserializerFactory)
113-
// 2.1: allow modifications by "id ref" annotations as well:
114-
objectIdInfo = intr.findObjectReferenceInfo(accessor, objectIdInfo);
115-
116-
Class<?> implClass = objectIdInfo.getGeneratorType();
117-
// 02-May-2017, tatu: Alas, properties are NOT available for abstract classes; can not
118-
// support this particular type
119-
if (implClass == ObjectIdGenerators.PropertyGenerator.class) {
120-
ctxt.reportMappingException(
107+
if (property != null && intr != null) {
108+
final AnnotatedMember accessor = property.getMember();
109+
if (accessor != null) {
110+
ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
111+
if (objectIdInfo != null) { // some code duplication here as well (from BeanDeserializerFactory)
112+
// 2.1: allow modifications by "id ref" annotations as well:
113+
objectIdInfo = intr.findObjectReferenceInfo(accessor, objectIdInfo);
114+
Class<?> implClass = objectIdInfo.getGeneratorType();
115+
// 02-May-2017, tatu: Alas, properties are NOT available for abstract classes; can not
116+
// support this particular type. Yet.
117+
if (implClass == ObjectIdGenerators.PropertyGenerator.class) {
118+
ctxt.reportBadDefinition(_baseType, String.format(
121119
"Invalid Object Id definition for abstract type %s: can not use `PropertyGenerator` on polymorphic types using property annotation",
122-
handledType().getName());
120+
handledType().getName()));
121+
}
122+
ObjectIdResolver resolver = ctxt.objectIdResolverInstance(accessor, objectIdInfo);
123+
JavaType type = ctxt.constructType(implClass);
124+
JavaType idType = ctxt.getTypeFactory().findTypeParameters(type, ObjectIdGenerator.class)[0];
125+
SettableBeanProperty idProp = null;
126+
ObjectIdGenerator<?> idGen = ctxt.objectIdGeneratorInstance(accessor, objectIdInfo);
127+
JsonDeserializer<?> deser = ctxt.findRootValueDeserializer(idType);
128+
ObjectIdReader oir = ObjectIdReader.construct(idType, objectIdInfo.getPropertyName(),
129+
idGen, deser, idProp, resolver);
130+
return new AbstractDeserializer(this, oir);
123131
}
124-
ObjectIdResolver resolver = ctxt.objectIdResolverInstance(accessor, objectIdInfo);
125-
JavaType type = ctxt.constructType(implClass);
126-
JavaType idType = ctxt.getTypeFactory().findTypeParameters(type, ObjectIdGenerator.class)[0];
127-
SettableBeanProperty idProp = null;
128-
ObjectIdGenerator<?> idGen = ctxt.objectIdGeneratorInstance(accessor, objectIdInfo);
129-
JsonDeserializer<?> deser = ctxt.findRootValueDeserializer(idType);
130-
ObjectIdReader oir = ObjectIdReader.construct(idType, objectIdInfo.getPropertyName(),
131-
idGen, deser, idProp, resolver);
132-
return new AbstractDeserializer(this, oir);
133132
}
134133
}
135134
// either way, need to resolve serializer:
@@ -207,10 +206,8 @@ public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
207206
&& _objectIdReader.isValidReferencePropertyName(p.getCurrentName(), p)) {
208207
return _deserializeFromObjectId(p, ctxt);
209208
}
210-
211209
}
212210
}
213-
214211
// First: support "natural" values (which are always serialized without type info!)
215212
Object result = _deserializeIfNatural(p, ctxt);
216213
if (result != null) {
@@ -236,7 +233,7 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt)
236233
/* Internal methods
237234
/**********************************************************
238235
*/
239-
236+
240237
protected Object _deserializeIfNatural(JsonParser p, DeserializationContext ctxt) throws IOException
241238
{
242239
/* There is a chance we might be "natural" types

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
668668
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
669669
final AnnotatedMember accessor = (property == null || intr == null)
670670
? null : property.getMember();
671-
if (accessor != null && intr != null) {
671+
if (_neitherNull(accessor, intr)) {
672672
ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
673673
if (objectIdInfo != null) { // some code duplication here as well (from BeanDeserializerFactory)
674674
// 2.1: allow modifications by "id ref" annotations as well:
@@ -1040,7 +1040,7 @@ public SettableBeanProperty findProperty(String propertyName)
10401040
{
10411041
SettableBeanProperty prop = (_beanProperties == null) ?
10421042
null : _beanProperties.find(propertyName);
1043-
if (prop == null && _propertyBasedCreator != null) {
1043+
if (_neitherNull(prop, _propertyBasedCreator)) {
10441044
prop = _propertyBasedCreator.findCreatorProperty(propertyName);
10451045
}
10461046
return prop;
@@ -1060,7 +1060,7 @@ public SettableBeanProperty findProperty(int propertyIndex)
10601060
{
10611061
SettableBeanProperty prop = (_beanProperties == null) ?
10621062
null : _beanProperties.find(propertyIndex);
1063-
if (prop == null && _propertyBasedCreator != null) {
1063+
if (_neitherNull(prop, _propertyBasedCreator)) {
10641064
prop = _propertyBasedCreator.findCreatorProperty(propertyIndex);
10651065
}
10661066
return prop;

src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ protected JsonDeserializer<?> findConvertingContentDeserializer(DeserializationC
948948
throws JsonMappingException
949949
{
950950
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
951-
if (intr != null && prop != null) {
951+
if (_neitherNull(intr, prop)) {
952952
AnnotatedMember member = prop.getMember();
953953
if (member != null) {
954954
Object convDef = intr.findDeserializationContentConverter(member);
@@ -1168,6 +1168,13 @@ protected void _verifyEndArrayForSingle(JsonParser p, DeserializationContext ctx
11681168
/**********************************************************
11691169
*/
11701170

1171+
/**
1172+
* @since 2.9
1173+
*/
1174+
protected final static boolean _neitherNull(Object a, Object b) {
1175+
return (a != null) && (b != null);
1176+
}
1177+
11711178
/**
11721179
* @since 2.9
11731180
*/

src/test/java/com/fasterxml/jackson/failing/PolymorphicWithObjectId1551Test.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonTypeInfo;
66
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
77
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
89

910
public class PolymorphicWithObjectId1551Test extends BaseMapTest
1011
{
@@ -44,7 +45,8 @@ public void testWithAbstractUsingProp() throws Exception {
4445
/*VehicleOwnerViaProp[] deserialized = */
4546
objectMapper.readValue(serialized, VehicleOwnerViaProp[].class);
4647
fail("Should not pass");
47-
} catch (JsonMappingException e) {
48+
} catch (InvalidDefinitionException e) {
49+
assertEquals(Vehicle.class, e.getType().getRawClass());
4850
verifyException(e, "Invalid Object Id definition for abstract type");
4951
}
5052
// assertEquals(2, deserialized.length);

0 commit comments

Comments
 (0)