DATACMNS-883 - Improved collection path binding for Querydsl-backed web requests.
Improved the dot-path translation of existing Querydsl Path instances by not relying on the toString() representation of the metadata but manually traversing the elements up to the root. Extracted the translation method into QueryDslUtils. Furthermore, we now automatically insert an CollectionPathBase.any() step for Path instances of that type when reifying the paths from request attributes.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.querydsl;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link QueryDslUtils}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class QueryDslUtilsUnitTests {
|
||||
|
||||
/**
|
||||
* @see DATACMNS-883
|
||||
*/
|
||||
@Test
|
||||
public void rendersDotPathForPathTraversalContainingAnyExpression() {
|
||||
assertThat(QueryDslUtils.toDotPath(QUser.user.addresses.any().street), is("addresses.street"));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2015 the original author or authors.
|
||||
* Copyright 2011-2016 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.
|
||||
@@ -34,6 +34,7 @@ public class User {
|
||||
public String firstname, lastname;
|
||||
public @DateTimeFormat(iso = ISO.DATE) Date dateOfBirth;
|
||||
public Address address;
|
||||
public List<Address> addresses;
|
||||
public List<String> nickNames;
|
||||
public Long inceptionYear;
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ public class QuerydslBindingsUnitTests {
|
||||
|
||||
bindings.bind(String.class).first(CONTAINS_BINDING);
|
||||
|
||||
assertThat(bindings.isPathVisible("inceptionYear", User.class), is(true));
|
||||
assertThat(bindings.isPathAvailable("inceptionYear", User.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,7 +140,7 @@ public class QuerydslBindingsUnitTests {
|
||||
|
||||
bindings.including(QUser.user.inceptionYear);
|
||||
|
||||
assertThat(bindings.isPathVisible("inceptionYear", User.class), is(true));
|
||||
assertThat(bindings.isPathAvailable("inceptionYear", User.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,7 +151,7 @@ public class QuerydslBindingsUnitTests {
|
||||
|
||||
bindings.including(QUser.user.inceptionYear);
|
||||
|
||||
assertThat(bindings.isPathVisible("firstname", User.class), is(false));
|
||||
assertThat(bindings.isPathAvailable("firstname", User.class), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,7 +162,7 @@ public class QuerydslBindingsUnitTests {
|
||||
|
||||
bindings.excluding(QUser.user.inceptionYear);
|
||||
|
||||
assertThat(bindings.isPathVisible("inceptionYear", User.class), is(false));
|
||||
assertThat(bindings.isPathAvailable("inceptionYear", User.class), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -173,7 +173,7 @@ public class QuerydslBindingsUnitTests {
|
||||
|
||||
bindings.excluding(QUser.user.inceptionYear);
|
||||
|
||||
assertThat(bindings.isPathVisible("firstname", User.class), is(true));
|
||||
assertThat(bindings.isPathAvailable("firstname", User.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,7 +185,7 @@ public class QuerydslBindingsUnitTests {
|
||||
bindings.excluding(QUser.user.firstname);
|
||||
bindings.including(QUser.user.firstname);
|
||||
|
||||
assertThat(bindings.isPathVisible("firstname", User.class), is(true));
|
||||
assertThat(bindings.isPathAvailable("firstname", User.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -196,7 +196,7 @@ public class QuerydslBindingsUnitTests {
|
||||
|
||||
bindings.excluding(QUser.user.address);
|
||||
|
||||
assertThat(bindings.isPathVisible("address.city", User.class), is(false));
|
||||
assertThat(bindings.isPathAvailable("address.city", User.class), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -208,7 +208,7 @@ public class QuerydslBindingsUnitTests {
|
||||
bindings.excluding(QUser.user.address);
|
||||
bindings.including(QUser.user.address.city);
|
||||
|
||||
assertThat(bindings.isPathVisible("address.city", User.class), is(true));
|
||||
assertThat(bindings.isPathAvailable("address.city", User.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -220,7 +220,7 @@ public class QuerydslBindingsUnitTests {
|
||||
bindings.excluding(QUser.user.address);
|
||||
bindings.including(QUser.user.address.city);
|
||||
|
||||
assertThat(bindings.isPathVisible("address.street", User.class), is(false));
|
||||
assertThat(bindings.isPathAvailable("address.street", User.class), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -231,10 +231,10 @@ public class QuerydslBindingsUnitTests {
|
||||
|
||||
bindings.including(QUser.user.firstname, QUser.user.address.street);
|
||||
|
||||
assertThat(bindings.isPathVisible("firstname", User.class), is(true));
|
||||
assertThat(bindings.isPathVisible("address.street", User.class), is(true));
|
||||
assertThat(bindings.isPathVisible("lastname", User.class), is(false));
|
||||
assertThat(bindings.isPathVisible("address.city", User.class), is(false));
|
||||
assertThat(bindings.isPathAvailable("firstname", User.class), is(true));
|
||||
assertThat(bindings.isPathAvailable("address.street", User.class), is(true));
|
||||
assertThat(bindings.isPathAvailable("lastname", User.class), is(false));
|
||||
assertThat(bindings.isPathAvailable("address.city", User.class), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -264,10 +264,10 @@ public class QuerydslBindingsUnitTests {
|
||||
PropertyPath path = bindings.getPropertyPath("city", ClassTypeInformation.from(User.class));
|
||||
|
||||
assertThat(path, is(notNullValue()));
|
||||
assertThat(bindings.isPathVisible("city", User.class), is(true));
|
||||
assertThat(bindings.isPathAvailable("city", User.class), is(true));
|
||||
|
||||
// Aliasing implicitly blacklists original path
|
||||
assertThat(bindings.isPathVisible("address.city", User.class), is(false));
|
||||
assertThat(bindings.isPathAvailable("address.city", User.class), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,9 +282,9 @@ public class QuerydslBindingsUnitTests {
|
||||
PropertyPath path = bindings.getPropertyPath("city", ClassTypeInformation.from(User.class));
|
||||
|
||||
assertThat(path, is(notNullValue()));
|
||||
assertThat(bindings.isPathVisible("city", User.class), is(true));
|
||||
assertThat(bindings.isPathAvailable("city", User.class), is(true));
|
||||
|
||||
assertThat(bindings.isPathVisible("address.city", User.class), is(true));
|
||||
assertThat(bindings.isPathAvailable("address.city", User.class), is(true));
|
||||
|
||||
PropertyPath propertyPath = bindings.getPropertyPath("address.city", ClassTypeInformation.from(User.class));
|
||||
assertThat(propertyPath, is(notNullValue()));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
* Copyright 2015-2016 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.
|
||||
@@ -206,4 +206,17 @@ public class QuerydslPredicateBuilderUnitTests {
|
||||
|
||||
assertThat(predicate, is((Predicate) QUser.user.dateOfBirth.eq(format.parseDateTime(date).toDate())));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-883
|
||||
*/
|
||||
@Test
|
||||
public void automaticallyInsertsAnyStepInCollectionReference() {
|
||||
|
||||
values.add("addresses.street", "VALUE");
|
||||
|
||||
Predicate predicate = builder.getPredicate(USER_TYPE, values, DEFAULT_BINDINGS);
|
||||
|
||||
assertThat(predicate, is((Predicate) QUser.user.addresses.any().street.eq("VALUE")));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user