Browse Source

Assert MongoTemplate rejects saving collection like entities like Lists or Iterator.

Closes #3570.
Original pull request: #3576.
pull/3581/head
Christoph Strobl 5 years ago committed by Mark Paluch
parent
commit
cde39008cf
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java
  2. 1
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  3. 7
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java

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

@ -1319,9 +1319,12 @@ public interface MongoOperations extends FluentMongoOperations { @@ -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
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation" > Spring's
* Type Conversion"</a> for more details.
* <p />
* 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> T save(T objectToSave);
@ -1337,10 +1340,14 @@ public interface MongoOperations extends FluentMongoOperations { @@ -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 <a
* https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation">Spring's Type
* Conversion"</a> for more details.
* <p />
* 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> T save(T objectToSave, String collectionName);

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

@ -1376,6 +1376,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, @@ -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<T> source = operations.forEntity(objectToSave, mongoConverter.getConversionService());

7
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java

@ -2213,6 +2213,13 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests { @@ -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;

Loading…
Cancel
Save