Browse Source

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
issue/4428
lijixue 3 years ago committed by Christoph Strobl
parent
commit
2de00cdb2f
No known key found for this signature in database
GPG Key ID: 8CC1AB53391458C8
  1. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 12
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

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

@ -1233,6 +1233,18 @@ public class QueryMapper {
String rawPath = removePlaceholders(POSITIONAL_OPERATOR, String rawPath = removePlaceholders(POSITIONAL_OPERATOR,
removePlaceholders(DOT_POSITIONAL_PATTERN, pathExpression)); removePlaceholders(DOT_POSITIONAL_PATTERN, pathExpression));
// fix xx.11.22.33 becomes xx3, it should be xx.33, then path should be null. (test mapNestedLastBigIntegerFieldCorrectly)
if (pathExpression.contains(".")) {
String lastDotString = pathExpression.substring(pathExpression.lastIndexOf("."));
int lastDotLength = lastDotString.length();
int newLength = 0;
if (rawPath.contains(".")) {
newLength = rawPath.substring(rawPath.lastIndexOf(".")).length();
}
if (lastDotLength != newLength) {
rawPath = rawPath.substring(0, rawPath.length() - 1) + lastDotString;
}
}
if (sourceProperty != null && sourceProperty.getOwner().equals(entity)) { if (sourceProperty != null && sourceProperty.getOwner().equals(entity)) {
return mappingContext.getPersistentPropertyPath( return mappingContext.getPersistentPropertyPath(

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

@ -1217,6 +1217,16 @@ class UpdateMapperUnitTests {
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0.1.3", "4"))); assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0.1.3", "4")));
} }
@Test
void mapNestedLastBigIntegerFieldCorrectly() {
Update update = new Update().set("levelOne.0.1.32", "4");
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(EntityWithNestedMap.class));
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document("levelOne.0.1.32", "4")));
}
@Test // GH-3775 @Test // GH-3775
void mapNestedMixedStringIntegerFieldCorrectly() { void mapNestedMixedStringIntegerFieldCorrectly() {
@ -1732,6 +1742,8 @@ class UpdateMapperUnitTests {
static class EntityWithNestedMap { static class EntityWithNestedMap {
Map<String, Map<String, Map<String, Object>>> levelOne; Map<String, Map<String, Map<String, Object>>> levelOne;
// for test mapNestedLastBigIntegerFieldCorrectly()
Map<String, Map<String, Map<String, Object>>> levelOne2;
} }
static class Customer { static class Customer {

Loading…
Cancel
Save