Skip to content
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

Added include() and exclude() methods that accept Collection in field projection #4668

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -16,6 +16,7 @@
package org.springframework.data.mongodb.core.query;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -35,6 +36,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Owen Q
* @author Kirill Egorov
*/
public class Field {

Expand Down Expand Up @@ -124,6 +126,17 @@ public Field projectAs(MongoExpression expression, String field) {
*/
public Field include(String... fields) {

return include(Arrays.asList(fields));
}

/**
* Include one or more {@code fields} to be returned by the query operation.
*
* @param fields the document field names to be included.
* @return {@code this} field projection instance.
*/
public Field include(Collection<String> fields) {

Assert.notNull(fields, "Keys must not be null");

for (String key : fields) {
Expand All @@ -136,7 +149,7 @@ public Field include(String... fields) {
/**
* Exclude a single {@code field} from being returned by the query operation.
*
* @param field the document field name to be included.
* @param field the document field name to be excluded.
* @return {@code this} field projection instance.
*/
public Field exclude(String field) {
Expand All @@ -151,12 +164,23 @@ public Field exclude(String field) {
/**
* Exclude one or more {@code fields} from being returned by the query operation.
*
* @param fields the document field names to be included.
* @param fields the document field names to be excluded.
* @return {@code this} field projection instance.
* @since 3.1
*/
public Field exclude(String... fields) {

return exclude(Arrays.asList(fields));
}

/**
* Exclude one or more {@code fields} from being returned by the query operation.
*
* @param fields the document field names to be excluded.
* @return {@code this} field projection instance.
*/
public Field exclude(Collection<String> fields) {

Assert.notNull(fields, "Keys must not be null");

for (String key : fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* @author Mark Paluch
* @author Mehran Behnam
* @author Jens Schauder
* @author Kirill Egorov
*/
public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {

Expand Down Expand Up @@ -485,7 +486,7 @@ private ExecutableFindOperation.TerminatingFind<T> createQuery(UnaryOperator<Que
query.limit(getLimit());

if (!getFieldsToInclude().isEmpty()) {
query.fields().include(getFieldsToInclude().toArray(new String[0]));
query.fields().include(getFieldsToInclude());
}

getReadPreference().ifPresent(query::withReadPreference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
* @author Ruben J Garcia
* @author Jens Schauder
* @author Clément Petit
* @author Kirill Egorov
* @since 2.0
*/
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {
Expand Down Expand Up @@ -555,7 +556,7 @@ private ReactiveFindOperation.TerminatingFind<T> createQuery(UnaryOperator<Query
query.limit(getLimit());

if (!getFieldsToInclude().isEmpty()) {
query.fields().include(getFieldsToInclude().toArray(new String[0]));
query.fields().include(getFieldsToInclude());
}

readPreference.ifPresent(query::withReadPreference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@

import org.junit.jupiter.api.Test;

import java.util.List;

/**
* Unit tests for {@link Field}.
*
* @author Oliver Gierke
* @author Owen Q
* @author Mark Paluch
* @author Kirill Egorov
*/
class FieldUnitTests {

Expand Down Expand Up @@ -64,4 +67,22 @@ void rendersExclusionCorrectly() {

assertThat(fields.getFieldsObject()).isEqualTo("{foo:0, bar:0, baz:0}");
}

@Test // GH-4625
void overriddenInclusionMethodsCreateEqualFields() {

Field left = new Field().include("foo", "bar");
Field right = new Field().include(List.of("foo", "bar"));

assertThat(left).isEqualTo(right);
}

@Test // GH-4625
void overriddenExclusionMethodsCreateEqualFields() {

Field left = new Field().exclude("foo", "bar");
Field right = new Field().exclude(List.of("foo", "bar"));

assertThat(left).isEqualTo(right);
}
}
Loading