Skip to content

Commit 2de00cd

Browse files
lijixuechristophstrobl
lijixue
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 05c38b8 commit 2de00cd

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

+12
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,18 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre
12331233

12341234
String rawPath = removePlaceholders(POSITIONAL_OPERATOR,
12351235
removePlaceholders(DOT_POSITIONAL_PATTERN, pathExpression));
1236+
// fix xx.11.22.33 becomes xx3, it should be xx.33, then path should be null. (test mapNestedLastBigIntegerFieldCorrectly)
1237+
if (pathExpression.contains(".")) {
1238+
String lastDotString = pathExpression.substring(pathExpression.lastIndexOf("."));
1239+
int lastDotLength = lastDotString.length();
1240+
int newLength = 0;
1241+
if (rawPath.contains(".")) {
1242+
newLength = rawPath.substring(rawPath.lastIndexOf(".")).length();
1243+
}
1244+
if (lastDotLength != newLength) {
1245+
rawPath = rawPath.substring(0, rawPath.length() - 1) + lastDotString;
1246+
}
1247+
}
12361248

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

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

+12
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,16 @@ void mapNestedIntegerFieldCorrectly() {
12171217
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0.1.3", "4")));
12181218
}
12191219

1220+
@Test
1221+
void mapNestedLastBigIntegerFieldCorrectly() {
1222+
1223+
Update update = new Update().set("levelOne.0.1.32", "4");
1224+
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
1225+
context.getPersistentEntity(EntityWithNestedMap.class));
1226+
1227+
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0.1.32", "4")));
1228+
}
1229+
12201230
@Test // GH-3775
12211231
void mapNestedMixedStringIntegerFieldCorrectly() {
12221232

@@ -1732,6 +1742,8 @@ static class UnwrappableType {
17321742

17331743
static class EntityWithNestedMap {
17341744
Map<String, Map<String, Map<String, Object>>> levelOne;
1745+
// for test mapNestedLastBigIntegerFieldCorrectly()
1746+
Map<String, Map<String, Map<String, Object>>> levelOne2;
17351747
}
17361748

17371749
static class Customer {

0 commit comments

Comments
 (0)