Skip to content

Commit 06bc4fd

Browse files
John Willeminchristophstrobl
John Willemin
authored andcommitted
DATAMONGO-1387 - Fix BasicQuery getFieldsObject() inconsistency.
We changed BasicQuery to consider its parent getFieldsObject() when not given an explicit fields DBObject. Original Pull Request: #345 CLA: 165520160303021604 (John Willemin)
1 parent 2687cb8 commit 06bc4fd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/BasicQuery.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @author Oliver Gierke
2929
* @author Christoph Strobl
3030
* @author Thomas Darimont
31+
* @author John Willemin
3132
*/
3233
public class BasicQuery extends Query {
3334

@@ -70,7 +71,11 @@ public DBObject getQueryObject() {
7071

7172
@Override
7273
public DBObject getFieldsObject() {
73-
return fieldsObject;
74+
if(fieldsObject != null) {
75+
return fieldsObject;
76+
} else {
77+
return super.getFieldsObject();
78+
}
7479
}
7580

7681
@Override

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*
3333
* @author Oliver Gierke
3434
* @author Thomas Darimont
35+
* @author John Willemin
3536
*/
3637
public class BasicQueryUnitTests {
3738

@@ -137,4 +138,21 @@ public void handlesEqualsAndHashCodeCorrectlyWhenQuerySettingsDiffer() {
137138
assertThat(query1, is(not(equalTo(query2))));
138139
assertThat(query1.hashCode(), is(not(query2.hashCode())));
139140
}
141+
142+
/**
143+
* @see DATAMONGO-1387
144+
*/
145+
@Test
146+
public void handlesFieldsIncludeCorrectly() {
147+
148+
String qry = "{ \"name\" : \"Thomas\"}";
149+
150+
BasicQuery query1 = new BasicQuery(qry);
151+
query1.fields().include("name");
152+
153+
DBObject fieldsObject = query1.getFieldsObject();
154+
fieldsObject.containsField("name");
155+
assertThat(query1.getFieldsObject(), notNullValue());
156+
assertThat(query1.getFieldsObject().containsField("name"), is(true));
157+
}
140158
}

0 commit comments

Comments
 (0)