From 1f07a3eb29768068e49f9d941720db99f98c4af7 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 | 9 ++++----- .../core/convert/MappingMongoConverter.java | 14 +++++++------- 2 files changed, 11 insertions(+), 12 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 915878978..5b6b3a80a 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 @@ -3029,12 +3029,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, @Nullable public T doWith(@Nullable Document object) { + T source = null; + if (null != object) { maybeEmitEvent(new AfterLoadEvent<>(object, type, collectionName)); + source = reader.read(type, object); } - T source = reader.read(type, object); - if (null != source) { maybeEmitEvent(new AfterConvertEvent<>(object, source, collectionName)); } @@ -3074,9 +3075,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, Class typeToRead = targetType.isInterface() || targetType.isAssignableFrom(entityType) ? entityType : targetType; - if (null != object) { - maybeEmitEvent(new AfterLoadEvent<>(object, targetType, collectionName)); - } + maybeEmitEvent(new AfterLoadEvent<>(object, targetType, collectionName)); Object source = reader.read(typeToRead, object); 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 e9eda4274..44d6f4921 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 @@ -200,11 +200,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(); @@ -1562,17 +1560,19 @@ 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); } - final T target = (T) read(type, document, path); - targeList.add(target); - if (target != null) { maybeEmitEvent(new AfterConvertEvent<>(document, target, collectionName)); } + + targeList.add(target); } return targeList;