Skip to content

Commit 4a5789d

Browse files
christophstroblmp911de
authored andcommitted
DocumentReference should consider Reference annotations.
Closes #3851 Original pull request: #3852.
1 parent 7b05cfa commit 4a5789d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor acce
828828
return;
829829
}
830830

831-
if (prop.isAssociation()) {
831+
if (prop.isAssociation() && prop.isAnnotationPresent(Reference.class)) {
832832

833833
accessor.put(prop, new DocumentPointerFactory(conversionService, mappingContext)
834834
.computePointer(mappingContext, prop, obj, valueType.getType()).getPointer());

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

+19
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,26 @@ void readsMapThatDoesNotComeAsDocument() {
26142614
ClassWithMapProperty target = converter.read(ClassWithMapProperty.class, source);
26152615

26162616
assertThat(target.mapOfObjects).containsEntry("simple",1);
2617+
}
2618+
2619+
@Test // GH-3851
2620+
void associationMappingShouldFallBackToDefaultIfNoAtReferenceAnnotationPresent/* as done via jmolecules */() {
2621+
2622+
UUID id = UUID.randomUUID();
2623+
Person sourceValue = new Person();
2624+
sourceValue.id = id.toString();
2625+
2626+
DocumentAccessor accessor = new DocumentAccessor(new org.bson.Document());
2627+
MongoPersistentProperty persistentProperty = mock(MongoPersistentProperty.class);
2628+
when(persistentProperty.isAssociation()).thenReturn(true);
2629+
when(persistentProperty.getFieldName()).thenReturn("pName");
2630+
doReturn(ClassTypeInformation.from(Person.class)).when(persistentProperty).getTypeInformation();
2631+
doReturn(Person.class).when(persistentProperty).getType();
2632+
doReturn(Person.class).when(persistentProperty).getRawType();
2633+
2634+
converter.writePropertyInternal(sourceValue, accessor, persistentProperty);
26172635

2636+
assertThat(accessor.getDocument()).isEqualTo(new org.bson.Document("pName", new org.bson.Document("_id", id.toString())));
26182637
}
26192638

26202639
static class GenericType<T> {

0 commit comments

Comments
 (0)