Skip to content

Commit 02c30bf

Browse files
author
Kirill Egorov
committed
Added include() and exclude() methods that accept Collection in field projection
1 parent d294d50 commit 02c30bf

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.mongodb.core.query;
1717

1818
import java.util.Arrays;
19+
import java.util.Collection;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122
import java.util.Map.Entry;
@@ -35,6 +36,7 @@
3536
* @author Christoph Strobl
3637
* @author Mark Paluch
3738
* @author Owen Q
39+
* @author Kirill Egorov
3840
*/
3941
public class Field {
4042

@@ -124,6 +126,17 @@ public Field projectAs(MongoExpression expression, String field) {
124126
*/
125127
public Field include(String... fields) {
126128

129+
return include(Arrays.asList(fields));
130+
}
131+
132+
/**
133+
* Include one or more {@code fields} to be returned by the query operation.
134+
*
135+
* @param fields the document field names to be included.
136+
* @return {@code this} field projection instance.
137+
*/
138+
public Field include(Collection<String> fields) {
139+
127140
Assert.notNull(fields, "Keys must not be null");
128141

129142
for (String key : fields) {
@@ -136,7 +149,7 @@ public Field include(String... fields) {
136149
/**
137150
* Exclude a single {@code field} from being returned by the query operation.
138151
*
139-
* @param field the document field name to be included.
152+
* @param field the document field name to be excluded.
140153
* @return {@code this} field projection instance.
141154
*/
142155
public Field exclude(String field) {
@@ -151,12 +164,23 @@ public Field exclude(String field) {
151164
/**
152165
* Exclude one or more {@code fields} from being returned by the query operation.
153166
*
154-
* @param fields the document field names to be included.
167+
* @param fields the document field names to be excluded.
155168
* @return {@code this} field projection instance.
156169
* @since 3.1
157170
*/
158171
public Field exclude(String... fields) {
159172

173+
return exclude(Arrays.asList(fields));
174+
}
175+
176+
/**
177+
* Exclude one or more {@code fields} from being returned by the query operation.
178+
*
179+
* @param fields the document field names to be excluded.
180+
* @return {@code this} field projection instance.
181+
*/
182+
public Field exclude(Collection<String> fields) {
183+
160184
Assert.notNull(fields, "Keys must not be null");
161185

162186
for (String key : fields) {

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* @author Mark Paluch
6161
* @author Mehran Behnam
6262
* @author Jens Schauder
63+
* @author Kirill Egorov
6364
*/
6465
public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
6566

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

487488
if (!getFieldsToInclude().isEmpty()) {
488-
query.fields().include(getFieldsToInclude().toArray(new String[0]));
489+
query.fields().include(getFieldsToInclude());
489490
}
490491

491492
getReadPreference().ifPresent(query::withReadPreference);

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
* @author Ruben J Garcia
6464
* @author Jens Schauder
6565
* @author Clément Petit
66+
* @author Kirill Egorov
6667
* @since 2.0
6768
*/
6869
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {
@@ -555,7 +556,7 @@ private ReactiveFindOperation.TerminatingFind<T> createQuery(UnaryOperator<Query
555556
query.limit(getLimit());
556557

557558
if (!getFieldsToInclude().isEmpty()) {
558-
query.fields().include(getFieldsToInclude().toArray(new String[0]));
559+
query.fields().include(getFieldsToInclude());
559560
}
560561

561562
readPreference.ifPresent(query::withReadPreference);

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919

2020
import org.junit.jupiter.api.Test;
2121

22+
import java.util.List;
23+
2224
/**
2325
* Unit tests for {@link Field}.
2426
*
2527
* @author Oliver Gierke
2628
* @author Owen Q
2729
* @author Mark Paluch
30+
* @author Kirill Egorov
2831
*/
2932
class FieldUnitTests {
3033

@@ -64,4 +67,22 @@ void rendersExclusionCorrectly() {
6467

6568
assertThat(fields.getFieldsObject()).isEqualTo("{foo:0, bar:0, baz:0}");
6669
}
70+
71+
@Test // GH-4625
72+
void overriddenInclusionMethodsCreateEqualFields() {
73+
74+
Field left = new Field().include("foo", "bar");
75+
Field right = new Field().include(List.of("foo", "bar"));
76+
77+
assertThat(left).isEqualTo(right);
78+
}
79+
80+
@Test // GH-4625
81+
void overriddenExclusionMethodsCreateEqualFields() {
82+
83+
Field left = new Field().exclude("foo", "bar");
84+
Field right = new Field().exclude(List.of("foo", "bar"));
85+
86+
assertThat(left).isEqualTo(right);
87+
}
6788
}

0 commit comments

Comments
 (0)