From a9d22825aa07a23ff27df96f0e4f5f6292eb0a99 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 22 Sep 2016 17:55:50 +0200 Subject: [PATCH] DATAMONGO-1497 - MappingMongoConverter now consistently uses DbObjectAccessor. We now use DbObjectAccessor also for preliminary inspections of the source DBObject (e.g. whether a value is present at all). Previously we operated on the DBObject directly which caused issues with properties mapped to nested fields as the keys weren't exploded correctly and thus the check always failed. --- .../core/convert/MappingMongoConverter.java | 10 +++++----- .../convert/MappingMongoConverterUnitTests.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index 59049c01c..348763de0 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -259,14 +259,14 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App // make sure id property is set before all other properties Object idValue = null; + final DBObjectAccessor dbObjectAccessor = new DBObjectAccessor(dbo); - if (idProperty != null && new DBObjectAccessor(dbo).hasValue(idProperty)) { + if (idProperty != null && dbObjectAccessor.hasValue(idProperty)) { idValue = getValueInternal(idProperty, dbo, evaluator, path); accessor.setProperty(idProperty, idValue); } - final ObjectPath currentPath = path.push(result, entity, - idValue != null ? dbo.get(idProperty.getFieldName()) : null); + final ObjectPath currentPath = path.push(result, entity, idValue != null ? dbObjectAccessor.get(idProperty) : null); // Set properties not already set in the constructor entity.doWithProperties(new PropertyHandler() { @@ -277,7 +277,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App return; } - if (!dbo.containsField(prop.getFieldName()) || entity.isConstructorArgument(prop)) { + if (entity.isConstructorArgument(prop) || !dbObjectAccessor.hasValue(prop)) { return; } @@ -290,7 +290,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App public void doWithAssociation(Association association) { final MongoPersistentProperty property = association.getInverse(); - Object value = dbo.get(property.getFieldName()); + Object value = dbObjectAccessor.get(property); if (value == null || entity.isConstructorArgument(property)) { return; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java index 9328ad1fc..f8d00bc8b 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java @@ -2074,6 +2074,18 @@ public class MappingMongoConverterUnitTests { assertThat(converter.read(ClassWithIntId.class, new BasicDBObject()), is(notNullValue())); } + /** + * @see DATAMONGO-1497 + */ + @Test + public void readsPropertyFromNestedFieldCorrectly() { + + DBObject source = new BasicDBObject("nested", new BasicDBObject("sample", "value")); + TypeWithPropertyInNestedField result = converter.read(TypeWithPropertyInNestedField.class, source); + + assertThat(result.sample, is("value")); + } + static class GenericType { T content; } @@ -2420,4 +2432,8 @@ public class MappingMongoConverterUnitTests { throw new ConversionNotSupportedException(source, String.class, null); } } + + static class TypeWithPropertyInNestedField { + @Field("nested.sample") String sample; + } }