Skip to content

Commit 563ecd1

Browse files
Fix property value conversion in query mapper for nested values.
1 parent 5c537b3 commit 563ecd1

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
@@ -463,6 +463,15 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
463463
return collection.stream().map(it -> valueConverter.write(it, conversionContext)).collect(Collectors.toList());
464464
}
465465

466+
if(!documentField.getProperty().isMap() && sourceValue instanceof Document document) {
467+
return new Document(document.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey(), entry -> {
468+
if(isKeyword(entry.getKey())) {
469+
return getMappedValue(documentField, entry.getValue());
470+
}
471+
return entry.getValue();
472+
})));
473+
}
474+
466475
return valueConverter.write(value, conversionContext);
467476
}
468477

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
@@ -1528,6 +1528,24 @@ void mappingShouldRetainMapKeyOrder() {
15281528
assertThat(target.get("simpleMap", Map.class)).containsExactlyEntriesOf(sourceMap);
15291529
}
15301530

1531+
@Test // GH-4510
1532+
void convertsNestedOperatorValueForPropertyThatHasValueConverter() {
1533+
1534+
org.bson.Document mappedObject = mapper.getMappedObject(query(where("text").gt("spring").lt( "data")).getQueryObject(),
1535+
context.getPersistentEntity(WithPropertyValueConverter.class));
1536+
1537+
assertThat(mappedObject).isEqualTo("{ 'text' : { $gt : 'gnirps', $lt : 'atad' } }");
1538+
}
1539+
1540+
@Test // GH-4510
1541+
void convertsNestedOperatorValueForPropertyContainingListThatHasValueConverter() {
1542+
1543+
org.bson.Document mappedObject = mapper.getMappedObject(query(where("text").gt("spring").in( "data")).getQueryObject(),
1544+
context.getPersistentEntity(WithPropertyValueConverter.class));
1545+
1546+
assertThat(mappedObject).isEqualTo("{ 'text' : { $gt : 'gnirps', $in : [ 'atad' ] } }");
1547+
}
1548+
15311549
class WithSimpleMap {
15321550
Map<String, String> simpleMap;
15331551
}

0 commit comments

Comments
 (0)