Skip to content

Commit 6b1d95b

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1421 - Fix serialization in error message causing error itself.
We now make sure to safely serialize the criteria object used for creating the error message when raising an `InvalidMongoDbApiUsageException` in cases where `addCriteria` is used to add multiple entries for the same property. Original pull request: #448.
1 parent 0171084 commit 6b1d95b

File tree

2 files changed

+34
-17
lines changed
  • spring-data-mongodb/src
    • main/java/org/springframework/data/mongodb/core/query
    • test/java/org/springframework/data/mongodb/core/query

2 files changed

+34
-17
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 the original author or authors.
2+
* Copyright 2010-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,7 +58,7 @@ public class Query {
5858

5959
/**
6060
* Static factory method to create a {@link Query} using the provided {@link CriteriaDefinition}.
61-
*
61+
*
6262
* @param criteriaDefinition must not be {@literal null}.
6363
* @return
6464
* @since 1.6
@@ -71,7 +71,7 @@ public Query() {}
7171

7272
/**
7373
* Creates a new {@link Query} using the given {@link CriteriaDefinition}.
74-
*
74+
*
7575
* @param criteriaDefinition must not be {@literal null}.
7676
* @since 1.6
7777
*/
@@ -81,7 +81,7 @@ public Query(CriteriaDefinition criteriaDefinition) {
8181

8282
/**
8383
* Adds the given {@link CriteriaDefinition} to the current {@link Query}.
84-
*
84+
*
8585
* @param criteriaDefinition must not be {@literal null}.
8686
* @return
8787
* @since 1.6
@@ -96,7 +96,7 @@ public Query addCriteria(CriteriaDefinition criteriaDefinition) {
9696
} else {
9797
throw new InvalidMongoDbApiUsageException(
9898
"Due to limitations of the com.mongodb.BasicDBObject, " + "you can't add a second '" + key + "' criteria. "
99-
+ "Query already contains '" + existing.getCriteriaObject() + "'.");
99+
+ "Query already contains '" + serializeToJsonSafely(existing.getCriteriaObject()) + "'.");
100100
}
101101

102102
return this;
@@ -111,7 +111,7 @@ public Field fields() {
111111

112112
/**
113113
* Set number of documents to skip before returning results.
114-
*
114+
*
115115
* @param skip
116116
* @return
117117
*/
@@ -122,7 +122,7 @@ public Query skip(int skip) {
122122

123123
/**
124124
* Limit the number of returned documents to {@code limit}.
125-
*
125+
*
126126
* @param limit
127127
* @return
128128
*/
@@ -133,7 +133,7 @@ public Query limit(int limit) {
133133

134134
/**
135135
* Configures the query to use the given hint when being executed.
136-
*
136+
*
137137
* @param name must not be {@literal null} or empty.
138138
* @return
139139
*/
@@ -146,7 +146,7 @@ public Query withHint(String name) {
146146
/**
147147
* Sets the given pagination information on the {@link Query} instance. Will transparently set {@code skip} and
148148
* {@code limit} as well as applying the {@link Sort} instance defined with the {@link Pageable}.
149-
*
149+
*
150150
* @param pageable
151151
* @return
152152
*/
@@ -164,7 +164,7 @@ public Query with(Pageable pageable) {
164164

165165
/**
166166
* Adds a {@link Sort} to the {@link Query} instance.
167-
*
167+
*
168168
* @param sort
169169
* @return
170170
*/
@@ -199,7 +199,7 @@ public Set<Class<?>> getRestrictedTypes() {
199199

200200
/**
201201
* Restricts the query to only return documents instances that are exactly of the given types.
202-
*
202+
*
203203
* @param type may not be {@literal null}
204204
* @param additionalTypes may not be {@literal null}
205205
* @return
@@ -253,7 +253,7 @@ public DBObject getSortObject() {
253253

254254
/**
255255
* Get the number of documents to skip.
256-
*
256+
*
257257
* @return
258258
*/
259259
public int getSkip() {
@@ -262,7 +262,7 @@ public int getSkip() {
262262

263263
/**
264264
* Get the maximum number of documents to be return.
265-
*
265+
*
266266
* @return
267267
*/
268268
public int getLimit() {
@@ -388,7 +388,7 @@ public boolean equals(Object obj) {
388388

389389
/**
390390
* Tests whether the settings of the given {@link Query} are equal to this query.
391-
*
391+
*
392392
* @param that
393393
* @return
394394
*/
@@ -405,7 +405,7 @@ protected boolean querySettingsEquals(Query that) {
405405
return criteriaEqual && fieldsEqual && sortEqual && hintEqual && skipEqual && limitEqual && metaEqual;
406406
}
407407

408-
/*
408+
/*
409409
* (non-Javadoc)
410410
* @see java.lang.Object#hashCode()
411411
*/
@@ -427,7 +427,7 @@ public int hashCode() {
427427

428428
/**
429429
* Returns whether the given key is the one used to hold the type restriction information.
430-
*
430+
*
431431
* @deprecated don't call this method as the restricted type handling will undergo some significant changes going
432432
* forward.
433433
* @param key

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333

3434
/**
3535
* Unit tests for {@link Query}.
36-
*
36+
*
3737
* @author Thomas Risberg
3838
* @author Oliver Gierke
3939
* @author Patryk Wasik
4040
* @author Thomas Darimont
41+
* @author Christoph Strobl
4142
*/
4243
public class QueryTests {
4344

@@ -213,4 +214,20 @@ public void shouldReturnClassHierarchyOfRestrictedTypes() {
213214
assertThat(query.getRestrictedTypes().size(), is(1));
214215
assertThat(query.getRestrictedTypes(), hasItems(Arrays.asList(SpecialDoc.class).toArray(new Class<?>[0])));
215216
}
217+
218+
@Test // DATAMONGO-1421
219+
public void addCriteriaForSamePropertyMultipleTimesShouldThrowAndSafelySerializeErrorMessage() {
220+
221+
exception.expect(InvalidMongoDbApiUsageException.class);
222+
exception.expectMessage("second 'value' criteria");
223+
exception.expectMessage("already contains '{ \"value\" : { $java : VAL_1 } }'");
224+
225+
Query query = new Query();
226+
query.addCriteria(where("value").is(EnumType.VAL_1));
227+
query.addCriteria(where("value").is(EnumType.VAL_2));
228+
}
229+
230+
enum EnumType {
231+
VAL_1, VAL_2
232+
}
216233
}

0 commit comments

Comments
 (0)