Skip to content

Commit e7150f5

Browse files
divyajnu08christophstrobl
authored andcommitted
Fix update mapping using nested integer keys on map structures.
Closes: #3775 Original Pull Request: #3815
1 parent 7d6b5ae commit e7150f5

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
* @author Christoph Strobl
7171
* @author Mark Paluch
7272
* @author David Julia
73+
* @author Divya Srivastava
7374
*/
7475
public class QueryMapper {
7576

@@ -1032,8 +1033,8 @@ public TypeInformation<?> getTypeHint() {
10321033
*/
10331034
protected static class MetadataBackedField extends Field {
10341035

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

10391040
private final MongoPersistentEntity<?> entity;

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

+25
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.springframework.data.mongodb.core.query.Criteria;
6262
import org.springframework.data.mongodb.core.query.Query;
6363
import org.springframework.data.mongodb.core.query.TextQuery;
64+
import org.springframework.data.mongodb.core.query.Update;
6465

6566
import com.mongodb.BasicDBObject;
6667
import com.mongodb.MongoClientSettings;
@@ -1354,6 +1355,25 @@ void mapStringIdFieldProjection() {
13541355
org.bson.Document mappedFields = mapper.getMappedFields(new org.bson.Document("id", 1), context.getPersistentEntity(WithStringId.class));
13551356
assertThat(mappedFields).containsEntry("_id", 1);
13561357
}
1358+
1359+
@Test
1360+
void mapNestedStringFieldCorrectly() {
1361+
Update update = new Update();
1362+
update.set("levelOne.a.b.d", "e");
1363+
org.bson.Document document = mapper.getMappedObject(update.getUpdateObject(),
1364+
context.getPersistentEntity(EntityWithNestedMap.class));
1365+
assertThat(document).isEqualTo(new org.bson.Document("$set",new org.bson.Document("levelOne.a.b.d","e")));
1366+
}
1367+
1368+
@Test
1369+
void mapNestedIntegerFieldCorrectly() {
1370+
Update update = new Update();
1371+
update.set("levelOne.0.1.3", "4");
1372+
org.bson.Document document = mapper.getMappedObject(update.getUpdateObject(),
1373+
context.getPersistentEntity(EntityWithNestedMap.class));
1374+
assertThat(document).isEqualTo(new org.bson.Document("$set",new org.bson.Document("levelOne.0.1.3","4")));
1375+
}
1376+
13571377

13581378
@Test // GH-3783
13591379
void retainsId$InWithStringArray() {
@@ -1542,6 +1562,11 @@ static class EntityWithIntKeyedMapOfMap{
15421562
static class EntityWithComplexValueTypeList {
15431563
List<SimpleEntityWithoutId> list;
15441564
}
1565+
1566+
static class EntityWithNestedMap {
1567+
Map<String, Map<String, Map<String, Object>>> levelOne;
1568+
}
1569+
15451570

15461571
static class WithExplicitTargetTypes {
15471572

0 commit comments

Comments
 (0)