From cbc923c727afb82f5aede32cb27cd8358272848f Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 3 Apr 2018 10:06:54 +0200 Subject: [PATCH] DATAMONGO-1916 - Fix potential ClassCastException in MappingMongoConverter#writeInternal when writing collections. Original pull request: #547. --- .../core/convert/MappingMongoConverter.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index efb6a702d..e91513a94 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -428,7 +428,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App } if (Collection.class.isAssignableFrom(entityType)) { - writeCollectionInternal((Collection) obj, ClassTypeInformation.LIST, (BasicDBList) bson); + writeCollectionInternal((Collection) obj, ClassTypeInformation.LIST, (Collection) bson); return; } @@ -654,17 +654,19 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App } /** - * Populates the given {@link BasicDBList} with values from the given {@link Collection}. + * Populates the given {@link Collection sink} with converted values from the given {@link Collection source}. * - * @param source the collection to create a {@link BasicDBList} for, must not be {@literal null}. + * @param source the collection to create a {@link Collection} for, must not be {@literal null}. * @param type the {@link TypeInformation} to consider or {@literal null} if unknown. - * @param sink the {@link BasicDBList} to write to. + * @param sink the {@link Collection} to write to. * @return */ - private BasicDBList writeCollectionInternal(Collection source, TypeInformation type, BasicDBList sink) { + private List writeCollectionInternal(Collection source, TypeInformation type, Collection sink) { TypeInformation componentType = null; + List collection = sink instanceof List ? (List) sink : new ArrayList<>(sink); + if (type != null) { componentType = type.getComponentType(); } @@ -674,17 +676,17 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App Class elementType = element == null ? null : element.getClass(); if (elementType == null || conversions.isSimpleType(elementType)) { - sink.add(getPotentiallyConvertedSimpleWrite(element)); + collection.add(getPotentiallyConvertedSimpleWrite(element)); } else if (element instanceof Collection || elementType.isArray()) { - sink.add(writeCollectionInternal(asCollection(element), componentType, new BasicDBList())); + collection.add(writeCollectionInternal(asCollection(element), componentType, new BasicDBList())); } else { Document document = new Document(); writeInternal(element, document, componentType); - sink.add(document); + collection.add(document); } } - return sink; + return collection; } /**