Skip to content

Commit 82917db

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 e581d9d commit 82917db

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
@@ -1218,6 +1218,18 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre
12181218

12191219
String rawPath = removePlaceholders(POSITIONAL_OPERATOR,
12201220
removePlaceholders(DOT_POSITIONAL_PATTERN, pathExpression));
1221+
// fix xx.11.22.33 becomes xx3, it should be xx.33, then path should be null. (test mapNestedLastBigIntegerFieldCorrectly)
1222+
if (pathExpression.contains(".")) {
1223+
String lastDotString = pathExpression.substring(pathExpression.lastIndexOf("."));
1224+
int lastDotLength = lastDotString.length();
1225+
int newLength = 0;
1226+
if (rawPath.contains(".")) {
1227+
newLength = rawPath.substring(rawPath.lastIndexOf(".")).length();
1228+
}
1229+
if (lastDotLength != newLength) {
1230+
rawPath = rawPath.substring(0, rawPath.length() - 1) + lastDotString;
1231+
}
1232+
}
12211233

12221234
if (sourceProperty != null && sourceProperty.getOwner().equals(entity)) {
12231235
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
@@ -1223,6 +1223,16 @@ void mapNestedIntegerFieldCorrectly() {
12231223
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0.1.3", "4")));
12241224
}
12251225

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

@@ -1720,6 +1730,8 @@ static class UnwrappableType {
17201730

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

17251737
static class Customer {

0 commit comments

Comments
 (0)