Skip to content

Commit 3f4087d

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. Original pull request: #227.
1 parent 0430962 commit 3f4087d

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ public void doWithPersistentProperty(MongoPersistentProperty prop) {
268268
entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
269269
public void doWithAssociation(Association<MongoPersistentProperty> association) {
270270

271-
MongoPersistentProperty property = association.getInverse();
272-
Object value = dbo.get(property.getName());
271+
final MongoPersistentProperty property = association.getInverse();
272+
Object value = dbo.get(property.getFieldName());
273273

274274
if (value == null) {
275275
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: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,26 @@
1515
*/
1616
package org.springframework.data.mongodb.core.convert;
1717

18-
import static org.hamcrest.Matchers.*;
19-
import static org.junit.Assert.*;
20-
import static org.mockito.Mockito.*;
21-
import static org.springframework.data.mongodb.core.DBObjectTestUtils.*;
18+
import static org.hamcrest.Matchers.arrayWithSize;
19+
import static org.hamcrest.Matchers.equalTo;
20+
import static org.hamcrest.Matchers.hasItem;
21+
import static org.hamcrest.Matchers.hasItems;
22+
import static org.hamcrest.Matchers.hasSize;
23+
import static org.hamcrest.Matchers.instanceOf;
24+
import static org.hamcrest.Matchers.is;
25+
import static org.hamcrest.Matchers.not;
26+
import static org.hamcrest.Matchers.notNullValue;
27+
import static org.hamcrest.Matchers.nullValue;
28+
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertThat;
30+
import static org.junit.Assert.assertTrue;
31+
import static org.junit.Assert.fail;
32+
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.times;
34+
import static org.mockito.Mockito.verify;
35+
import static org.mockito.Mockito.when;
36+
import static org.springframework.data.mongodb.core.DBObjectTestUtils.getAsDBObject;
37+
import static org.springframework.data.mongodb.core.DBObjectTestUtils.getTypedValue;
2238

2339
import java.math.BigDecimal;
2440
import java.math.BigInteger;
@@ -50,6 +66,7 @@
5066
import org.junit.rules.ExpectedException;
5167
import org.junit.runner.RunWith;
5268
import org.mockito.Mock;
69+
import org.mockito.Mockito;
5370
import org.mockito.runners.MockitoJUnitRunner;
5471
import org.springframework.aop.framework.ProxyFactory;
5572
import org.springframework.beans.factory.annotation.Value;
@@ -1866,6 +1883,21 @@ public void rejectsBasicDbListToBeConvertedIntoComplexType() {
18661883
converter.read(Item.class, source);
18671884
}
18681885

1886+
/**
1887+
* @see DATAMONGO-1058
1888+
*/
1889+
@Test
1890+
public void readShouldRespectExplicitFieldNameForDbRef() {
1891+
1892+
BasicDBObject source = new BasicDBObject();
1893+
source.append("explict-name-for-db-ref", new DBRef(mock(DB.class), "foo", "1"));
1894+
1895+
converter.read(ClassWithExplicitlyNamedDBRefProperty.class, source);
1896+
1897+
verify(resolver, times(1)).resolveDbRef(Mockito.any(MongoPersistentProperty.class), Mockito.any(DBRef.class),
1898+
Mockito.any(DbRefResolverCallback.class));
1899+
}
1900+
18691901
static class GenericType<T> {
18701902
T content;
18711903
}
@@ -2116,4 +2148,16 @@ class ClassWithGeoShape {
21162148

21172149
Shape shape;
21182150
}
2151+
2152+
class ClassWithExplicitlyNamedDBRefProperty {
2153+
2154+
@Field("explict-name-for-db-ref")//
2155+
@org.springframework.data.mongodb.core.mapping.DBRef//
2156+
ClassWithIntId dbRefProperty;
2157+
2158+
public ClassWithIntId getDbRefProperty() {
2159+
return dbRefProperty;
2160+
}
2161+
2162+
}
21192163
}

0 commit comments

Comments
 (0)