Skip to content

Commit 77895bd

Browse files
committed
DATAMONGO-2044 make ensureNotIterable actually check if object is iterable
1 parent 6728191 commit 77895bd

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,13 @@
151151
* @author Borislav Rangelov
152152
* @author duozhilin
153153
* @author Andreas Zink
154+
* @author Bartłomiej Mazur
154155
*/
155156
@SuppressWarnings("deprecation")
156157
public class MongoTemplate implements MongoOperations, ApplicationContextAware, IndexOperationsProvider {
157158

158159
private static final Logger LOGGER = LoggerFactory.getLogger(MongoTemplate.class);
159160
private static final WriteResultChecking DEFAULT_WRITE_RESULT_CHECKING = WriteResultChecking.NONE;
160-
private static final Collection<String> ITERABLE_CLASSES;
161-
162-
static {
163-
164-
Set<String> iterableClasses = new HashSet<>();
165-
iterableClasses.add(List.class.getName());
166-
iterableClasses.add(Collection.class.getName());
167-
iterableClasses.add(Iterator.class.getName());
168-
169-
ITERABLE_CLASSES = Collections.unmodifiableCollection(iterableClasses);
170-
}
171161

172162
private final MongoConverter mongoConverter;
173163
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
@@ -1129,7 +1119,7 @@ public <T> T insert(T objectToSave) {
11291119

11301120
Assert.notNull(objectToSave, "ObjectToSave must not be null!");
11311121

1132-
ensureNotIterable(objectToSave);
1122+
ensureNotAnArrayOrCollection(objectToSave);
11331123
return insert(objectToSave, operations.determineEntityCollectionName(objectToSave));
11341124
}
11351125

@@ -1144,13 +1134,13 @@ public <T> T insert(T objectToSave, String collectionName) {
11441134
Assert.notNull(objectToSave, "ObjectToSave must not be null!");
11451135
Assert.notNull(collectionName, "CollectionName must not be null!");
11461136

1147-
ensureNotIterable(objectToSave);
1137+
ensureNotAnArrayOrCollection(objectToSave);
11481138
return (T) doInsert(collectionName, objectToSave, this.mongoConverter);
11491139
}
11501140

1151-
protected void ensureNotIterable(@Nullable Object o) {
1141+
protected void ensureNotAnArrayOrCollection(@Nullable Object o) {
11521142
if (null != o) {
1153-
if (o.getClass().isArray() || ITERABLE_CLASSES.contains(o.getClass().getName())) {
1143+
if (o.getClass().isArray() || (o instanceof Collection) || (o instanceof Iterator)) {
11541144
throw new IllegalArgumentException("Cannot use a collection here.");
11551145
}
11561146
}

0 commit comments

Comments
 (0)