Skip to content

Commit cde3900

Browse files
christophstroblmp911de
authored andcommitted
Assert MongoTemplate rejects saving collection like entities like Lists or Iterator.
Closes #3570. Original pull request: #3576.
1 parent ffaa7ca commit cde3900

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -1319,9 +1319,12 @@ default long estimatedCount(Class<?> entityClass) {
13191319
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
13201320
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation" > Spring's
13211321
* Type Conversion"</a> for more details.
1322+
* <p />
1323+
* The {@literal objectToSave} must not be collection like.
13221324
*
13231325
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
13241326
* @return the saved object.
1327+
* @throws IllegalArgumentException in case the objectToSave is collection like.
13251328
*/
13261329
<T> T save(T objectToSave);
13271330

@@ -1337,10 +1340,14 @@ default long estimatedCount(Class<?> entityClass) {
13371340
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See <a
13381341
* https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation">Spring's Type
13391342
* Conversion"</a> for more details.
1343+
* <p />
1344+
* The {@literal objectToSave} must not be collection like.
1345+
*
13401346
*
13411347
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
13421348
* @param collectionName name of the collection to store the object in. Must not be {@literal null}.
13431349
* @return the saved object.
1350+
* @throws IllegalArgumentException in case the objectToSave is collection like.
13441351
*/
13451352
<T> T save(T objectToSave, String collectionName);
13461353

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

+1
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,7 @@ public <T> T save(T objectToSave, String collectionName) {
13761376

13771377
Assert.notNull(objectToSave, "Object to save must not be null!");
13781378
Assert.hasText(collectionName, "Collection name must not be null or empty!");
1379+
ensureNotCollectionLike(objectToSave);
13791380

13801381
AdaptibleEntity<T> source = operations.forEntity(objectToSave, mongoConverter.getConversionService());
13811382

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

+7
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,13 @@ void insertErrorsOnCustomIteratorImplementation() {
22132213
.isThrownBy(() -> template.insert(new TypeImplementingIterator()));
22142214
}
22152215

2216+
@Test // GH-3570
2217+
void saveErrorsOnCollectionLikeObjects() {
2218+
2219+
assertThatExceptionOfType(IllegalArgumentException.class)
2220+
.isThrownBy(() -> template.save(new ArrayList<>(Arrays.asList(1, 2, 3)), "myList"));
2221+
}
2222+
22162223
class AutogenerateableId {
22172224

22182225
@Id BigInteger id;

0 commit comments

Comments
 (0)