From 77895bda8d5868b5f19da9ade07a551d61c75ce8 Mon Sep 17 00:00:00 2001 From: GotoFinal Date: Tue, 7 Aug 2018 12:21:00 +0200 Subject: [PATCH] DATAMONGO-2044 make ensureNotIterable actually check if object is iterable --- .../data/mongodb/core/MongoTemplate.java | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 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 311b1712ef..cd821a032e 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 @@ -151,23 +151,13 @@ * @author Borislav Rangelov * @author duozhilin * @author Andreas Zink + * @author Bartłomiej Mazur */ @SuppressWarnings("deprecation") public class MongoTemplate implements MongoOperations, ApplicationContextAware, IndexOperationsProvider { private static final Logger LOGGER = LoggerFactory.getLogger(MongoTemplate.class); private static final WriteResultChecking DEFAULT_WRITE_RESULT_CHECKING = WriteResultChecking.NONE; - private static final Collection ITERABLE_CLASSES; - - static { - - Set iterableClasses = new HashSet<>(); - iterableClasses.add(List.class.getName()); - iterableClasses.add(Collection.class.getName()); - iterableClasses.add(Iterator.class.getName()); - - ITERABLE_CLASSES = Collections.unmodifiableCollection(iterableClasses); - } private final MongoConverter mongoConverter; private final MappingContext, MongoPersistentProperty> mappingContext; @@ -1129,7 +1119,7 @@ public T insert(T objectToSave) { Assert.notNull(objectToSave, "ObjectToSave must not be null!"); - ensureNotIterable(objectToSave); + ensureNotAnArrayOrCollection(objectToSave); return insert(objectToSave, operations.determineEntityCollectionName(objectToSave)); } @@ -1144,13 +1134,13 @@ public T insert(T objectToSave, String collectionName) { Assert.notNull(objectToSave, "ObjectToSave must not be null!"); Assert.notNull(collectionName, "CollectionName must not be null!"); - ensureNotIterable(objectToSave); + ensureNotAnArrayOrCollection(objectToSave); return (T) doInsert(collectionName, objectToSave, this.mongoConverter); } - protected void ensureNotIterable(@Nullable Object o) { + protected void ensureNotAnArrayOrCollection(@Nullable Object o) { if (null != o) { - if (o.getClass().isArray() || ITERABLE_CLASSES.contains(o.getClass().getName())) { + if (o.getClass().isArray() || (o instanceof Collection) || (o instanceof Iterator)) { throw new IllegalArgumentException("Cannot use a collection here."); } }