Skip to content

Commit 4e960a9

Browse files
christophstroblmp911de
authored andcommitted
Fix document reference on empty reference arrays.
This commit fixes an issue caused by empty reference arrays. Closes #3805 Original pull request: #3807.
1 parent 061c28f commit 4e960a9

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Collection;
2121
import java.util.Collections;
22+
import java.util.Iterator;
2223
import java.util.LinkedHashMap;
2324
import java.util.List;
2425
import java.util.Map;
@@ -122,7 +123,9 @@ private ReferenceCollection computeReferenceContext(MongoPersistentProperty prop
122123

123124
// Use the first value as a reference for others in case of collection like
124125
if (value instanceof Iterable) {
125-
value = ((Iterable<?>) value).iterator().next();
126+
127+
Iterator iterator = ((Iterable) value).iterator();
128+
value = iterator.hasNext() ? iterator.next() : new Document();
126129
}
127130

128131
// handle DBRef value
@@ -266,6 +269,10 @@ DocumentReferenceQuery computeFilter(MongoPersistentProperty property, Object so
266269
ors.add(decoded);
267270
}
268271

272+
if(ors.isEmpty()) {
273+
return new ListDocumentReferenceQuery(new Document("_id", new Document("$exists", false)), sort);
274+
}
275+
269276
return new ListDocumentReferenceQuery(new Document("$or", ors), sort);
270277
}
271278

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import lombok.Setter;
2626
import lombok.ToString;
2727

28+
import java.util.ArrayList;
2829
import java.util.Arrays;
2930
import java.util.Collections;
3031
import java.util.LinkedHashMap;
@@ -679,6 +680,41 @@ void loadCollectionReferenceWithMissingRefs() {
679680
assertThat(result.getSimpleValueRef()).containsExactly(new SimpleObjectRef("ref-2", "me-the-2-referenced-object"));
680681
}
681682

683+
@Test // GH-3805
684+
void loadEmptyCollectionReference() {
685+
686+
String rootCollectionName = template.getCollectionName(CollectionRefRoot.class);
687+
688+
// an empty reference array.
689+
Document source = new Document("_id", "id-1").append("value", "v1").append("simplePreinitializedValueRef",
690+
Collections.emptyList());
691+
692+
template.execute(db -> {
693+
db.getCollection(rootCollectionName).insertOne(source);
694+
return null;
695+
});
696+
697+
CollectionRefRoot result = template.findOne(query(where("id").is("id-1")), CollectionRefRoot.class);
698+
assertThat(result.simplePreinitializedValueRef).isEmpty();
699+
}
700+
701+
@Test // GH-3805
702+
void loadNoExistingCollectionReference() {
703+
704+
String rootCollectionName = template.getCollectionName(CollectionRefRoot.class);
705+
706+
// no reference array at all
707+
Document source = new Document("_id", "id-1").append("value", "v1");
708+
709+
template.execute(db -> {
710+
db.getCollection(rootCollectionName).insertOne(source);
711+
return null;
712+
});
713+
714+
CollectionRefRoot result = template.findOne(query(where("id").is("id-1")), CollectionRefRoot.class);
715+
assertThat(result.simplePreinitializedValueRef).isEmpty();
716+
}
717+
682718
@Test // GH-3602
683719
void queryForReference() {
684720

@@ -1122,6 +1158,9 @@ static class CollectionRefRoot {
11221158
@DocumentReference(lookup = "{ '_id' : '?#{#target}' }") //
11231159
List<SimpleObjectRef> simpleValueRef;
11241160

1161+
@DocumentReference
1162+
List<SimpleObjectRef> simplePreinitializedValueRef = new ArrayList<>();
1163+
11251164
@DocumentReference(lookup = "{ '_id' : '?#{#target}' }", sort = "{ '_id' : -1 } ") //
11261165
List<SimpleObjectRef> simpleSortedValueRef;
11271166

0 commit comments

Comments
 (0)