Skip to content

Commit 207c82a

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-2267 - Fix eager collection resolution in Object path.
We now lazily read the collection of an entity as it potentially requires a more expensive SpEL evaluation that might not have been required in fist place. Original pull request: #746.
1 parent 7596881 commit 207c82a

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
20+
import java.util.function.Supplier;
2021

2122
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
23+
import org.springframework.data.util.Lazy;
2224
import org.springframework.lang.Nullable;
2325
import org.springframework.util.Assert;
2426
import org.springframework.util.ClassUtils;
@@ -46,14 +48,14 @@ class ObjectPath {
4648
private final @Nullable ObjectPath parent;
4749
private final @Nullable Object object;
4850
private final @Nullable Object idValue;
49-
private final String collection;
51+
private final Lazy<String> collection;
5052

5153
private ObjectPath() {
5254

5355
this.parent = null;
5456
this.object = null;
5557
this.idValue = null;
56-
this.collection = "";
58+
this.collection = Lazy.empty();
5759
}
5860

5961
/**
@@ -64,7 +66,7 @@ private ObjectPath() {
6466
* @param idValue
6567
* @param collection
6668
*/
67-
private ObjectPath(ObjectPath parent, Object object, @Nullable Object idValue, String collection) {
69+
private ObjectPath(ObjectPath parent, Object object, @Nullable Object idValue, Lazy<String> collection) {
6870

6971
this.parent = parent;
7072
this.object = object;
@@ -85,7 +87,7 @@ ObjectPath push(Object object, MongoPersistentEntity<?> entity, @Nullable Object
8587
Assert.notNull(object, "Object must not be null!");
8688
Assert.notNull(entity, "MongoPersistentEntity must not be null!");
8789

88-
return new ObjectPath(this, object, id, entity.getCollection());
90+
return new ObjectPath(this, object, id, Lazy.of(() -> entity.getCollection()));
8991
}
9092

9193
/**
@@ -175,7 +177,7 @@ private Object getIdValue() {
175177
}
176178

177179
private String getCollection() {
178-
return collection;
180+
return collection.get();
179181
}
180182

181183
/*

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.mongodb.core.convert;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
1920

2021
import org.junit.Before;
2122
import org.junit.Test;
@@ -81,6 +82,19 @@ public void getPathItemShouldReturnNullWhenIdAndCollectionMatchAndAssignableToIn
8182
assertThat(path.getPathItem("id-1", "one", ValueInterface.class)).isNotNull();
8283
}
8384

85+
@Test // DATAMONGO-2267
86+
public void collectionLookupShouldBeLazy/* because we may need to resolve SpEL which can be pretty expensive */() {
87+
88+
MongoPersistentEntity<EntityOne> spied = spy(one);
89+
ObjectPath path = ObjectPath.ROOT.push(new EntityThree(), spied, "id-1");
90+
91+
verify(spied, never()).getCollection();
92+
93+
path.getPathItem("id-1", "foo", EntityTwo.class);
94+
95+
verify(spied).getCollection();
96+
}
97+
8498
@Document("one")
8599
static class EntityOne {
86100

0 commit comments

Comments
 (0)