DATAMONGO-2054 - Add support for array update operator $[].

We now support the $[] array update operator when mapping Update.

Original pull request: #653.
This commit is contained in:
Christoph Strobl
2019-03-01 12:49:30 +01:00
committed by Mark Paluch
parent 6ce23a0d90
commit 745dae4993
3 changed files with 42 additions and 6 deletions

View File

@@ -17,6 +17,8 @@ package org.springframework.data.mongodb.core.convert;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bson.BsonValue;
import org.bson.Document;
@@ -758,6 +760,8 @@ public class QueryMapper {
*/
protected static class Field {
protected static final Pattern POSITIONAL_PARM = Pattern.compile("\\$\\[.*\\]");
private static final String ID_KEY = "_id";
protected final String name;
@@ -1030,7 +1034,10 @@ public class QueryMapper {
try {
PropertyPath path = PropertyPath.from(pathExpression.replaceAll("\\.\\d+", ""), entity.getTypeInformation());
String rawPath = pathExpression.replaceAll("\\.\\d+", "") //
.replaceAll(POSITIONAL_PARM.pattern(), "");
PropertyPath path = PropertyPath.from(rawPath, entity.getTypeInformation());
if (isPathToJavaLangClassProperty(path)) {
return null;
@@ -1179,6 +1186,11 @@ public class QueryMapper {
return true;
}
Matcher matcher = POSITIONAL_PARM.matcher(partial);
if (matcher.find()) {
return true;
}
try {
Long.valueOf(partial);
return true;

View File

@@ -289,7 +289,7 @@ public class UpdateMapper extends QueryMapper {
public MetadataBackedUpdateField(MongoPersistentEntity<?> entity, String key,
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
super(key.replaceAll("\\.\\$", ""), entity, mappingContext);
super(key.replaceAll("\\.\\$(\\[.*\\])?", ""), entity, mappingContext);
this.key = key;
}

View File

@@ -1029,6 +1029,28 @@ public class UpdateMapperUnitTests {
assertThat(mappedUpdate).isEqualTo(new Document("AValue", "a value"));
}
@Test // DATAMONGO-2054
public void mappingShouldAllowPositionAllParameter() {
Update update = new Update().inc("grades.$[]", 10);
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(EntityWithListOfSimple.class));
assertThat(mappedUpdate).isEqualTo(new Document("$inc", new Document("grades.$[]", 10)));
}
@Test // DATAMONGO-2054
public void mappingShouldAllowPositionAllParameterWhenPopertyHasExplicitFieldName() {
Update update = new Update().inc("list.$[]", 10);
Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(ParentClass.class));
assertThat(mappedUpdate).isEqualTo(new Document("$inc", new Document("aliased.$[]", 10)));
}
static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
}
@@ -1245,6 +1267,10 @@ public class UpdateMapperUnitTests {
List<EntityWithAliasedObject> list;
}
static class EntityWithListOfSimple {
List<Integer> grades;
}
static class EntityWithAliasedObject {
@Field("renamed-value") Object value;
@@ -1348,11 +1374,9 @@ public class UpdateMapperUnitTests {
@Data
static class TypeWithFieldNameThatCannotBeDecapitalized {
@Id
protected String id;
@Id protected String id;
@Field("AValue")
private Long aValue = 0L;
@Field("AValue") private Long aValue = 0L;
}