Skip to content

Commit 02f5819

Browse files
Fix property value conversion in query mapper for nested values.
1 parent 6ae7cda commit 02f5819

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,15 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
455455
return collection.stream().map(it -> valueConverter.write(it, conversionContext)).collect(Collectors.toList());
456456
}
457457

458+
if(!documentField.getProperty().isMap() && sourceValue instanceof Document document) {
459+
return new Document(document.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey(), entry -> {
460+
if(isKeyword(entry.getKey())) {
461+
return getMappedValue(documentField, entry.getValue());
462+
}
463+
return entry.getValue();
464+
})));
465+
}
466+
458467
return valueConverter.write(value, conversionContext);
459468
}
460469

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,24 @@ void convertsListOfValuesForPropertyThatHasValueConverterButIsNotCollectionLikeO
15091509
assertThat(mappedObject).isEqualTo("{ 'text' : { $in : ['gnirps', 'atad'] } }");
15101510
}
15111511

1512+
@Test // GH-4510
1513+
void convertsNestedOperatorValueForPropertyThatHasValueConverter() {
1514+
1515+
org.bson.Document mappedObject = mapper.getMappedObject(query(where("text").gt("spring").lt( "data")).getQueryObject(),
1516+
context.getPersistentEntity(WithPropertyValueConverter.class));
1517+
1518+
assertThat(mappedObject).isEqualTo("{ 'text' : { $gt : 'gnirps', $lt : 'atad' } }");
1519+
}
1520+
1521+
@Test // GH-4510
1522+
void convertsNestedOperatorValueForPropertyContainingListThatHasValueConverter() {
1523+
1524+
org.bson.Document mappedObject = mapper.getMappedObject(query(where("text").gt("spring").in( "data")).getQueryObject(),
1525+
context.getPersistentEntity(WithPropertyValueConverter.class));
1526+
1527+
assertThat(mappedObject).isEqualTo("{ 'text' : { $gt : 'gnirps', $in : [ 'atad' ] } }");
1528+
}
1529+
15121530
class WithDeepArrayNesting {
15131531

15141532
List<WithNestedArray> level0;

0 commit comments

Comments
 (0)