Skip to content

Commit 852a461

Browse files
divyajnu08christophstrobl
authored andcommitted
Fix update mapping using nested integer keys on map structures.
Closes: #3775 Original Pull Request: #3815
1 parent 2cbed2a commit 852a461

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

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

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

@@ -1026,8 +1027,8 @@ public TypeInformation<?> getTypeHint() {
10261027
*/
10271028
protected static class MetadataBackedField extends Field {
10281029

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

10331034
private final MongoPersistentEntity<?> entity;

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

+25
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.springframework.data.mongodb.core.query.Criteria;
6060
import org.springframework.data.mongodb.core.query.Query;
6161
import org.springframework.data.mongodb.core.query.TextQuery;
62+
import org.springframework.data.mongodb.core.query.Update;
6263

6364
import com.mongodb.BasicDBObject;
6465
import com.mongodb.MongoClientSettings;
@@ -1326,6 +1327,25 @@ void mapStringIdFieldProjection() {
13261327
org.bson.Document mappedFields = mapper.getMappedFields(new org.bson.Document("id", 1), context.getPersistentEntity(WithStringId.class));
13271328
assertThat(mappedFields).containsEntry("_id", 1);
13281329
}
1330+
1331+
@Test
1332+
void mapNestedStringFieldCorrectly() {
1333+
Update update = new Update();
1334+
update.set("levelOne.a.b.d", "e");
1335+
org.bson.Document document = mapper.getMappedObject(update.getUpdateObject(),
1336+
context.getPersistentEntity(EntityWithNestedMap.class));
1337+
assertThat(document).isEqualTo(new org.bson.Document("$set",new org.bson.Document("levelOne.a.b.d","e")));
1338+
}
1339+
1340+
@Test
1341+
void mapNestedIntegerFieldCorrectly() {
1342+
Update update = new Update();
1343+
update.set("levelOne.0.1.3", "4");
1344+
org.bson.Document document = mapper.getMappedObject(update.getUpdateObject(),
1345+
context.getPersistentEntity(EntityWithNestedMap.class));
1346+
assertThat(document).isEqualTo(new org.bson.Document("$set",new org.bson.Document("levelOne.0.1.3","4")));
1347+
}
1348+
13291349

13301350
@Test // GH-3783
13311351
void retainsId$InWithStringArray() {
@@ -1514,6 +1534,11 @@ static class EntityWithIntKeyedMapOfMap{
15141534
static class EntityWithComplexValueTypeList {
15151535
List<SimpleEntityWithoutId> list;
15161536
}
1537+
1538+
static class EntityWithNestedMap {
1539+
Map<String, Map<String, Map<String, Object>>> levelOne;
1540+
}
1541+
15171542

15181543
static class WithExplicitTargetTypes {
15191544

0 commit comments

Comments
 (0)