From 2af13c27a7e548d308e35f2e0d28c05cfbb6b62e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 23 Apr 2020 14:57:52 +0200 Subject: [PATCH] =?UTF-8?q?DATAMONGO-2529=20-=20Ensure=20that=20MappingMon?= =?UTF-8?q?goConverter.read(=E2=80=A6)=20is=20never=20called=20with=20null?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../data/mongodb/core/MongoTemplate.java | 14 +++++++------- .../core/convert/MappingMongoConverter.java | 11 +++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index ea858d2de..9d10d4fc0 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -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, } 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, @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, 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; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index 592046bdd..3ca5e2156 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/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 @Nullable @SuppressWarnings("unchecked") - private S read(TypeInformation type, @Nullable Bson bson, ObjectPath path) { + private S read(TypeInformation type, Bson bson, ObjectPath path) { - if (null == bson) { - return null; - } + Assert.notNull(bson, "Bson must not be null!"); TypeInformation typeToUse = typeMapper.readType(bson, type); Class rawType = typeToUse.getType(); @@ -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) (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);