Skip to content

Commit b66bfae

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 eb3d55e commit b66bfae

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
@@ -264,14 +264,14 @@ private <S extends Object> S read(final MongoPersistentEntity<S> entity, final D
264264

265265
// make sure id property is set before all other properties
266266
Object idValue = null;
267+
final DBObjectAccessor dbObjectAccessor = new DBObjectAccessor(dbo);
267268

268-
if (idProperty != null && new DBObjectAccessor(dbo).hasValue(idProperty)) {
269+
if (idProperty != null && dbObjectAccessor.hasValue(idProperty)) {
269270
idValue = getValueInternal(idProperty, dbo, evaluator, path);
270271
accessor.setProperty(idProperty, idValue);
271272
}
272273

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

276276
// Set properties not already set in the constructor
277277
entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
@@ -282,7 +282,7 @@ public void doWithPersistentProperty(MongoPersistentProperty prop) {
282282
return;
283283
}
284284

285-
if (!dbo.containsField(prop.getFieldName()) || entity.isConstructorArgument(prop)) {
285+
if (entity.isConstructorArgument(prop) || !dbObjectAccessor.hasValue(prop)) {
286286
return;
287287
}
288288

@@ -295,7 +295,7 @@ public void doWithPersistentProperty(MongoPersistentProperty prop) {
295295
public void doWithAssociation(Association<MongoPersistentProperty> association) {
296296

297297
final MongoPersistentProperty property = association.getInverse();
298-
Object value = dbo.get(property.getFieldName());
298+
Object value = dbObjectAccessor.get(property);
299299

300300
if (value == null || entity.isConstructorArgument(property)) {
301301
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)