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
This commit is contained in:
committed by
Mark Paluch
parent
6004c9a9eb
commit
6b7ac32ea0
@@ -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<Object, Object, ValueConversionContext<MongoPersistentProperty>> 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()) {
|
||||
|
||||
@@ -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<WithNestedArray> level0;
|
||||
@@ -1739,9 +1748,9 @@ public class QueryMapperUnitTests {
|
||||
static class MyAddress {
|
||||
private String street;
|
||||
}
|
||||
|
||||
|
||||
static class WithPropertyValueConverter {
|
||||
|
||||
|
||||
@ValueConverter(ReversingValueConverter.class)
|
||||
String text;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user