From cde39008cf0da238cb59a0dbb7eaa92280e4306c Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 2 Mar 2021 08:48:30 +0100 Subject: [PATCH] Assert MongoTemplate rejects saving collection like entities like Lists or Iterator. Closes #3570. Original pull request: #3576. --- .../springframework/data/mongodb/core/MongoOperations.java | 7 +++++++ .../springframework/data/mongodb/core/MongoTemplate.java | 1 + .../data/mongodb/core/MongoTemplateUnitTests.java | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java index ffa301a28..0fe456aa7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java @@ -1319,9 +1319,12 @@ public interface MongoOperations extends FluentMongoOperations { * property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See * Spring's * Type Conversion" for more details. + *

+ * The {@literal objectToSave} must not be collection like. * * @param objectToSave the object to store in the collection. Must not be {@literal null}. * @return the saved object. + * @throws IllegalArgumentException in case the objectToSave is collection like. */ T save(T objectToSave); @@ -1337,10 +1340,14 @@ public interface MongoOperations extends FluentMongoOperations { * property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See Spring's Type * Conversion" for more details. + *

+ * The {@literal objectToSave} must not be collection like. + * * * @param objectToSave the object to store in the collection. Must not be {@literal null}. * @param collectionName name of the collection to store the object in. Must not be {@literal null}. * @return the saved object. + * @throws IllegalArgumentException in case the objectToSave is collection like. */ T save(T objectToSave, String collectionName); 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 5fbb05149..d8ac2d0a0 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 @@ -1376,6 +1376,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, Assert.notNull(objectToSave, "Object to save must not be null!"); Assert.hasText(collectionName, "Collection name must not be null or empty!"); + ensureNotCollectionLike(objectToSave); AdaptibleEntity source = operations.forEntity(objectToSave, mongoConverter.getConversionService()); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java index 74ee74b1f..7006091d1 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java @@ -2213,6 +2213,13 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests { .isThrownBy(() -> template.insert(new TypeImplementingIterator())); } + @Test // GH-3570 + void saveErrorsOnCollectionLikeObjects() { + + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> template.save(new ArrayList<>(Arrays.asList(1, 2, 3)), "myList")); + } + class AutogenerateableId { @Id BigInteger id;