Skip to content

Fix document reference on empty reference arrays. #3807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3805-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3805-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3805-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3805-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -119,7 +120,9 @@ private ReferenceCollection computeReferenceContext(MongoPersistentProperty prop

// Use the first value as a reference for others in case of collection like
if (value instanceof Iterable) {
value = ((Iterable<?>) value).iterator().next();

Iterator iterator = ((Iterable) value).iterator();
value = iterator.hasNext() ? iterator.next() : new Document();
}

// handle DBRef value
Expand Down Expand Up @@ -247,6 +250,10 @@ DocumentReferenceQuery computeFilter(MongoPersistentProperty property, Object va
ors.add(decoded);
}

if(ors.isEmpty()) {
return new ListDocumentReferenceQuery(new Document("_id", new Document("$exists", false)), sort);
}

return new ListDocumentReferenceQuery(new Document("$or", ors), sort);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -678,6 +679,41 @@ void loadCollectionReferenceWithMissingRefs() {
assertThat(result.getSimpleValueRef()).containsExactly(new SimpleObjectRef("ref-2", "me-the-2-referenced-object"));
}

@Test // GH-3805
void loadEmptyCollectionReference() {

String rootCollectionName = template.getCollectionName(CollectionRefRoot.class);

// an empty reference array.
Document source = new Document("_id", "id-1").append("value", "v1").append("simplePreinitializedValueRef",
Collections.emptyList());

template.execute(db -> {
db.getCollection(rootCollectionName).insertOne(source);
return null;
});

CollectionRefRoot result = template.findOne(query(where("id").is("id-1")), CollectionRefRoot.class);
assertThat(result.simplePreinitializedValueRef).isEmpty();
}

@Test // GH-3805
void loadNoExistingCollectionReference() {

String rootCollectionName = template.getCollectionName(CollectionRefRoot.class);

// no reference array at all
Document source = new Document("_id", "id-1").append("value", "v1");

template.execute(db -> {
db.getCollection(rootCollectionName).insertOne(source);
return null;
});

CollectionRefRoot result = template.findOne(query(where("id").is("id-1")), CollectionRefRoot.class);
assertThat(result.simplePreinitializedValueRef).isEmpty();
}

@Test // GH-3602
void queryForReference() {

Expand Down Expand Up @@ -1094,6 +1130,9 @@ static class CollectionRefRoot {
@DocumentReference(lookup = "{ '_id' : '?#{#target}' }") //
List<SimpleObjectRef> simpleValueRef;

@DocumentReference
List<SimpleObjectRef> simplePreinitializedValueRef = new ArrayList<>();

@DocumentReference(lookup = "{ '_id' : '?#{#target}' }", sort = "{ '_id' : -1 } ") //
List<SimpleObjectRef> simpleSortedValueRef;

Expand Down