Browse Source

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.
pull/663/head
Oliver Gierke 9 years ago
parent
commit
a9d22825aa
  1. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  2. 16
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

10
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 @@ -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<MongoPersistentProperty>() {
@ -277,7 +277,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App @@ -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 @@ -290,7 +290,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
public void doWithAssociation(Association<MongoPersistentProperty> association) {
final MongoPersistentProperty property = association.getInverse();
Object value = dbo.get(property.getFieldName());
Object value = dbObjectAccessor.get(property);
if (value == null || entity.isConstructorArgument(property)) {
return;

16
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

@ -2074,6 +2074,18 @@ public class MappingMongoConverterUnitTests { @@ -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> {
T content;
}
@ -2420,4 +2432,8 @@ public class MappingMongoConverterUnitTests { @@ -2420,4 +2432,8 @@ public class MappingMongoConverterUnitTests {
throw new ConversionNotSupportedException(source, String.class, null);
}
}
static class TypeWithPropertyInNestedField {
@Field("nested.sample") String sample;
}
}

Loading…
Cancel
Save