Browse Source

DATAMONGO-2529 - Ensure that MappingMongoConverter.read(…) is never called with null.

Previously, various methods attempted to pass a null argument as source for the converter. The API is non-null and implementations relying on these constraints were easily breakable.

We now make sure that the source is never null.
pull/862/head
Mark Paluch 6 years ago
parent
commit
2af13c27a7
No known key found for this signature in database
GPG Key ID: 51A00FA751B91849
  1. 14
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  2. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

14
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

@ -239,7 +239,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, @@ -239,7 +239,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
this.projectionFactory = new SpelAwareProxyProjectionFactory();
this.operations = new EntityOperations(this.mongoConverter.getMappingContext());
this.propertyOperations = new PropertyOperations(this.mongoConverter.getMappingContext());
this.queryOperations = new QueryOperations(queryMapper, updateMapper, operations, propertyOperations, mongoDbFactory);
this.queryOperations = new QueryOperations(queryMapper, updateMapper, operations, propertyOperations,
mongoDbFactory);
// We always have a mapping context in the converter, whether it's a simple one or not
mappingContext = this.mongoConverter.getMappingContext();
@ -1824,7 +1825,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, @@ -1824,7 +1825,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
}
Document mappedSort = getMappedSortObject(query, domainType);
if(mappedSort != null && !mappedSort.isEmpty()) {
if (mappedSort != null && !mappedSort.isEmpty()) {
mapReduce = mapReduce.sort(getMappedSortObject(query, domainType));
}
@ -3122,12 +3123,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, @@ -3122,12 +3123,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
@Nullable
public T doWith(@Nullable Document document) {
T source = null;
if (document != null) {
maybeEmitEvent(new AfterLoadEvent<>(document, type, collectionName));
source = reader.read(type, document);
}
T source = reader.read(type, document);
if (source != null) {
maybeEmitEvent(new AfterConvertEvent<>(document, source, collectionName));
source = maybeCallAfterConvert(source, document, collectionName);
@ -3168,9 +3170,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, @@ -3168,9 +3170,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
Class<?> typeToRead = targetType.isInterface() || targetType.isAssignableFrom(entityType) ? entityType
: targetType;
if (document != null) {
maybeEmitEvent(new AfterLoadEvent<>(document, targetType, collectionName));
}
maybeEmitEvent(new AfterLoadEvent<>(document, targetType, collectionName));
Object source = reader.read(typeToRead, document);
Object result = targetType.isInterface() ? projectionFactory.createProjection(targetType, source) : source;

11
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

@ -252,11 +252,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App @@ -252,11 +252,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
@Nullable
@SuppressWarnings("unchecked")
private <S extends Object> S read(TypeInformation<S> type, @Nullable Bson bson, ObjectPath path) {
private <S extends Object> S read(TypeInformation<S> type, Bson bson, ObjectPath path) {
if (null == bson) {
return null;
}
Assert.notNull(bson, "Bson must not be null!");
TypeInformation<? extends S> typeToUse = typeMapper.readType(bson, type);
Class<? extends S> rawType = typeToUse.getType();
@ -1640,13 +1638,14 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App @@ -1640,13 +1638,14 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
for (Document document : referencedRawDocuments) {
T target = null;
if (document != null) {
maybeEmitEvent(
new AfterLoadEvent<>(document, (Class<T>) (rawType != null ? rawType : Object.class), collectionName));
target = (T) read(type, document, path);
}
T target = (T) read(type, document, path);
if (target != null) {
maybeEmitEvent(new AfterConvertEvent<>(document, target, collectionName));
target = maybeCallAfterConvert(target, document, collectionName);

Loading…
Cancel
Save