Skip to content

Commit 5e2f16c

Browse files
committed
DATAMONGO-508 - Eagerly return DBRef creation if the given value already is a DBRef.
1 parent d7ae95a commit 5e2f16c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,10 @@ protected DBRef createDBRef(Object target, org.springframework.data.mongodb.core
671671

672672
Assert.notNull(target);
673673

674+
if (target instanceof DBRef) {
675+
return (DBRef) target;
676+
}
677+
674678
MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass());
675679

676680
if (null == targetEntity) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,20 @@ private static void assertSyntheticFieldValueOf(Object target, Object expected)
12781278
fail(String.format("Didn't find synthetic field on %s!", target));
12791279
}
12801280

1281+
/**
1282+
* @see DATAMGONGO-508
1283+
*/
1284+
@Test
1285+
public void eagerlyReturnsDBRefObjectIfTargetAlreadyIsOne() {
1286+
1287+
DB db = mock(DB.class);
1288+
DBRef dbRef = new DBRef(db, "collection", "id");
1289+
1290+
org.springframework.data.mongodb.core.mapping.DBRef annotation = mock(org.springframework.data.mongodb.core.mapping.DBRef.class);
1291+
1292+
assertThat(converter.createDBRef(dbRef, annotation), is(dbRef));
1293+
}
1294+
12811295
static class GenericType<T> {
12821296
T content;
12831297
}

0 commit comments

Comments
 (0)