Browse Source

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
3.4.x
Christoph Strobl 3 years ago committed by Mark Paluch
parent
commit
6b7ac32ea0
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 18
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 9
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

18
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.Document;
import org.bson.conversions.Bson; import org.bson.conversions.Bson;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.data.annotation.Reference; 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.domain.Example;
import org.springframework.data.mapping.Association; import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.MappingException; import org.springframework.data.mapping.MappingException;
@ -438,9 +439,18 @@ public class QueryMapper {
if (documentField.getProperty() != null if (documentField.getProperty() != null
&& converter.getCustomConversions().getPropertyValueConversions().hasValueConverter(documentField.getProperty())) { && converter.getCustomConversions().getPropertyValueConversions().hasValueConverter(documentField.getProperty())) {
return converter.getCustomConversions().getPropertyValueConversions()
.getValueConverter(documentField.getProperty()) MongoConversionContext conversionContext = new MongoConversionContext(documentField.getProperty(), converter);
.write(value, 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()) { if (documentField.isIdField() && !documentField.isAssociation()) {

9
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

@ -1461,6 +1461,15 @@ public class QueryMapperUnitTests {
assertThat(mappedObject).isEqualTo(new org.bson.Document("text", "eulav")); 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 { class WithDeepArrayNesting {
List<WithNestedArray> level0; List<WithNestedArray> level0;

Loading…
Cancel
Save