Skip to content

Commit 0cd082e

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 56e763c commit 0cd082e

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
@@ -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)