Skip to content

Commit 5bb7364

Browse files
lijixuechristophstrobl
authored andcommitted
Fix QueryMapper property path resolution for nested paths containing numeric values.
Prior to this fix a path that contains numeric values used as position parameters would have been stripped in a way that left out the last digit. This could lead to wrong path resolution if the incorrectly constructed property name accidentally matched an existing one. Closes: #4426 Original Pull Request: #4427
1 parent e959b04 commit 5bb7364

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,18 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre
12451245

12461246
String rawPath = removePlaceholders(POSITIONAL_OPERATOR,
12471247
removePlaceholders(DOT_POSITIONAL_PATTERN, pathExpression));
1248+
// fix xx.11.22.33 becomes xx3, it should be xx.33, then path should be null. (test mapNestedLastBigIntegerFieldCorrectly)
1249+
if (pathExpression.contains(".")) {
1250+
String lastDotString = pathExpression.substring(pathExpression.lastIndexOf("."));
1251+
int lastDotLength = lastDotString.length();
1252+
int newLength = 0;
1253+
if (rawPath.contains(".")) {
1254+
newLength = rawPath.substring(rawPath.lastIndexOf(".")).length();
1255+
}
1256+
if (lastDotLength != newLength) {
1257+
rawPath = rawPath.substring(0, rawPath.length() - 1) + lastDotString;
1258+
}
1259+
}
12481260

12491261
if (sourceProperty != null && sourceProperty.getOwner().equals(entity)) {
12501262
return mappingContext.getPersistentPropertyPath(

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,16 @@ void mapNestedIntegerFieldCorrectly() {
12221222
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0.1.3", "4")));
12231223
}
12241224

1225+
@Test
1226+
void mapNestedLastBigIntegerFieldCorrectly() {
1227+
1228+
Update update = new Update().set("levelOne.0.1.32", "4");
1229+
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
1230+
context.getPersistentEntity(EntityWithNestedMap.class));
1231+
1232+
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0.1.32", "4")));
1233+
}
1234+
12251235
@Test // GH-3775
12261236
void mapNestedMixedStringIntegerFieldCorrectly() {
12271237

@@ -1719,6 +1729,8 @@ static class UnwrappableType {
17191729

17201730
static class EntityWithNestedMap {
17211731
Map<String, Map<String, Map<String, Object>>> levelOne;
1732+
// for test mapNestedLastBigIntegerFieldCorrectly()
1733+
Map<String, Map<String, Map<String, Object>>> levelOne2;
17221734
}
17231735

17241736
static class Customer {

0 commit comments

Comments
 (0)