|
30 | 30 | import org.springframework.core.convert.ConversionService;
|
31 | 31 | import org.springframework.core.convert.converter.Converter;
|
32 | 32 | import org.springframework.data.annotation.Reference;
|
| 33 | +import org.springframework.data.convert.PropertyValueConverter; |
| 34 | +import org.springframework.data.convert.ValueConversionContext; |
33 | 35 | import org.springframework.data.domain.Example;
|
34 | 36 | import org.springframework.data.mapping.Association;
|
35 | 37 | import org.springframework.data.mapping.MappingException;
|
|
40 | 42 | import org.springframework.data.mapping.PropertyReferenceException;
|
41 | 43 | import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
|
42 | 44 | import org.springframework.data.mapping.context.MappingContext;
|
| 45 | +import org.springframework.data.mapping.model.PropertyValueProvider; |
43 | 46 | import org.springframework.data.mongodb.MongoExpression;
|
44 | 47 | import org.springframework.data.mongodb.core.aggregation.AggregationExpression;
|
45 | 48 | import org.springframework.data.mongodb.core.aggregation.RelaxedTypeBasedAggregationOperationContext;
|
@@ -437,9 +440,22 @@ protected Object getMappedValue(Field documentField, Object sourceValue) {
|
437 | 440 |
|
438 | 441 | if (documentField.getProperty() != null
|
439 | 442 | && converter.getCustomConversions().hasValueConverter(documentField.getProperty())) {
|
440 |
| - return converter.getCustomConversions().getPropertyValueConversions() |
441 |
| - .getValueConverter(documentField.getProperty()) |
442 |
| - .write(value, new MongoConversionContext(null, documentField.getProperty(), converter)); |
| 443 | + |
| 444 | + MongoConversionContext conversionContext = new MongoConversionContext(new PropertyValueProvider<>() { |
| 445 | + @Override |
| 446 | + public <T> T getPropertyValue(MongoPersistentProperty property) { |
| 447 | + throw new IllegalStateException("No enclosing property available"); |
| 448 | + } |
| 449 | + }, documentField.getProperty(), converter); |
| 450 | + PropertyValueConverter<Object, Object, ValueConversionContext<MongoPersistentProperty>> valueConverter = converter |
| 451 | + .getCustomConversions().getPropertyValueConversions().getValueConverter(documentField.getProperty()); |
| 452 | + |
| 453 | + /* might be an $in clause with multiple entries */ |
| 454 | + if (!documentField.getProperty().isCollectionLike() && sourceValue instanceof Collection<?> collection) { |
| 455 | + return collection.stream().map(it -> valueConverter.write(it, conversionContext)).collect(Collectors.toList()); |
| 456 | + } |
| 457 | + |
| 458 | + return valueConverter.write(value, conversionContext); |
443 | 459 | }
|
444 | 460 |
|
445 | 461 | if (documentField.isIdField() && !documentField.isAssociation()) {
|
|
0 commit comments