Skip to content

Fix update path resolution for index placeholders. #4514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4502-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4502-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4502-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4502-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Original file line number Diff line number Diff line change
@@ -1471,26 +1471,29 @@ protected String mapPropertyName(MongoPersistentProperty property) {
}

String nextToken = nextToken();
if (isPositionalParameter(nextToken)) {
if (property.isMap()) {

mappedName.append(".").append(nextToken);
currentIndex += 2;
return mappedName.toString();
}

if (property.isMap()) {

int i = 1;
while (isPositionalParameter(nextToken)) {
mappedName.append(".").append(nextToken);
currentIndex += 2;
return mappedName.toString();
i++;
nextToken = currentIndex + i < pathParts.size() ? pathParts.get(currentIndex + i) : "";
}

currentIndex++;
currentIndex += i;
return mappedName.toString();
}

static boolean isPositionalParameter(String partial) {

if(!StringUtils.hasText(partial)) {
return false;
}

if ("$".equals(partial)) {
return true;
}
@@ -1500,12 +1503,12 @@ static boolean isPositionalParameter(String partial) {
return true;
}

try {
Long.valueOf(partial);
return true;
} catch (NumberFormatException e) {
return false;
for (int i = 0; i < partial.length(); i++) {
if (!Character.isDigit(partial.charAt(i))) {
return false;
}
}
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1350,6 +1350,17 @@ void updateConsidersValueConverterWhenPresent() {
assertThat(mappedUpdate).isEqualTo("{ $set : { 'text' : 'eulav' } }");
}

@ParameterizedTest // GH-4502
@ValueSource(strings = {"levelOne.levelTwo.1", "levelOne.levelTwo.1.0", "levelOne.levelTwo.2.0",})
void objectNestedIntegerFieldCorrectly(String path) {

Update update = new Update().set(path, "4");
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(EntityWithNestedObject1.class));

assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document(path, "4")));
}

static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
}
@@ -1818,4 +1829,12 @@ static class WithPropertyValueConverter {
@ValueConverter(ReversingValueConverter.class)
String text;
}

static class EntityWithNestedObject1 {
EntityWithNestedObject2 levelOne;
}

static class EntityWithNestedObject2 {
Integer levelTwo;
}
}