Skip to content

Commit 458fe4f

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
DATAMONGO-1058 - DBRef should respect explicit field name.
We now use property.getFieldName() for mapping DbRefs. This assures we also capture explicitly defined names set via @field.
1 parent 2780f60 commit 458fe4f

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public void doWithPersistentProperty(MongoPersistentProperty prop) {
284284
public void doWithAssociation(Association<MongoPersistentProperty> association) {
285285

286286
final MongoPersistentProperty property = association.getInverse();
287-
Object value = dbo.get(property.getName());
287+
Object value = dbo.get(property.getFieldName());
288288

289289
if (value == null) {
290290
return;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,6 +2791,7 @@ static class DocumentWithDBRefCollection {
27912791

27922792
@Id public String id;
27932793

2794+
@Field("db_ref_list")/** @see DATAMONGO-1058 */
27942795
@org.springframework.data.mongodb.core.mapping.DBRef//
27952796
public List<Sample> dbRefAnnotatedList;
27962797

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.junit.rules.ExpectedException;
5151
import org.junit.runner.RunWith;
5252
import org.mockito.Mock;
53+
import org.mockito.Mockito;
5354
import org.mockito.runners.MockitoJUnitRunner;
5455
import org.springframework.aop.framework.ProxyFactory;
5556
import org.springframework.beans.factory.annotation.Value;
@@ -1853,6 +1854,21 @@ public void rejectsBasicDbListToBeConvertedIntoComplexType() {
18531854
converter.read(Item.class, source);
18541855
}
18551856

1857+
/**
1858+
* @see DATAMONGO-1058
1859+
*/
1860+
@Test
1861+
public void readShouldRespectExplicitFieldNameForDbRef() {
1862+
1863+
BasicDBObject source = new BasicDBObject();
1864+
source.append("explict-name-for-db-ref", new DBRef(mock(DB.class), "foo", "1"));
1865+
1866+
converter.read(ClassWithExplicitlyNamedDBRefProperty.class, source);
1867+
1868+
verify(resolver, times(1)).resolveDbRef(Mockito.any(MongoPersistentProperty.class), Mockito.any(DBRef.class),
1869+
Mockito.any(DbRefResolverCallback.class), Mockito.any(DbRefProxyHandler.class));
1870+
}
1871+
18561872
static class GenericType<T> {
18571873
T content;
18581874
}
@@ -2102,4 +2118,16 @@ class ClassWithTextScoreProperty {
21022118

21032119
@TextScore Float score;
21042120
}
2121+
2122+
class ClassWithExplicitlyNamedDBRefProperty {
2123+
2124+
@Field("explict-name-for-db-ref")//
2125+
@org.springframework.data.mongodb.core.mapping.DBRef//
2126+
ClassWithIntId dbRefProperty;
2127+
2128+
public ClassWithIntId getDbRefProperty() {
2129+
return dbRefProperty;
2130+
}
2131+
2132+
}
21052133
}

0 commit comments

Comments
 (0)