Skip to content

issue-3775 - Using $set with nested integer keys breaks the structure #3815

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 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,8 @@ public TypeInformation<?> getTypeHint() {
*/
protected static class MetadataBackedField extends Field {

private static final Pattern POSITIONAL_PARAMETER_PATTERN = Pattern.compile("\\.\\$(\\[.*?\\])?|\\.\\d+");
private static final Pattern DOT_POSITIONAL_PATTERN = Pattern.compile("\\.\\d+");
private static final Pattern POSITIONAL_PARAMETER_PATTERN = Pattern.compile("\\.\\$(\\[.*?\\])?");
private static final Pattern DOT_POSITIONAL_PATTERN = Pattern.compile("\\.\\d+(?!$)");
private static final String INVALID_ASSOCIATION_REFERENCE = "Invalid path reference %s! Associations can only be pointed to directly or via their id property!";

private final MongoPersistentEntity<?> entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.TextQuery;
import org.springframework.data.mongodb.core.query.Update;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClientSettings;
Expand Down Expand Up @@ -1334,6 +1335,25 @@ void mapStringIdFieldProjection() {
org.bson.Document mappedFields = mapper.getMappedFields(new org.bson.Document("id", 1), context.getPersistentEntity(WithStringId.class));
assertThat(mappedFields).containsEntry("_id", 1);
}

@Test
void mapNestedStringFieldCorrectly() {
Update update = new Update();
update.set("levelOne.a.b.d", "e");
org.bson.Document document = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(EntityWithNestedMap.class));
assertThat(document).isEqualTo(new org.bson.Document("$set",new org.bson.Document("levelOne.a.b.d","e")));
}

@Test
void mapNestedIntegerFieldCorrectly() {
Update update = new Update();
update.set("levelOne.0.1.3", "4");
org.bson.Document document = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(EntityWithNestedMap.class));
assertThat(document).isEqualTo(new org.bson.Document("$set",new org.bson.Document("levelOne.0.1.3","4")));
}


class WithDeepArrayNesting {

Expand Down Expand Up @@ -1497,6 +1517,11 @@ static class EntityWithIntKeyedMapOfMap{
static class EntityWithComplexValueTypeList {
List<SimpleEntityWithoutId> list;
}

static class EntityWithNestedMap {
Map<String, Map<String, Map<String, Object>>> levelOne;
}


static class WithExplicitTargetTypes {

Expand Down