From 5ace4032edf5a2241b50f82c51f90aa49976a8a7 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 26 Feb 2014 15:30:46 +0100 Subject: [PATCH] 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. --- .../mongodb/core/convert/UpdateMapper.java | 9 ++++ .../data/mongodb/core/MongoTemplateTests.java | 45 ++++++++++++++----- .../core/convert/UpdateMapperUnitTests.java | 18 ++++++++ 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/UpdateMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/UpdateMapper.java index 2e687a282..0b839b102 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/UpdateMapper.java +++ b/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; } + /* + * (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) * @see org.springframework.data.mongodb.core.convert.QueryMapper.MetadataBackedField#getPropertyConverter() diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java index 475573963..2e2d22da7 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java @@ -2293,8 +2293,6 @@ public class MongoTemplateTests { } /** - * <<<<<<< HEAD - * * @see DATAMONOGO-828 */ @Test @@ -2394,7 +2392,7 @@ public class MongoTemplateTests { assertThat(result.dbRefAnnotatedList.get(0), is(notNullValue())); assertThat(result.dbRefAnnotatedList.get(0).id, is((Object) "1")); } - + /** * @see DATAMONGO-852 */ @@ -2422,7 +2420,7 @@ public class MongoTemplateTests { * @see DATAMONGO-468 */ @Test - public void shouldBeAbleToUpdateDbRefPropertyWithDomainObject(){ + public void shouldBeAbleToUpdateDbRefPropertyWithDomainObject() { Sample sample1 = new Sample("1", "A"); Sample sample2 = new Sample("2", "B"); @@ -2434,17 +2432,36 @@ public class MongoTemplateTests { doc.dbRefProperty = sample1; template.save(doc); - Update update = new Update().set("dbRefProperty",sample2); + Update update = new Update().set("dbRefProperty", sample2); Query qry = query(where("id").is("1")); template.updateFirst(qry, update, DocumentWithDBRefCollection.class); DocumentWithDBRefCollection updatedDoc = template.findOne(qry, DocumentWithDBRefCollection.class); - assertThat(updatedDoc,is(notNullValue())); - assertThat(updatedDoc.dbRefProperty,is(notNullValue())); - assertThat(updatedDoc.dbRefProperty.id,is(sample2.id)); - assertThat(updatedDoc.dbRefProperty.field,is(sample2.field)); + assertThat(updatedDoc, is(notNullValue())); + assertThat(updatedDoc.dbRefProperty, is(notNullValue())); + assertThat(updatedDoc.dbRefProperty.id, is(sample2.id)); + assertThat(updatedDoc.dbRefProperty.field, is(sample2.field)); + } + + /** + * @see DATAMONGO-862 + */ + @Test + public void testUpdateShouldWorkForPathsOnInterfaceMethods() { + + DocumentWithCollection document = new DocumentWithCollection(Arrays. 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 { @@ -2454,7 +2471,7 @@ public class MongoTemplateTests { @org.springframework.data.mongodb.core.mapping.DBRef// public List dbRefAnnotatedList; - @org.springframework.data.mongodb.core.mapping.DBRef + @org.springframework.data.mongodb.core.mapping.DBRef// public Sample dbRefProperty; } @@ -2482,10 +2499,13 @@ public class MongoTemplateTests { static interface Model { String value(); + + String id(); } static class ModelA implements Model { + @Id String id; private String value; ModelA(String value) { @@ -2496,6 +2516,11 @@ public class MongoTemplateTests { public String value() { return this.value; } + + @Override + public String id() { + return id; + } } static class Document { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java index 81dd80824..3af0fc27a 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java +++ b/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))); } + /** + * @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 class ModelImpl implements Model { @@ -384,6 +398,7 @@ public class UpdateMapperUnitTests { public ModelImpl(int value) { this.value = value; } + } public class ModelWrapper { @@ -411,6 +426,9 @@ public class UpdateMapperUnitTests { @Field("aliased")// List list; + @Field// + List listOfInterface; + public ParentClass(String id, List list) { this.id = id; this.list = list;