Skip to content

DATAMONGO-2044 make ensureNotIterable actually check if object is iterable #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> ITERABLE_CLASSES;

static {

Set<String> 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<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
Expand Down Expand Up @@ -1129,7 +1119,7 @@ public <T> T insert(T objectToSave) {

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

ensureNotIterable(objectToSave);
ensureNotAnArrayOrCollection(objectToSave);
return insert(objectToSave, operations.determineEntityCollectionName(objectToSave));
}

Expand All @@ -1144,13 +1134,13 @@ public <T> 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.");
}
}
Expand Down