Skip to content

Commit d71a1e9

Browse files
committed
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.
1 parent c8877de commit d71a1e9

File tree

7 files changed

+159
-45
lines changed

7 files changed

+159
-45
lines changed

src/main/java/org/springframework/data/querydsl/QueryDslUtils.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 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.
@@ -15,17 +15,61 @@
1515
*/
1616
package org.springframework.data.querydsl;
1717

18+
import lombok.experimental.UtilityClass;
19+
20+
import org.springframework.util.StringUtils;
21+
22+
import com.querydsl.core.types.Path;
23+
import com.querydsl.core.types.PathMetadata;
24+
1825
/**
1926
* Utility class for Querydsl.
2027
*
2128
* @author Oliver Gierke
2229
*/
23-
public abstract class QueryDslUtils {
30+
@UtilityClass
31+
public class QueryDslUtils {
2432

2533
public static final boolean QUERY_DSL_PRESENT = org.springframework.util.ClassUtils
2634
.isPresent("com.querydsl.core.types.Predicate", QueryDslUtils.class.getClassLoader());
2735

28-
private QueryDslUtils() {
36+
/**
37+
* Returns the property path for the given {@link Path}.
38+
*
39+
* @param path can be {@literal null}.
40+
* @return
41+
*/
42+
public static String toDotPath(Path<?> path) {
43+
return toDotPath(path, "");
44+
}
45+
46+
/**
47+
* Recursively builds up the dot path for the given {@link Path} instance by walking up the individual segments until
48+
* the root.
49+
*
50+
* @param path can be {@literal null}.
51+
* @param tail must not be {@literal null}.
52+
* @return
53+
*/
54+
private static String toDotPath(Path<?> path, String tail) {
55+
56+
if (path == null) {
57+
return tail;
58+
}
59+
60+
PathMetadata metadata = path.getMetadata();
61+
Path<?> parent = metadata.getParent();
62+
63+
if (parent == null) {
64+
return tail;
65+
}
66+
67+
Object element = metadata.getElement();
68+
69+
if (element == null || !StringUtils.hasText(element.toString())) {
70+
return toDotPath(parent, tail);
71+
}
2972

73+
return toDotPath(parent, StringUtils.hasText(tail) ? String.format("%s.%s", element, tail) : element.toString());
3074
}
3175
}

src/main/java/org/springframework/data/querydsl/binding/QuerydslBindings.java

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 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.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.querydsl.binding;
1717

18+
import static org.springframework.data.querydsl.QueryDslUtils.*;
19+
1820
import java.util.Arrays;
1921
import java.util.Collection;
2022
import java.util.HashSet;
@@ -32,7 +34,6 @@
3234
import org.springframework.util.StringUtils;
3335

3436
import com.querydsl.core.types.Path;
35-
import com.querydsl.core.types.PathMetadata;
3637
import com.querydsl.core.types.Predicate;
3738

3839
/**
@@ -158,8 +159,34 @@ public final QuerydslBindings excludeUnlistedProperties(boolean excludeUnlistedP
158159
return this;
159160
}
160161

161-
public boolean isPathVisible(String path, Class<?> type) {
162-
return getPropertyPath(path, ClassTypeInformation.from(type)) != null;
162+
/**
163+
* Returns whether the given path is available on the given type.
164+
*
165+
* @param path must not be {@literal null}.
166+
* @param type must not be {@literal null}.
167+
* @return
168+
*/
169+
boolean isPathAvailable(String path, Class<?> type) {
170+
171+
Assert.notNull(path, "Path must not be null!");
172+
Assert.notNull(type, "Type must not be null!");
173+
174+
return isPathAvailable(path, ClassTypeInformation.from(type));
175+
}
176+
177+
/**
178+
* Returns whether the given path is available on the given type.
179+
*
180+
* @param path must not be {@literal null}.
181+
* @param type
182+
* @return
183+
*/
184+
boolean isPathAvailable(String path, TypeInformation<?> type) {
185+
186+
Assert.notNull(path, "Path must not be null!");
187+
Assert.notNull(type, "Type must not be null!");
188+
189+
return getPropertyPath(path, type) != null;
163190
}
164191

165192
/**
@@ -198,6 +225,8 @@ public <S extends Path<? extends T>, T> MultiValueBinding<S, T> getBindingForPat
198225
*/
199226
Path<?> getExistingPath(PropertyPath path) {
200227

228+
Assert.notNull(path, "PropertyPath must not be null!");
229+
201230
PathAndBinding<?, ?> pathAndBuilder = pathSpecs.get(path.toDotPath());
202231
return pathAndBuilder == null ? null : pathAndBuilder.getPath();
203232
}
@@ -209,6 +238,8 @@ Path<?> getExistingPath(PropertyPath path) {
209238
*/
210239
PropertyPath getPropertyPath(String path, TypeInformation<?> type) {
211240

241+
Assert.notNull(path, "Path must not be null!");
242+
212243
if (!isPathVisible(path)) {
213244
return null;
214245
}
@@ -272,23 +303,6 @@ private boolean isPathVisible(String path) {
272303
return whiteList.contains(path);
273304
}
274305

275-
/**
276-
* Returns the property path for the given {@link Path}.
277-
*
278-
* @param path can be {@literal null}.
279-
* @return
280-
*/
281-
private static String toDotPath(Path<?> path) {
282-
283-
if (path == null) {
284-
return "";
285-
}
286-
287-
PathMetadata metadata = path.getMetadata();
288-
289-
return path.toString().substring(metadata.getRootPath().getMetadata().getName().length() + 1);
290-
}
291-
292306
/**
293307
* A binder for {@link Path}s.
294308
*

src/main/java/org/springframework/data/querydsl/binding/QuerydslPredicateBuilder.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 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.
@@ -41,6 +41,7 @@
4141
import com.querydsl.core.BooleanBuilder;
4242
import com.querydsl.core.types.Path;
4343
import com.querydsl.core.types.Predicate;
44+
import com.querydsl.core.types.dsl.CollectionPathBase;
4445

4546
/**
4647
* Builder assembling {@link Predicate} out of {@link PropertyValues}.
@@ -101,7 +102,7 @@ public Predicate getPredicate(TypeInformation<?> type, MultiValueMap<String, Str
101102

102103
String path = entry.getKey();
103104

104-
if (!bindings.isPathVisible(path, type.getType())) {
105+
if (!bindings.isPathAvailable(path, type)) {
105106
continue;
106107
}
107108

@@ -179,6 +180,10 @@ private Path<?> getPath(PropertyPath path, QuerydslBindings bindings) {
179180
*/
180181
private Path<?> reifyPath(PropertyPath path, Path<?> base) {
181182

183+
if (base instanceof CollectionPathBase) {
184+
return reifyPath(path, (Path<?>) ((CollectionPathBase<?, ?, ?>) base).any());
185+
}
186+
182187
Path<?> entityPath = base != null ? base : resolver.createPath(path.getOwningType().getType());
183188

184189
Field field = ReflectionUtils.findField(entityPath.getClass(), path.getSegment());
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.querydsl;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
23+
/**
24+
* Unit tests for {@link QueryDslUtils}.
25+
*
26+
* @author Oliver Gierke
27+
*/
28+
public class QueryDslUtilsUnitTests {
29+
30+
/**
31+
* @see DATACMNS-883
32+
*/
33+
@Test
34+
public void rendersDotPathForPathTraversalContainingAnyExpression() {
35+
assertThat(QueryDslUtils.toDotPath(QUser.user.addresses.any().street), is("addresses.street"));
36+
}
37+
}

src/test/java/org/springframework/data/querydsl/User.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 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.
@@ -34,6 +34,7 @@ public class User {
3434
public String firstname, lastname;
3535
public @DateTimeFormat(iso = ISO.DATE) Date dateOfBirth;
3636
public Address address;
37+
public List<Address> addresses;
3738
public List<String> nickNames;
3839
public Long inceptionYear;
3940

src/test/java/org/springframework/data/querydsl/binding/QuerydslBindingsUnitTests.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void pathIsVisibleIfTypeBasedBindingWasRegistered() {
129129

130130
bindings.bind(String.class).first(CONTAINS_BINDING);
131131

132-
assertThat(bindings.isPathVisible("inceptionYear", User.class), is(true));
132+
assertThat(bindings.isPathAvailable("inceptionYear", User.class), is(true));
133133
}
134134

135135
/**
@@ -140,7 +140,7 @@ public void explicitlyIncludedPathIsVisible() {
140140

141141
bindings.including(QUser.user.inceptionYear);
142142

143-
assertThat(bindings.isPathVisible("inceptionYear", User.class), is(true));
143+
assertThat(bindings.isPathAvailable("inceptionYear", User.class), is(true));
144144
}
145145

146146
/**
@@ -151,7 +151,7 @@ public void notExplicitlyIncludedPathIsInvisible() {
151151

152152
bindings.including(QUser.user.inceptionYear);
153153

154-
assertThat(bindings.isPathVisible("firstname", User.class), is(false));
154+
assertThat(bindings.isPathAvailable("firstname", User.class), is(false));
155155
}
156156

157157
/**
@@ -162,7 +162,7 @@ public void excludedPathIsInvisible() {
162162

163163
bindings.excluding(QUser.user.inceptionYear);
164164

165-
assertThat(bindings.isPathVisible("inceptionYear", User.class), is(false));
165+
assertThat(bindings.isPathAvailable("inceptionYear", User.class), is(false));
166166
}
167167

168168
/**
@@ -173,7 +173,7 @@ public void pathIsVisibleIfNotExplicitlyExcluded() {
173173

174174
bindings.excluding(QUser.user.inceptionYear);
175175

176-
assertThat(bindings.isPathVisible("firstname", User.class), is(true));
176+
assertThat(bindings.isPathAvailable("firstname", User.class), is(true));
177177
}
178178

179179
/**
@@ -185,7 +185,7 @@ public void pathIsVisibleIfItsBothBlackAndWhitelisted() {
185185
bindings.excluding(QUser.user.firstname);
186186
bindings.including(QUser.user.firstname);
187187

188-
assertThat(bindings.isPathVisible("firstname", User.class), is(true));
188+
assertThat(bindings.isPathAvailable("firstname", User.class), is(true));
189189
}
190190

191191
/**
@@ -196,7 +196,7 @@ public void nestedPathIsInvisibleIfAParanetPathWasExcluded() {
196196

197197
bindings.excluding(QUser.user.address);
198198

199-
assertThat(bindings.isPathVisible("address.city", User.class), is(false));
199+
assertThat(bindings.isPathAvailable("address.city", User.class), is(false));
200200
}
201201

202202
/**
@@ -208,7 +208,7 @@ public void pathIsVisibleIfConcretePathIsVisibleButParentExcluded() {
208208
bindings.excluding(QUser.user.address);
209209
bindings.including(QUser.user.address.city);
210210

211-
assertThat(bindings.isPathVisible("address.city", User.class), is(true));
211+
assertThat(bindings.isPathAvailable("address.city", User.class), is(true));
212212
}
213213

214214
/**
@@ -220,7 +220,7 @@ public void isPathVisibleShouldReturnFalseWhenPartialPathContainedInExcludingAnd
220220
bindings.excluding(QUser.user.address);
221221
bindings.including(QUser.user.address.city);
222222

223-
assertThat(bindings.isPathVisible("address.street", User.class), is(false));
223+
assertThat(bindings.isPathAvailable("address.street", User.class), is(false));
224224
}
225225

226226
/**
@@ -231,10 +231,10 @@ public void whitelistsPropertiesCorrectly() {
231231

232232
bindings.including(QUser.user.firstname, QUser.user.address.street);
233233

234-
assertThat(bindings.isPathVisible("firstname", User.class), is(true));
235-
assertThat(bindings.isPathVisible("address.street", User.class), is(true));
236-
assertThat(bindings.isPathVisible("lastname", User.class), is(false));
237-
assertThat(bindings.isPathVisible("address.city", User.class), is(false));
234+
assertThat(bindings.isPathAvailable("firstname", User.class), is(true));
235+
assertThat(bindings.isPathAvailable("address.street", User.class), is(true));
236+
assertThat(bindings.isPathAvailable("lastname", User.class), is(false));
237+
assertThat(bindings.isPathAvailable("address.city", User.class), is(false));
238238
}
239239

240240
/**
@@ -264,10 +264,10 @@ public void aliasesBinding() {
264264
PropertyPath path = bindings.getPropertyPath("city", ClassTypeInformation.from(User.class));
265265

266266
assertThat(path, is(notNullValue()));
267-
assertThat(bindings.isPathVisible("city", User.class), is(true));
267+
assertThat(bindings.isPathAvailable("city", User.class), is(true));
268268

269269
// Aliasing implicitly blacklists original path
270-
assertThat(bindings.isPathVisible("address.city", User.class), is(false));
270+
assertThat(bindings.isPathAvailable("address.city", User.class), is(false));
271271
}
272272

273273
/**
@@ -282,9 +282,9 @@ public void explicitlyIncludesOriginalBindingDespiteAlias() {
282282
PropertyPath path = bindings.getPropertyPath("city", ClassTypeInformation.from(User.class));
283283

284284
assertThat(path, is(notNullValue()));
285-
assertThat(bindings.isPathVisible("city", User.class), is(true));
285+
assertThat(bindings.isPathAvailable("city", User.class), is(true));
286286

287-
assertThat(bindings.isPathVisible("address.city", User.class), is(true));
287+
assertThat(bindings.isPathAvailable("address.city", User.class), is(true));
288288

289289
PropertyPath propertyPath = bindings.getPropertyPath("address.city", ClassTypeInformation.from(User.class));
290290
assertThat(propertyPath, is(notNullValue()));

0 commit comments

Comments
 (0)