From e4eaf6f2d300d0c9a14a3b0f1d9496a0a9484166 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 23 Mar 2016 09:26:00 +0100 Subject: [PATCH] DATAMONGO-1401 - Fix error when updating entity with both GeoJsonPoint and Version property. We now ignore property reference exceptions when resolving field values that have already been mapped. Eg. in case of an already mapped update extracted from an actual domain type instance. Original pull request: #351. --- .../mongodb/core/convert/QueryMapper.java | 19 ++++++++--- .../data/mongodb/core/MongoTemplateTests.java | 34 +++++++++++++++++-- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java index 490325461..2039f1396 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2015 the original author or authors. + * Copyright 2011-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PropertyPath; import org.springframework.data.mapping.PropertyReferenceException; +import org.springframework.data.mapping.context.InvalidPersistentPropertyPath; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.PersistentPropertyPath; import org.springframework.data.mapping.model.MappingException; @@ -119,10 +120,20 @@ public class QueryMapper { continue; } - Field field = createPropertyField(entity, key, mappingContext); - Entry entry = getMappedObjectForField(field, query.get(key)); + try { + + Field field = createPropertyField(entity, key, mappingContext); + Entry entry = getMappedObjectForField(field, query.get(key)); + result.put(entry.getKey(), entry.getValue()); + } catch (InvalidPersistentPropertyPath invalidPathException) { - result.put(entry.getKey(), entry.getValue()); + // in case the object has not already been mapped + if (!(query.get(key) instanceof DBObject)) { + throw invalidPathException; + } + + result.put(key, query.get(key)); + } } return result; 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 1feec6553..86ce2a08b 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 @@ -1,5 +1,5 @@ /* - * Copyright 2011-2015 the original author or authors. + * Copyright 2011-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,6 +63,7 @@ import org.springframework.data.mongodb.core.convert.DbRefResolver; import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; import org.springframework.data.mongodb.core.convert.LazyLoadingProxy; import org.springframework.data.mongodb.core.convert.MappingMongoConverter; +import org.springframework.data.mongodb.core.geo.GeoJsonPoint; import org.springframework.data.mongodb.core.index.Index; import org.springframework.data.mongodb.core.index.Index.Duplicates; import org.springframework.data.mongodb.core.index.IndexField; @@ -197,6 +198,7 @@ public class MongoTemplateTests { template.dropCollection(SomeTemplate.class); template.dropCollection(Address.class); template.dropCollection(DocumentWithCollectionOfSamples.class); + template.dropCollection(WithGeoJson.class); } @Test @@ -3143,11 +3145,30 @@ public class MongoTemplateTests { assertThat(loaded.refToDocNotUsedInCtor, instanceOf(LazyLoadingProxy.class)); } + /** + * @see DATAMONGO-1401 + */ + @Test + public void updateShouldWorkForTypesContainingGeoJsonTypes() { + + WithGeoJson wgj = new WithGeoJson(); + wgj.id = "1"; + wgj.description = "datamongo-1401"; + wgj.point = new GeoJsonPoint(1D, 2D); + + template.save(wgj); + + wgj.description = "datamongo-1401-update"; + template.save(wgj); + + assertThat(template.findOne(query(where("id").is(wgj.id)), WithGeoJson.class).point, is(equalTo(wgj.point))); + } + static class DoucmentWithNamedIdField { @Id String someIdKey; - @Field(value = "val")// + @Field(value = "val") // String value; @Override @@ -3484,4 +3505,13 @@ public class MongoTemplateTests { } + static class WithGeoJson { + + @Id String id; + @Version // + Integer version; + String description; + GeoJsonPoint point; + } + }