Browse Source

DATAMONGO-862 - Fixed handling of unmapped paths for updates.

UpdateMapper uses key instead of cleaned property path when not directly pointing to a property.

Original pull request: #132.
pull/135/merge
Christoph Strobl 12 years ago committed by Oliver Gierke
parent
commit
5ace4032ed
  1. 9
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/UpdateMapper.java
  2. 43
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java
  3. 18
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

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

@ -163,6 +163,15 @@ public class UpdateMapper extends QueryMapper {
this.key = key; this.key = key;
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.convert.QueryMapper.MetadataBackedField#getMappedKey()
*/
@Override
public String getMappedKey() {
return this.getPath() == null ? key : super.getMappedKey();
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.convert.QueryMapper.MetadataBackedField#getPropertyConverter() * @see org.springframework.data.mongodb.core.convert.QueryMapper.MetadataBackedField#getPropertyConverter()

43
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

@ -2293,8 +2293,6 @@ public class MongoTemplateTests {
} }
/** /**
* <<<<<<< HEAD
*
* @see DATAMONOGO-828 * @see DATAMONOGO-828
*/ */
@Test @Test
@ -2422,7 +2420,7 @@ public class MongoTemplateTests {
* @see DATAMONGO-468 * @see DATAMONGO-468
*/ */
@Test @Test
public void shouldBeAbleToUpdateDbRefPropertyWithDomainObject(){ public void shouldBeAbleToUpdateDbRefPropertyWithDomainObject() {
Sample sample1 = new Sample("1", "A"); Sample sample1 = new Sample("1", "A");
Sample sample2 = new Sample("2", "B"); Sample sample2 = new Sample("2", "B");
@ -2434,17 +2432,36 @@ public class MongoTemplateTests {
doc.dbRefProperty = sample1; doc.dbRefProperty = sample1;
template.save(doc); template.save(doc);
Update update = new Update().set("dbRefProperty",sample2); Update update = new Update().set("dbRefProperty", sample2);
Query qry = query(where("id").is("1")); Query qry = query(where("id").is("1"));
template.updateFirst(qry, update, DocumentWithDBRefCollection.class); template.updateFirst(qry, update, DocumentWithDBRefCollection.class);
DocumentWithDBRefCollection updatedDoc = template.findOne(qry, DocumentWithDBRefCollection.class); DocumentWithDBRefCollection updatedDoc = template.findOne(qry, DocumentWithDBRefCollection.class);
assertThat(updatedDoc,is(notNullValue())); assertThat(updatedDoc, is(notNullValue()));
assertThat(updatedDoc.dbRefProperty,is(notNullValue())); assertThat(updatedDoc.dbRefProperty, is(notNullValue()));
assertThat(updatedDoc.dbRefProperty.id,is(sample2.id)); assertThat(updatedDoc.dbRefProperty.id, is(sample2.id));
assertThat(updatedDoc.dbRefProperty.field,is(sample2.field)); assertThat(updatedDoc.dbRefProperty.field, is(sample2.field));
}
/**
* @see DATAMONGO-862
*/
@Test
public void testUpdateShouldWorkForPathsOnInterfaceMethods() {
DocumentWithCollection document = new DocumentWithCollection(Arrays.<Model> asList(new ModelA("spring"),
new ModelA("data")));
template.save(document);
Query query = query(where("id").is(document.id).and("models._id").exists(true));
Update update = new Update().set("models.$.value", "mongodb");
template.findAndModify(query, update, DocumentWithCollection.class);
DocumentWithCollection result = template.findOne(query(where("id").is(document.id)), DocumentWithCollection.class);
assertThat(result.models.get(0).value(), is("mongodb"));
} }
static class DocumentWithDBRefCollection { static class DocumentWithDBRefCollection {
@ -2454,7 +2471,7 @@ public class MongoTemplateTests {
@org.springframework.data.mongodb.core.mapping.DBRef// @org.springframework.data.mongodb.core.mapping.DBRef//
public List<Sample> dbRefAnnotatedList; public List<Sample> dbRefAnnotatedList;
@org.springframework.data.mongodb.core.mapping.DBRef @org.springframework.data.mongodb.core.mapping.DBRef//
public Sample dbRefProperty; public Sample dbRefProperty;
} }
@ -2482,10 +2499,13 @@ public class MongoTemplateTests {
static interface Model { static interface Model {
String value(); String value();
String id();
} }
static class ModelA implements Model { static class ModelA implements Model {
@Id String id;
private String value; private String value;
ModelA(String value) { ModelA(String value) {
@ -2496,6 +2516,11 @@ public class MongoTemplateTests {
public String value() { public String value() {
return this.value; return this.value;
} }
@Override
public String id() {
return id;
}
} }
static class Document { static class Document {

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

@ -376,6 +376,20 @@ public class UpdateMapperUnitTests {
assertThat(setClause.get("dbRefProperty"), is((Object) new DBRef(null, "entity", entity.id))); assertThat(setClause.get("dbRefProperty"), is((Object) new DBRef(null, "entity", entity.id)));
} }
/**
* @see DATAMONGO-862
*/
@Test
public void rendersUpdateAndPreservesKeyForPathsNotPointingToProperty() {
Update update = new Update().set("listOfInterface.$.value", "expected-value");
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(ParentClass.class));
DBObject setClause = getAsDBObject(mappedObject, "$set");
assertThat(setClause.containsField("listOfInterface.$.value"), is(true));
}
static interface Model {} static interface Model {}
static class ModelImpl implements Model { static class ModelImpl implements Model {
@ -384,6 +398,7 @@ public class UpdateMapperUnitTests {
public ModelImpl(int value) { public ModelImpl(int value) {
this.value = value; this.value = value;
} }
} }
public class ModelWrapper { public class ModelWrapper {
@ -411,6 +426,9 @@ public class UpdateMapperUnitTests {
@Field("aliased")// @Field("aliased")//
List<? extends AbstractChildClass> list; List<? extends AbstractChildClass> list;
@Field//
List<Model> listOfInterface;
public ParentClass(String id, List<? extends AbstractChildClass> list) { public ParentClass(String id, List<? extends AbstractChildClass> list) {
this.id = id; this.id = id;
this.list = list; this.list = list;

Loading…
Cancel
Save