Skip to content

Commit 6f13837

Browse files
GotoFinalchristophstrobl
authored andcommitted
Update ensureNotIterable check for inserts.
Check if an object is an array or is an instance of collection or iterator instead of checking against collection names which is likely to never work. Closes spring-projects#2911 Original Pull Request: spring-projects#590
1 parent 1ca2f5c commit 6f13837

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

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

+6-16
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,12 @@
157157
* @author Roman Puchkovskiy
158158
* @author Yadhukrishna S Pai
159159
* @author Anton Barkan
160+
* @author Bartłomiej Mazur
160161
*/
161162
public class MongoTemplate implements MongoOperations, ApplicationContextAware, IndexOperationsProvider {
162163

163164
private static final Logger LOGGER = LoggerFactory.getLogger(MongoTemplate.class);
164165
private static final WriteResultChecking DEFAULT_WRITE_RESULT_CHECKING = WriteResultChecking.NONE;
165-
private static final Collection<String> ITERABLE_CLASSES;
166-
167-
static {
168-
169-
Set<String> iterableClasses = new HashSet<>();
170-
iterableClasses.add(List.class.getName());
171-
iterableClasses.add(Collection.class.getName());
172-
iterableClasses.add(Iterator.class.getName());
173-
174-
ITERABLE_CLASSES = Collections.unmodifiableCollection(iterableClasses);
175-
}
176166

177167
private final MongoConverter mongoConverter;
178168
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
@@ -1164,7 +1154,7 @@ public <T> T insert(T objectToSave) {
11641154

11651155
Assert.notNull(objectToSave, "ObjectToSave must not be null!");
11661156

1167-
ensureNotIterable(objectToSave);
1157+
ensureNotAnArrayOrCollection(objectToSave);
11681158
return insert(objectToSave, getCollectionName(ClassUtils.getUserClass(objectToSave)));
11691159
}
11701160

@@ -1179,13 +1169,13 @@ public <T> T insert(T objectToSave, String collectionName) {
11791169
Assert.notNull(objectToSave, "ObjectToSave must not be null!");
11801170
Assert.notNull(collectionName, "CollectionName must not be null!");
11811171

1182-
ensureNotIterable(objectToSave);
1172+
ensureNotAnArrayOrCollection(objectToSave);
11831173
return (T) doInsert(collectionName, objectToSave, this.mongoConverter);
11841174
}
11851175

1186-
protected void ensureNotIterable(@Nullable Object o) {
1187-
if (o != null) {
1188-
if (o.getClass().isArray() || ITERABLE_CLASSES.contains(o.getClass().getName())) {
1176+
protected void ensureNotAnArrayOrCollection(@Nullable Object o) {
1177+
if (null != o) {
1178+
if (o.getClass().isArray() || (o instanceof Collection) || (o instanceof Iterator)) {
11891179
throw new IllegalArgumentException("Cannot use a collection here.");
11901180
}
11911181
}

0 commit comments

Comments
 (0)