Skip to content

Commit

Permalink
Fix update mapping using nested integer keys on map structures.
Browse files Browse the repository at this point in the history
Closes: #3775
Original Pull Request: #3815
  • Loading branch information
divyajnu08 authored and christophstrobl committed Sep 13, 2021
1 parent 2cbed2a commit 852a461
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author David Julia
* @author Divya Srivastava
*/
public class QueryMapper {

Expand Down Expand Up @@ -1026,8 +1027,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 @@ -1326,6 +1327,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")));
}


@Test // GH-3783
void retainsId$InWithStringArray() {
Expand Down Expand Up @@ -1514,6 +1534,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

0 comments on commit 852a461

Please sign in to comment.