From 0ab635d7eefdc5b64075e3f4e40680c71b30f7fc Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 6 Mar 2017 09:03:34 +0100 Subject: [PATCH] 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. --- .../data/mongodb/core/query/Query.java | 4 ++-- .../data/mongodb/core/query/QueryTests.java | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java index cd2668bae..bf2a39cda 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -97,7 +97,7 @@ public class Query { } else { throw new InvalidMongoDbApiUsageException( "Due to limitations of the com.mongodb.BasicDBObject, " + "you can't add a second '" + key + "' criteria. " - + "Query already contains '" + existing.getCriteriaObject() + "'."); + + "Query already contains '" + serializeToJsonSafely(existing.getCriteriaObject()) + "'."); } return this; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java index a152374e1..5c7b384a1 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java @@ -33,11 +33,12 @@ import org.springframework.data.mongodb.core.SpecialDoc; /** * Unit tests for {@link Query}. - * + * * @author Thomas Risberg * @author Oliver Gierke * @author Patryk Wasik * @author Thomas Darimont + * @author Christoph Strobl */ public class QueryTests { @@ -204,4 +205,20 @@ public class QueryTests { assertThat(query.getRestrictedTypes().size(), is(1)); assertThat(query.getRestrictedTypes(), hasItems(Arrays.asList(SpecialDoc.class).toArray(new Class[0]))); } + + @Test // DATAMONGO-1421 + public void addCriteriaForSamePropertyMultipleTimesShouldThrowAndSafelySerializeErrorMessage() { + + exception.expect(InvalidMongoDbApiUsageException.class); + exception.expectMessage("second 'value' criteria"); + exception.expectMessage("already contains '{ \"value\" : { $java : VAL_1 } }'"); + + Query query = new Query(); + query.addCriteria(where("value").is(EnumType.VAL_1)); + query.addCriteria(where("value").is(EnumType.VAL_2)); + } + + enum EnumType { + VAL_1, VAL_2 + } }