DATACMNS-669 - Ignore non-reachable paths for Querydsl parameter binding.

We now ignore invalid PropertyPaths on property binding to Querydsl Predicates. Those may occur since the request also holds non-property-related arguments, such as pagination parameters.

Fixed two potential NullPointerExceptions along the way since we no longer depend on the presence of @QuerydslPredicate.

Original pull request: #132.
This commit is contained in:
Christoph Strobl
2015-07-15 09:51:55 +02:00
committed by Oliver Gierke
parent 5db3def79b
commit dc49101850
4 changed files with 69 additions and 12 deletions

View File

@@ -43,6 +43,8 @@ import com.mysema.query.types.Predicate;
*/
public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentResolver, ApplicationContextAware {
private static final QuerydslBindings DEFAULT_BINDINGS = new QuerydslBindings();
private final QuerydslPredicateBuilder predicateBuilder;
private AutowireCapableBeanFactory beanFactory;
@@ -99,18 +101,25 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
private TypeInformation<?> extractTypeInfo(MethodParameter parameter) {
Class<?> type = parameter.getParameterAnnotation(QuerydslPredicate.class).root();
QuerydslPredicate annotation = parameter.getParameterAnnotation(QuerydslPredicate.class);
return type == Object.class ? ClassTypeInformation.fromReturnTypeOf(parameter.getMethod())
: ClassTypeInformation.from(type);
if (annotation == null || Object.class.equals(annotation.root())) {
return ClassTypeInformation.fromReturnTypeOf(parameter.getMethod());
}
return ClassTypeInformation.from(annotation.root());
}
private QuerydslBindings createBindings(MethodParameter parameter)
throws InstantiationException, IllegalAccessException {
Class<? extends QuerydslBindings> bindingsType = parameter.getParameterAnnotation(QuerydslPredicate.class)
.bindings();
QuerydslPredicate annotation = parameter.getParameterAnnotation(QuerydslPredicate.class);
return beanFactory != null ? beanFactory.createBean(bindingsType) : BeanUtils.instantiateClass(bindingsType);
if (annotation == null) {
return DEFAULT_BINDINGS;
}
Class<? extends QuerydslBindings> type = annotation.bindings();
return beanFactory != null ? beanFactory.createBean(type) : BeanUtils.instantiateClass(type);
}
}

View File

@@ -26,6 +26,7 @@ import org.springframework.beans.PropertyValue;
import org.springframework.beans.PropertyValues;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.mapping.PropertyReferenceException;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
@@ -78,12 +79,17 @@ class QuerydslPredicateBuilder {
for (PropertyValue propertyValue : values.getPropertyValues()) {
PropertyPath propertyPath = PropertyPath.from(propertyValue.getName(), type);
try {
if (bindings.isPathVisible(propertyPath)) {
PropertyPath propertyPath = PropertyPath.from(propertyValue.getName(), type);
Collection<Object> value = convertToPropertyPathSpecificType(propertyValue.getValue(), propertyPath);
builder.and(invokeBinding(propertyPath, bindings, value));
if (bindings.isPathVisible(propertyPath)) {
Collection<Object> value = convertToPropertyPathSpecificType(propertyValue.getValue(), propertyPath);
builder.and(invokeBinding(propertyPath, bindings, value));
}
} catch (PropertyReferenceException o_O) {
// not a property of the domain object, continue
}
}