Skip to content

Use index instead of iterator to map positional and map keys for updates. #3930

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3921-SNAPSHOT</version>
<packaging>pom</packaging>

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

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3921-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3921-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,14 @@ public KeyMapper(String key,
this.currentIndex = 0;
}

String nextToken() {
return pathParts.get(currentIndex+1);
}

boolean hasNexToken() {
return pathParts.size() > currentIndex+1;
}

/**
* Maps the property name while retaining potential positional operator {@literal $}.
*
Expand All @@ -1420,31 +1428,25 @@ public KeyMapper(String key,
protected String mapPropertyName(MongoPersistentProperty property) {

StringBuilder mappedName = new StringBuilder(PropertyToFieldNameConverter.INSTANCE.convert(property));
if(!hasNexToken()) {
return mappedName.toString();
}

boolean inspect = iterator.hasNext();

while (inspect) {

String partial = iterator.next();
currentIndex++;

boolean isPositional = isPositionalParameter(partial) && property.isCollectionLike();
if (property.isMap() && currentPropertyRoot.equals(partial) && iterator.hasNext()) {
partial = iterator.next();
currentIndex++;
}

if (isPositional || property.isMap() && !currentPropertyRoot.equals(partial)) {
mappedName.append(".").append(partial);
}
String nextToken = nextToken();
if(isPositionalParameter(nextToken)) {

inspect = isPositional && iterator.hasNext();
mappedName.append(".").append(nextToken);
currentIndex+=2;
return mappedName.toString();
}

if (currentIndex + 1 < pathParts.size()) {
currentIndex++;
currentPropertyRoot = pathParts.get(currentIndex);
if(property.isMap()) {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,36 @@ void updateListWithDocuRefOnProperty() {
assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("customers", Arrays.asList("c-name"))));
}

@Test // GH-3921
void mapNumericKeyInPathHavingComplexMapValyeTypes() {

Update update = new Update().set("testInnerData.testMap.1.intValue", "4");
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(TestData.class));

assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("testInnerData.testMap.1.intValue","4")));
}

@Test // GH-3921
void mapNumericKeyInPathNotMatchingExistingProperties() {

Update update = new Update().set("testInnerData.imaginaryMap.1.nonExistingProperty", "4");
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(TestData.class));

assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("testInnerData.imaginaryMap.1.noExistingProperty","4")));
}

@Test // GH-3921
void mapNumericKeyInPathPartiallyMatchingExistingProperties() {

Update update = new Update().set("testInnerData.testMap.1.nonExistingProperty.2.someValue", "4");
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(TestData.class));

assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("testInnerData.testMap.1.nonExistingProperty.2.someValue","4")));
}

static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
}
Expand Down Expand Up @@ -1710,4 +1740,20 @@ static class WithDocumentReference {
private List<Sample> samples;
}

@Data
private static class TestData {
@Id
private String id;
private TestInnerData testInnerData;
}

@Data
private static class TestInnerData {
private Map<Integer, TestValue> testMap;
}

@Data
private static class TestValue {
private int intValue;
}
}