diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java index 96a2b38f9..3e728f0d8 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java @@ -27,10 +27,11 @@ import org.bson.BsonValue; import org.bson.Document; import org.bson.conversions.Bson; import org.bson.types.ObjectId; - import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.converter.Converter; import org.springframework.data.annotation.Reference; +import org.springframework.data.convert.PropertyValueConverter; +import org.springframework.data.convert.ValueConversionContext; import org.springframework.data.domain.Example; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.MappingException; @@ -438,9 +439,18 @@ public class QueryMapper { if (documentField.getProperty() != null && converter.getCustomConversions().getPropertyValueConversions().hasValueConverter(documentField.getProperty())) { - return converter.getCustomConversions().getPropertyValueConversions() - .getValueConverter(documentField.getProperty()) - .write(value, new MongoConversionContext(documentField.getProperty(), converter)); + + MongoConversionContext conversionContext = new MongoConversionContext(documentField.getProperty(), converter); + PropertyValueConverter> valueConverter = converter + .getCustomConversions().getPropertyValueConversions().getValueConverter(documentField.getProperty()); + + /* might be an $in clause with multiple entries */ + if (!documentField.getProperty().isCollectionLike() && sourceValue instanceof Collection) { + Collection collection = (Collection) sourceValue; + return collection.stream().map(it -> valueConverter.write(it, conversionContext)).collect(Collectors.toList()); + } + + return valueConverter.write(value, conversionContext); } if (documentField.isIdField() && !documentField.isAssociation()) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java index d82f6df7b..970bf21b4 100755 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java @@ -1453,7 +1453,7 @@ public class QueryMapperUnitTests { assertThat(mappedQuery.get("_id")) .isEqualTo(org.bson.Document.parse("{ $in: [ {$oid: \"5b8bedceb1e0bfc07b008828\" } ]}")); } - + @Test // GH-3596 void considersValueConverterWhenPresent() { @@ -1461,6 +1461,15 @@ public class QueryMapperUnitTests { assertThat(mappedObject).isEqualTo(new org.bson.Document("text", "eulav")); } + @Test // GH-4080 + void convertsListOfValuesForPropertyThatHasValueConverterButIsNotCollectionLikeOneByOne() { + + org.bson.Document mappedObject = mapper.getMappedObject(query(where("text").in("spring", "data")).getQueryObject(), + context.getPersistentEntity(WithPropertyValueConverter.class)); + + assertThat(mappedObject).isEqualTo("{ 'text' : { $in : ['gnirps', 'atad'] } }"); + } + class WithDeepArrayNesting { List level0; @@ -1739,9 +1748,9 @@ public class QueryMapperUnitTests { static class MyAddress { private String street; } - + static class WithPropertyValueConverter { - + @ValueConverter(ReversingValueConverter.class) String text; }