Assert MongoTemplate rejects saving collection like entities like Lists or Iterator.
Closes #3570. Original pull request: #3576.
This commit is contained in:
committed by
Mark Paluch
parent
ffaa7cae6f
commit
cde39008cf
@@ -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 {
|
||||
* 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);
|
||||
|
||||
|
||||
@@ -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());
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user