Skip to content

Commit c37dfd9

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 98d655f commit c37dfd9

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-2016 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.
@@ -57,7 +57,7 @@ public class Query {
5757

5858
/**
5959
* Static factory method to create a {@link Query} using the provided {@link CriteriaDefinition}.
60-
*
60+
*
6161
* @param criteriaDefinition must not be {@literal null}.
6262
* @return
6363
* @since 1.6
@@ -70,7 +70,7 @@ public Query() {}
7070

7171
/**
7272
* Creates a new {@link Query} using the given {@link CriteriaDefinition}.
73-
*
73+
*
7474
* @param criteriaDefinition must not be {@literal null}.
7575
* @since 1.6
7676
*/
@@ -80,7 +80,7 @@ public Query(CriteriaDefinition criteriaDefinition) {
8080

8181
/**
8282
* Adds the given {@link CriteriaDefinition} to the current {@link Query}.
83-
*
83+
*
8484
* @param criteriaDefinition must not be {@literal null}.
8585
* @return
8686
* @since 1.6
@@ -95,7 +95,7 @@ public Query addCriteria(CriteriaDefinition criteriaDefinition) {
9595
} else {
9696
throw new InvalidMongoDbApiUsageException(
9797
"Due to limitations of the com.mongodb.BasicDocument, " + "you can't add a second '" + key + "' criteria. "
98-
+ "Query already contains '" + existing.getCriteriaObject() + "'.");
98+
+ "Query already contains '" + serializeToJsonSafely(existing.getCriteriaObject()) + "'.");
9999
}
100100

101101
return this;
@@ -110,7 +110,7 @@ public Field fields() {
110110

111111
/**
112112
* Set number of documents to skip before returning results.
113-
*
113+
*
114114
* @param skip
115115
* @return
116116
*/
@@ -121,7 +121,7 @@ public Query skip(int skip) {
121121

122122
/**
123123
* Limit the number of returned documents to {@code limit}.
124-
*
124+
*
125125
* @param limit
126126
* @return
127127
*/
@@ -132,7 +132,7 @@ public Query limit(int limit) {
132132

133133
/**
134134
* Configures the query to use the given hint when being executed.
135-
*
135+
*
136136
* @param name must not be {@literal null} or empty.
137137
* @return
138138
*/
@@ -145,7 +145,7 @@ public Query withHint(String name) {
145145
/**
146146
* Sets the given pagination information on the {@link Query} instance. Will transparently set {@code skip} and
147147
* {@code limit} as well as applying the {@link Sort} instance defined with the {@link Pageable}.
148-
*
148+
*
149149
* @param pageable
150150
* @return
151151
*/
@@ -163,7 +163,7 @@ public Query with(Pageable pageable) {
163163

164164
/**
165165
* Adds a {@link Sort} to the {@link Query} instance.
166-
*
166+
*
167167
* @param sort
168168
* @return
169169
*/
@@ -198,7 +198,7 @@ public Set<Class<?>> getRestrictedTypes() {
198198

199199
/**
200200
* Restricts the query to only return documents instances that are exactly of the given types.
201-
*
201+
*
202202
* @param type may not be {@literal null}
203203
* @param additionalTypes may not be {@literal null}
204204
* @return
@@ -252,7 +252,7 @@ public Document getSortObject() {
252252

253253
/**
254254
* Get the number of documents to skip.
255-
*
255+
*
256256
* @return
257257
*/
258258
public int getSkip() {
@@ -261,7 +261,7 @@ public int getSkip() {
261261

262262
/**
263263
* Get the maximum number of documents to be return.
264-
*
264+
*
265265
* @return
266266
*/
267267
public int getLimit() {
@@ -431,7 +431,7 @@ public boolean equals(Object obj) {
431431

432432
/**
433433
* Tests whether the settings of the given {@link Query} are equal to this query.
434-
*
434+
*
435435
* @param that
436436
* @return
437437
*/
@@ -448,7 +448,7 @@ protected boolean querySettingsEquals(Query that) {
448448
return criteriaEqual && fieldsEqual && sortEqual && hintEqual && skipEqual && limitEqual && metaEqual;
449449
}
450450

451-
/*
451+
/*
452452
* (non-Javadoc)
453453
* @see java.lang.Object#hashCode()
454454
*/
@@ -470,7 +470,7 @@ public int hashCode() {
470470

471471
/**
472472
* Returns whether the given key is the one used to hold the type restriction information.
473-
*
473+
*
474474
* @deprecated don't call this method as the restricted type handling will undergo some significant changes going
475475
* forward.
476476
* @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
@@ -34,11 +34,12 @@
3434

3535
/**
3636
* Unit tests for {@link Query}.
37-
*
37+
*
3838
* @author Thomas Risberg
3939
* @author Oliver Gierke
4040
* @author Patryk Wasik
4141
* @author Thomas Darimont
42+
* @author Christoph Strobl
4243
*/
4344
public class QueryTests {
4445

@@ -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)