Prevent NullPointerException in AggregateReferenceDeserializerModifier.

In AggregateReferenceDeserializerModifier we now look up the original property name on the Jackson metadata before trying to resolve the property target type. We also explicitly guard against null values returned from that lookup and opt out of the customization in those cases.
This commit is contained in:
Oliver Drotbohm
2021-07-13 09:25:57 +02:00
parent 940a676e6d
commit e1dd82c927
2 changed files with 104 additions and 4 deletions

View File

@@ -33,6 +33,7 @@ import com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.deser.std.CollectionDeserializer;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.CollectionLikeType;
@@ -89,23 +90,28 @@ public class AggregateReferenceResolvingModule extends SimpleModule {
* @see com.fasterxml.jackson.databind.deser.BeanDeserializerModifier#updateBuilder(com.fasterxml.jackson.databind.DeserializationConfig, com.fasterxml.jackson.databind.BeanDescription, com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder)
*/
@Override
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc,
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription description,
BeanDeserializerBuilder builder) {
// Type is aggregate itself, already handled by AssociationUriResolvingDeserializerModifier
if (mappings.hasMappingFor(beanDesc.getBeanClass())) {
if (mappings.hasMappingFor(description.getBeanClass())) {
return builder;
}
TypeInformation<?> type = ClassTypeInformation.from(beanDesc.getBeanClass());
TypeInformation<?> type = ClassTypeInformation.from(description.getBeanClass());
ValueInstantiatorCustomizer customizer = new ValueInstantiatorCustomizer(builder.getValueInstantiator(), config);
Iterator<SettableBeanProperty> properties = builder.getProperties();
while (properties.hasNext()) {
SettableBeanProperty property = properties.next();
String originalPropertyName = coerceOriginalPropertyName(property, description);
TypeInformation<?> propertyType = type.getProperty(originalPropertyName);
if (propertyType == null) {
continue;
}
TypeInformation<?> propertyType = type.getProperty(property.getName());
TypeInformation<?> actualType = propertyType.getActualType();
if (!mappings.exportsMappingFor(actualType.getType())) {
@@ -134,5 +140,24 @@ public class AggregateReferenceResolvingModule extends SimpleModule {
return new CollectionDeserializer(collectionType, elementDeserializer, null, instantiator);
}
/**
* Tries to find the internal property name for a {@link SettableBeanProperty} that unfortunately does not allow
* accessing the original name anymore.
*
* @param property must not be {@literal null}.
* @param description must not be {@literal null}.
* @return
*/
private static String coerceOriginalPropertyName(SettableBeanProperty property, BeanDescription description) {
for (BeanPropertyDefinition properties : description.findProperties()) {
if (properties.hasName(property.getFullName())) {
return properties.getInternalName();
}
}
return property.getName();
}
}
}