Skip to content

Commit 190516f

Browse files
christophstroblmp911de
authored andcommitted
Fix property value conversion for $in clauses.
This commit fixes an issue where a property value converter is not applied if the query is using an $in clause that compares the value against a collection of potential candidates. Original pull request: #4324 Closes #4080
1 parent 39e30cd commit 190516f

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.springframework.core.convert.ConversionService;
3131
import org.springframework.core.convert.converter.Converter;
3232
import org.springframework.data.annotation.Reference;
33+
import org.springframework.data.convert.PropertyValueConverter;
34+
import org.springframework.data.convert.ValueConversionContext;
3335
import org.springframework.data.domain.Example;
3436
import org.springframework.data.mapping.Association;
3537
import org.springframework.data.mapping.MappingException;
@@ -435,9 +437,17 @@ protected Object getMappedValue(Field documentField, Object sourceValue) {
435437

436438
if (documentField.getProperty() != null
437439
&& converter.getCustomConversions().hasValueConverter(documentField.getProperty())) {
438-
return converter.getCustomConversions().getPropertyValueConversions()
439-
.getValueConverter(documentField.getProperty())
440-
.write(value, new MongoConversionContext(documentField.getProperty(), converter));
440+
441+
MongoConversionContext conversionContext = new MongoConversionContext(documentField.getProperty(), converter);
442+
PropertyValueConverter<Object, Object, ValueConversionContext<MongoPersistentProperty>> valueConverter = converter
443+
.getCustomConversions().getPropertyValueConversions().getValueConverter(documentField.getProperty());
444+
445+
/* might be an $in clause with multiple entries */
446+
if (!documentField.getProperty().isCollectionLike() && sourceValue instanceof Collection<?> collection) {
447+
return collection.stream().map(it -> valueConverter.write(it, conversionContext)).collect(Collectors.toList());
448+
}
449+
450+
return valueConverter.write(value, conversionContext);
441451
}
442452

443453
if (documentField.isIdField() && !documentField.isAssociation()) {

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -1453,14 +1453,23 @@ void mapStringIdFieldProjection() {
14531453
assertThat(mappedQuery.get("_id"))
14541454
.isEqualTo(org.bson.Document.parse("{ $in: [ {$oid: \"5b8bedceb1e0bfc07b008828\" } ]}"));
14551455
}
1456-
1456+
14571457
@Test // GH-3596
14581458
void considersValueConverterWhenPresent() {
14591459

14601460
org.bson.Document mappedObject = mapper.getMappedObject(new org.bson.Document("text", "value"), context.getPersistentEntity(WithPropertyValueConverter.class));
14611461
assertThat(mappedObject).isEqualTo(new org.bson.Document("text", "eulav"));
14621462
}
14631463

1464+
@Test // GH-4080
1465+
void convertsListOfValuesForPropertyThatHasValueConverterButIsNotCollectionLikeOneByOne() {
1466+
1467+
org.bson.Document mappedObject = mapper.getMappedObject(query(where("text").in("spring", "data")).getQueryObject(),
1468+
context.getPersistentEntity(WithPropertyValueConverter.class));
1469+
1470+
assertThat(mappedObject).isEqualTo("{ 'text' : { $in : ['gnirps', 'atad'] } }");
1471+
}
1472+
14641473
class WithDeepArrayNesting {
14651474

14661475
List<WithNestedArray> level0;
@@ -1739,9 +1748,9 @@ static class Customer {
17391748
static class MyAddress {
17401749
private String street;
17411750
}
1742-
1751+
17431752
static class WithPropertyValueConverter {
1744-
1753+
17451754
@ValueConverter(ReversingValueConverter.class)
17461755
String text;
17471756
}

0 commit comments

Comments
 (0)