Skip to content

Commit

Permalink
Skip reference lookup when DocumentReference resolves to an empty col…
Browse files Browse the repository at this point in the history
…lection.

Fixes #4612
Original pull request: #4613
  • Loading branch information
stefanbildl authored and mp911de committed Apr 11, 2024
1 parent 37aaf53 commit 4ec995f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Stefan Bildl
* @since 3.3
*/
public final class ReferenceLookupDelegate {
Expand Down Expand Up @@ -106,6 +107,11 @@ public Object readReference(MongoPersistentProperty property, Object source, Loo
Object value = source instanceof DocumentReferenceSource documentReferenceSource ? documentReferenceSource.getTargetSource()
: source;

// GH-4612 no need to query database if target collection is empty
if (value != null && property.isCollectionLike() && (value instanceof Collection<?> c) && c.isEmpty()) {
return new ArrayList<>();
}

DocumentReferenceQuery filter = computeFilter(property, source, spELContext);
ReferenceCollection referenceCollection = computeReferenceContext(property, value, spELContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,23 @@
*/
package org.springframework.data.mongodb.core.convert;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Collections;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.SpELContext;
import org.springframework.data.mongodb.core.convert.ReferenceResolver.MongoEntityReader;
import org.springframework.data.mongodb.core.mapping.DocumentReference;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

/**
* Unit tests for {@link ReferenceLookupDelegate}.
Expand All @@ -52,9 +50,17 @@ class ReferenceLookupDelegateUnitTests {

@BeforeEach
void beforeEach() {

lookupDelegate = new ReferenceLookupDelegate(mappingContext, spELContext);
when(spELContext.getParser()).thenReturn(new SpelExpressionParser());
}

@Test // GH-4612
void shouldResolveEmptyListOnEmptyTargetCollection() {
MongoPersistentProperty property = mock(MongoPersistentProperty.class);
ReferenceLookupDelegate.LookupFunction lookupFunction = mock(ReferenceLookupDelegate.LookupFunction.class);

when(property.isCollectionLike()).thenReturn(true);
lookupDelegate.readReference(property, Collections.emptyList(), lookupFunction, entityReader);
verify(lookupFunction, never()).apply(any(), any());
}

@Test // GH-3842
Expand Down

0 comments on commit 4ec995f

Please sign in to comment.