Skip to content

Commit 0ab635d

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 ef46edc commit 0ab635d

File tree

2 files changed

+20
-3
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

+20
-3
lines changed

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

Lines changed: 2 additions & 2 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.
@@ -97,7 +97,7 @@ public Query addCriteria(CriteriaDefinition criteriaDefinition) {
9797
} else {
9898
throw new InvalidMongoDbApiUsageException(
9999
"Due to limitations of the com.mongodb.BasicDBObject, " + "you can't add a second '" + key + "' criteria. "
100-
+ "Query already contains '" + existing.getCriteriaObject() + "'.");
100+
+ "Query already contains '" + serializeToJsonSafely(existing.getCriteriaObject()) + "'.");
101101
}
102102

103103
return this;

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

@@ -204,4 +205,20 @@ public void shouldReturnClassHierarchyOfRestrictedTypes() {
204205
assertThat(query.getRestrictedTypes().size(), is(1));
205206
assertThat(query.getRestrictedTypes(), hasItems(Arrays.asList(SpecialDoc.class).toArray(new Class<?>[0])));
206207
}
208+
209+
@Test // DATAMONGO-1421
210+
public void addCriteriaForSamePropertyMultipleTimesShouldThrowAndSafelySerializeErrorMessage() {
211+
212+
exception.expect(InvalidMongoDbApiUsageException.class);
213+
exception.expectMessage("second 'value' criteria");
214+
exception.expectMessage("already contains '{ \"value\" : { $java : VAL_1 } }'");
215+
216+
Query query = new Query();
217+
query.addCriteria(where("value").is(EnumType.VAL_1));
218+
query.addCriteria(where("value").is(EnumType.VAL_2));
219+
}
220+
221+
enum EnumType {
222+
VAL_1, VAL_2
223+
}
207224
}

0 commit comments

Comments
 (0)