Skip to content

Commit

Permalink
DATAMONGO-1058 - DBRef should respect explicit field name.
Browse files Browse the repository at this point in the history
We now use property.getFieldName() for mapping DbRefs. This assures we also capture explicitly defined names set via @field.

Original pull request: #227.
  • Loading branch information
christophstrobl authored and Thomas Darimont committed Oct 1, 2014
1 parent 2780f60 commit 17e0154
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Expand Up @@ -284,7 +284,7 @@ public void doWithPersistentProperty(MongoPersistentProperty prop) {
public void doWithAssociation(Association<MongoPersistentProperty> association) {

final MongoPersistentProperty property = association.getInverse();
Object value = dbo.get(property.getName());
Object value = dbo.get(property.getFieldName());

if (value == null) {
return;
Expand Down
Expand Up @@ -2791,6 +2791,7 @@ static class DocumentWithDBRefCollection {

@Id public String id;

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

Expand Down
Expand Up @@ -50,6 +50,7 @@
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -1853,6 +1854,21 @@ public void rejectsBasicDbListToBeConvertedIntoComplexType() {
converter.read(Item.class, source);
}

/**
* @see DATAMONGO-1058
*/
@Test
public void readShouldRespectExplicitFieldNameForDbRef() {

BasicDBObject source = new BasicDBObject();
source.append("explict-name-for-db-ref", new DBRef(mock(DB.class), "foo", "1"));

converter.read(ClassWithExplicitlyNamedDBRefProperty.class, source);

verify(resolver, times(1)).resolveDbRef(Mockito.any(MongoPersistentProperty.class), Mockito.any(DBRef.class),
Mockito.any(DbRefResolverCallback.class), Mockito.any(DbRefProxyHandler.class));
}

static class GenericType<T> {
T content;
}
Expand Down Expand Up @@ -2102,4 +2118,16 @@ class ClassWithTextScoreProperty {

@TextScore Float score;
}

class ClassWithExplicitlyNamedDBRefProperty {

@Field("explict-name-for-db-ref")//
@org.springframework.data.mongodb.core.mapping.DBRef//
ClassWithIntId dbRefProperty;

public ClassWithIntId getDbRefProperty() {
return dbRefProperty;
}

}
}

0 comments on commit 17e0154

Please sign in to comment.