Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public ReferenceLookupDelegate(
}

if (!result.iterator().hasNext()) {
return null;
return property.isMap() ? Collections.emptyMap() : null;
}

Object resultValue = result.iterator().next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import org.bson.Document;
import org.bson.types.ObjectId;
Expand All @@ -35,6 +36,8 @@
import org.mockito.quality.Strictness;

import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.SpELContext;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.DocumentTestUtils;
import org.springframework.data.mongodb.core.MongoExceptionTranslator;
Expand All @@ -43,6 +46,9 @@
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.springframework.data.mongodb.core.mapping.DocumentReference;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;

/**
* Unit tests for {@link DefaultDbRefResolver}.
Expand All @@ -58,6 +64,8 @@ class DefaultDbRefResolverUnitTests {
@Mock MongoDatabase dbMock;
@Mock MongoCollection<Document> collectionMock;
@Mock FindIterable<Document> cursorMock;
@Mock MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
@Mock SpELContext spELContext;
private DefaultDbRefResolver resolver;

@BeforeEach
Expand Down Expand Up @@ -91,7 +99,7 @@ void bulkFetchShouldLoadDbRefsCorrectly() {
}

@Test // DATAMONGO-1194
void bulkFetchShouldThrowExceptionWhenUsingDifferntCollectionsWithinSetOfReferences() {
void bulkFetchShouldThrowExceptionWhenUsingDifferentCollectionsWithinSetOfReferences() {

DBRef ref1 = new DBRef("collection-1", new ObjectId());
DBRef ref2 = new DBRef("collection-2", new ObjectId());
Expand Down Expand Up @@ -134,4 +142,55 @@ void bulkFetchContainsDuplicates() {

assertThat(resolver.bulkFetch(Arrays.asList(ref1, ref2))).containsExactly(document, document);
}

@Test // GH-5065
void emptyMapWithDocumentReferenceAnnotationShouldDeserializeToAnEmptyMap() {
DocumentReference documentReference = mock(DocumentReference.class);
when(documentReference.lookup()).thenReturn("{ '_id' : ?#{#target} }");
when(documentReference.sort()).thenReturn("");
when(documentReference.lazy()).thenReturn(false);
MongoPersistentProperty property = mock(MongoPersistentProperty.class);
when(property.isCollectionLike()).thenReturn(false);
when(property.isMap()).thenReturn(true);
when(property.isDocumentReference()).thenReturn(true);
when(property.getDocumentReference()).thenReturn(documentReference);
DocumentReferenceSource source = mock(DocumentReferenceSource.class);
when(source.getTargetSource()).thenReturn(Document.parse("{}"));
ReferenceLookupDelegate lookupDelegate = new ReferenceLookupDelegate(mappingContext, spELContext);

ReferenceResolver.MongoEntityReader entityReader = mock(ReferenceResolver.MongoEntityReader.class);

Object target = resolver.resolveReference(property, source, lookupDelegate, entityReader);

assertThat(target)
.isNotNull()
.isInstanceOf(Map.class);
}

@Test // GH-5065
void lazyLoadedEmptyMapWithDocumentReferenceAnnotationShouldDeserializeToAnEmptyMapWithANonnullValuesProperty() {
DocumentReference documentReference = mock(DocumentReference.class);
when(documentReference.lookup()).thenReturn("{ '_id' : ?#{#target} }");
when(documentReference.sort()).thenReturn("");
when(documentReference.lazy()).thenReturn(true);
MongoPersistentProperty property = mock(MongoPersistentProperty.class);
when(property.isCollectionLike()).thenReturn(false);
when(property.isMap()).thenReturn(true);
when(property.isDocumentReference()).thenReturn(true);
when(property.getDocumentReference()).thenReturn(documentReference);
//noinspection rawtypes,unchecked
when(property.getType()).thenReturn((Class) Map.class);
DocumentReferenceSource source = mock(DocumentReferenceSource.class);
when(source.getTargetSource()).thenReturn(Document.parse("{}"));
ReferenceLookupDelegate lookupDelegate = new ReferenceLookupDelegate(mappingContext, spELContext);

ReferenceResolver.MongoEntityReader entityReader = mock(ReferenceResolver.MongoEntityReader.class);

Object target = resolver.resolveReference(property, source, lookupDelegate, entityReader);

assertThat(target)
.isNotNull()
.isInstanceOf(Map.class)
.asInstanceOf(MAP).values().isNotNull();
}
}
Loading