DATAMONGO-2228 - Fixed loosing branches in AND expressions in MongodbDocumentSerializer.
Original Pull Request: #661
This commit is contained in:
committed by
Christoph Strobl
parent
70098ae426
commit
cd930ea0b7
@@ -20,7 +20,6 @@ import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bson.BsonJavaScript;
|
||||
@@ -49,6 +48,7 @@ import com.querydsl.mongodb.MongodbOps;
|
||||
* @author laimw
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
* @author Mikhail Kaduchka
|
||||
* @since 2.1
|
||||
*/
|
||||
abstract class MongodbDocumentSerializer implements Visitor<Object, Void> {
|
||||
@@ -184,8 +184,8 @@ abstract class MongodbDocumentSerializer implements Visitor<Object, Void> {
|
||||
Map<Object, Object> lhs = (Map<Object, Object>) handle(expr.getArg(0));
|
||||
Map<Object, Object> rhs = (Map<Object, Object>) handle(expr.getArg(1));
|
||||
|
||||
LinkedHashSet<Entry<Object, Object>> lhs2 = new LinkedHashSet<>(lhs.entrySet());
|
||||
lhs2.retainAll(rhs.entrySet());
|
||||
LinkedHashSet<Object> lhs2 = new LinkedHashSet<>(lhs.keySet());
|
||||
lhs2.retainAll(rhs.keySet());
|
||||
|
||||
if (lhs2.isEmpty()) {
|
||||
lhs.putAll(rhs);
|
||||
|
||||
@@ -15,17 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.repository.support;
|
||||
|
||||
import static com.querydsl.core.types.ExpressionUtils.path;
|
||||
import static com.querydsl.core.types.ExpressionUtils.predicate;
|
||||
import static com.querydsl.core.types.dsl.Expressions.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.mongodb.core.DocumentTestUtils.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.hamcrest.collection.IsIterableContainingInOrder;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -43,6 +42,8 @@ import org.springframework.data.mongodb.repository.Person.Sex;
|
||||
import org.springframework.data.mongodb.repository.QAddress;
|
||||
import org.springframework.data.mongodb.repository.QPerson;
|
||||
|
||||
import com.querydsl.core.types.Ops;
|
||||
import com.querydsl.core.types.PredicateOperation;
|
||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||
import com.querydsl.core.types.dsl.BooleanOperation;
|
||||
import com.querydsl.core.types.dsl.PathBuilder;
|
||||
@@ -55,6 +56,7 @@ import com.querydsl.core.types.dsl.StringPath;
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @author Mikhail Kaduchka
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SpringDataMongodbSerializerUnitTests {
|
||||
@@ -163,6 +165,21 @@ public class SpringDataMongodbSerializerUnitTests {
|
||||
+ "\"$ne\" : \"\"}}} , { \"firstname\" : { \"$not\" : { \"$regex\" : \".*\\\\Qfoo\\\\E.*\" , \"$options\" : \"i\"}}}]}"))));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2228
|
||||
public void retainsOpsInAndExpression() {
|
||||
|
||||
PredicateOperation testExpression = predicate(Ops.AND,
|
||||
predicate(Ops.OR, predicate(Ops.EQ, path(Object.class, "firstname"), constant("John")),
|
||||
predicate(Ops.EQ, path(Object.class, "firstname"), constant("Sarah"))),
|
||||
predicate(Ops.OR, predicate(Ops.EQ, path(Object.class, "lastname"), constant("Smith")),
|
||||
predicate(Ops.EQ, path(Object.class, "lastname"), constant("Connor"))));
|
||||
|
||||
Document result = (Document) serializer.visit(testExpression, null);
|
||||
|
||||
assertThat(result.toJson(), is(
|
||||
"{\"$and\": [{\"$or\": [{\"firstname\": \"John\"}, {\"firstname\": \"Sarah\"}]}, {\"$or\": [{\"lastname\": \"Smith\"}, {\"lastname\": \"Connor\"}]}]}"));
|
||||
}
|
||||
|
||||
class Address {
|
||||
String id;
|
||||
String street;
|
||||
|
||||
Reference in New Issue
Block a user