Skip to content

Commit 075ccb1

Browse files
committed
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.
1 parent e3bddd1 commit 075ccb1

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,14 @@ private <S extends Object> S read(final MongoPersistentEntity<S> entity, final D
259259

260260
// make sure id property is set before all other properties
261261
Object idValue = null;
262+
final DBObjectAccessor dbObjectAccessor = new DBObjectAccessor(dbo);
262263

263-
if (idProperty != null && new DBObjectAccessor(dbo).hasValue(idProperty)) {
264+
if (idProperty != null && dbObjectAccessor.hasValue(idProperty)) {
264265
idValue = getValueInternal(idProperty, dbo, evaluator, path);
265266
accessor.setProperty(idProperty, idValue);
266267
}
267268

268-
final ObjectPath currentPath = path.push(result, entity,
269-
idValue != null ? dbo.get(idProperty.getFieldName()) : null);
269+
final ObjectPath currentPath = path.push(result, entity, idValue != null ? dbObjectAccessor.get(idProperty) : null);
270270

271271
// Set properties not already set in the constructor
272272
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
@@ -277,7 +277,7 @@ public void doWithPersistentProperty(MongoPersistentProperty prop) {
277277
return;
278278
}
279279

280-
if (!dbo.containsField(prop.getFieldName()) || entity.isConstructorArgument(prop)) {
280+
if (entity.isConstructorArgument(prop) || !dbObjectAccessor.hasValue(prop)) {
281281
return;
282282
}
283283

@@ -290,7 +290,7 @@ public void doWithPersistentProperty(MongoPersistentProperty prop) {
290290
public void doWithAssociation(Association<MongoPersistentProperty> association) {
291291

292292
final MongoPersistentProperty property = association.getInverse();
293-
Object value = dbo.get(property.getFieldName());
293+
Object value = dbObjectAccessor.get(property);
294294

295295
if (value == null || entity.isConstructorArgument(property)) {
296296
return;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,18 @@ public void readsDocumentWithPrimitiveIdButNoValue() {
20742074
assertThat(converter.read(ClassWithIntId.class, new BasicDBObject()), is(notNullValue()));
20752075
}
20762076

2077+
/**
2078+
* @see DATAMONGO-1497
2079+
*/
2080+
@Test
2081+
public void readsPropertyFromNestedFieldCorrectly() {
2082+
2083+
DBObject source = new BasicDBObject("nested", new BasicDBObject("sample", "value"));
2084+
TypeWithPropertyInNestedField result = converter.read(TypeWithPropertyInNestedField.class, source);
2085+
2086+
assertThat(result.sample, is("value"));
2087+
}
2088+
20772089
static class GenericType<T> {
20782090
T content;
20792091
}
@@ -2420,4 +2432,8 @@ public FooBarEnum convert(String source) {
24202432
throw new ConversionNotSupportedException(source, String.class, null);
24212433
}
24222434
}
2435+
2436+
static class TypeWithPropertyInNestedField {
2437+
@Field("nested.sample") String sample;
2438+
}
24232439
}

0 commit comments

Comments
 (0)