Skip to content

Commit d57fb3e

Browse files
committed
DATACMNS-1785 - Polishing.
Introduce constructors accepting non-null ConversionService, handle conversion service defaulting in configuration classes by using ObjectProvider. Extract common code to create a predicate in getPredicate(…) method. Add unit test. Convert spaces to tabs. Related ticket: #2200. Original pull request: #2274.
1 parent a839690 commit d57fb3e

6 files changed

+348
-148
lines changed

src/main/java/org/springframework/data/web/config/QuerydslWebConfiguration.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
package org.springframework.data.web.config;
1717

1818
import java.util.List;
19-
import java.util.Optional;
2019

2120
import org.springframework.beans.factory.BeanFactory;
22-
import org.springframework.beans.factory.ObjectFactory;
2321
import org.springframework.beans.factory.ObjectProvider;
2422
import org.springframework.beans.factory.annotation.Autowired;
2523
import org.springframework.beans.factory.annotation.Qualifier;
2624
import org.springframework.context.annotation.Bean;
2725
import org.springframework.context.annotation.Configuration;
2826
import org.springframework.context.annotation.Lazy;
2927
import org.springframework.core.convert.ConversionService;
28+
import org.springframework.core.convert.support.DefaultConversionService;
3029
import org.springframework.data.querydsl.EntityPathResolver;
3130
import org.springframework.data.querydsl.SimpleEntityPathResolver;
3231
import org.springframework.data.querydsl.binding.QuerydslBindingsFactory;
@@ -48,7 +47,7 @@
4847
@Configuration(proxyBeanMethods = false)
4948
public class QuerydslWebConfiguration implements WebMvcConfigurer {
5049

51-
@Autowired @Qualifier("mvcConversionService") ObjectFactory<ConversionService> conversionService;
50+
@Autowired @Qualifier("mvcConversionService") ObjectProvider<ConversionService> conversionService;
5251
@Autowired ObjectProvider<EntityPathResolver> resolver;
5352
@Autowired BeanFactory beanFactory;
5453

@@ -63,7 +62,7 @@ public class QuerydslWebConfiguration implements WebMvcConfigurer {
6362
public QuerydslPredicateArgumentResolver querydslPredicateArgumentResolver() {
6463
return new QuerydslPredicateArgumentResolver(
6564
beanFactory.getBean("querydslBindingsFactory", QuerydslBindingsFactory.class),
66-
Optional.of(conversionService.getObject()));
65+
conversionService.getIfUnique(DefaultConversionService::getSharedInstance));
6766
}
6867

6968
@Lazy

src/main/java/org/springframework/data/web/config/ReactiveQuerydslWebConfiguration.java

+15-13
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@
1515
*/
1616
package org.springframework.data.web.config;
1717

18-
import java.util.Optional;
19-
2018
import org.springframework.beans.factory.BeanFactory;
21-
import org.springframework.beans.factory.ObjectFactory;
2219
import org.springframework.beans.factory.ObjectProvider;
2320
import org.springframework.beans.factory.annotation.Autowired;
2421
import org.springframework.beans.factory.annotation.Qualifier;
2522
import org.springframework.context.annotation.Bean;
2623
import org.springframework.context.annotation.Configuration;
2724
import org.springframework.context.annotation.Lazy;
2825
import org.springframework.core.convert.ConversionService;
26+
import org.springframework.core.convert.support.DefaultConversionService;
2927
import org.springframework.data.querydsl.EntityPathResolver;
3028
import org.springframework.data.querydsl.SimpleEntityPathResolver;
3129
import org.springframework.data.querydsl.binding.QuerydslBindingsFactory;
@@ -34,24 +32,24 @@
3432
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
3533

3634
/**
37-
* Querydsl-specific web configuration for Spring Data. Registers a {@link HandlerMethodArgumentResolver} that builds up
38-
* {@link Predicate}s from web requests.
35+
* Querydsl-specific web configuration for Spring Data. Registers a
36+
* {@link org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver} that builds up
37+
* {@link com.querydsl.core.types.Predicate}s from web requests.
3938
*
4039
* @author Matías Hermosilla
40+
* @author Mark Paluch
4141
* @since 1.11
42-
* @soundtrack Anika Nilles - Alter Ego
4342
*/
4443
@Configuration(proxyBeanMethods = false)
4544
public class ReactiveQuerydslWebConfiguration implements WebFluxConfigurer {
4645

47-
@Autowired
48-
@Qualifier("webFluxConversionService") ObjectFactory<ConversionService> conversionService;
46+
@Autowired @Qualifier("webFluxConversionService") ObjectProvider<ConversionService> conversionService;
4947
@Autowired ObjectProvider<EntityPathResolver> resolver;
5048
@Autowired BeanFactory beanFactory;
5149

5250
/**
53-
* Default {@link ReactiveQuerydslPredicateArgumentResolver} to create Querydsl {@link Predicate} instances for
54-
* Spring WebFlux controller methods.
51+
* Default {@link ReactiveQuerydslPredicateArgumentResolver} to create Querydsl {@link Predicate} instances for Spring
52+
* WebFlux controller methods.
5553
*
5654
* @return
5755
*/
@@ -60,7 +58,7 @@ public class ReactiveQuerydslWebConfiguration implements WebFluxConfigurer {
6058
public ReactiveQuerydslPredicateArgumentResolver querydslPredicateArgumentResolver() {
6159
return new ReactiveQuerydslPredicateArgumentResolver(
6260
beanFactory.getBean("querydslBindingsFactory", QuerydslBindingsFactory.class),
63-
Optional.of(conversionService.getObject()));
61+
conversionService.getIfUnique(DefaultConversionService::getSharedInstance));
6462
}
6563

6664
@Lazy
@@ -69,10 +67,14 @@ public QuerydslBindingsFactory querydslBindingsFactory() {
6967
return new QuerydslBindingsFactory(resolver.getIfUnique(() -> SimpleEntityPathResolver.INSTANCE));
7068
}
7169

70+
/*
71+
* (non-Javadoc)
72+
* @see org.springframework.web.reactive.config.WebFluxConfigurer#configureArgumentResolvers(org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer)
73+
*/
7274
@Override
7375
public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
74-
configurer.addCustomResolver(beanFactory.getBean("querydslPredicateArgumentResolver",
75-
ReactiveQuerydslPredicateArgumentResolver.class));
76+
configurer.addCustomResolver(
77+
beanFactory.getBean("querydslPredicateArgumentResolver", ReactiveQuerydslPredicateArgumentResolver.class));
7678
}
7779

7880
}

src/main/java/org/springframework/data/web/querydsl/QuerydslPredicateArgumentResolver.java

+35-24
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@
1616
package org.springframework.data.web.querydsl;
1717

1818
import java.util.Arrays;
19+
import java.util.Map;
1920
import java.util.Map.Entry;
2021
import java.util.Optional;
2122

2223
import org.springframework.core.MethodParameter;
2324
import org.springframework.core.ResolvableType;
2425
import org.springframework.core.convert.ConversionService;
25-
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
26-
import org.springframework.data.querydsl.binding.QuerydslBindings;
26+
import org.springframework.core.convert.support.DefaultConversionService;
2727
import org.springframework.data.querydsl.binding.QuerydslBindingsFactory;
28-
import org.springframework.data.querydsl.binding.QuerydslPredicate;
29-
import org.springframework.data.util.CastUtils;
30-
import org.springframework.data.util.TypeInformation;
3128
import org.springframework.lang.Nullable;
3229
import org.springframework.util.LinkedMultiValueMap;
3330
import org.springframework.util.MultiValueMap;
@@ -46,13 +43,32 @@
4643
* @author Christoph Strobl
4744
* @author Oliver Gierke
4845
* @author Matías Hermosilla
46+
* @author Mark Paluch
4947
* @since 1.11
5048
*/
5149
public class QuerydslPredicateArgumentResolver extends QuerydslPredicateArgumentResolverSupport
5250
implements HandlerMethodArgumentResolver {
5351

52+
/**
53+
* Create a new {@link QuerydslPredicateArgumentResolver}.
54+
*
55+
* @param factory the {@link QuerydslBindingsFactory} to use, must not be {@literal null}.
56+
* @param conversionService the optional {@link ConversionService} to use, must not be {@literal null}. Defaults to
57+
* {@link DefaultConversionService} if {@link Optional#empty() empty}.
58+
*/
5459
public QuerydslPredicateArgumentResolver(QuerydslBindingsFactory factory,
5560
Optional<ConversionService> conversionService) {
61+
super(factory, conversionService.orElseGet(DefaultConversionService::getSharedInstance));
62+
}
63+
64+
/**
65+
* Create a new {@link QuerydslPredicateArgumentResolver}.
66+
*
67+
* @param factory the {@link QuerydslBindingsFactory} to use, must not be {@literal null}.
68+
* @param conversionService the {@link ConversionService} to use, must not be {@literal null}.
69+
* @since 2.5
70+
*/
71+
public QuerydslPredicateArgumentResolver(QuerydslBindingsFactory factory, ConversionService conversionService) {
5672
super(factory, conversionService);
5773
}
5874

@@ -65,25 +81,8 @@ public QuerydslPredicateArgumentResolver(QuerydslBindingsFactory factory,
6581
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
6682
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
6783

68-
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
69-
70-
for (Entry<String, String[]> entry : webRequest.getParameterMap().entrySet()) {
71-
parameters.put(entry.getKey(), Arrays.asList(entry.getValue()));
72-
}
73-
74-
Optional<QuerydslPredicate> annotation = Optional
75-
.ofNullable(parameter.getParameterAnnotation(QuerydslPredicate.class));
76-
TypeInformation<?> domainType = extractTypeInfo(parameter).getRequiredActualType();
77-
78-
Optional<Class<? extends QuerydslBinderCustomizer<?>>> bindingsAnnotation = annotation //
79-
.map(QuerydslPredicate::bindings) //
80-
.map(CastUtils::cast);
81-
82-
QuerydslBindings bindings = bindingsAnnotation //
83-
.map(it -> bindingsFactory.createBindingsFor(domainType, it)) //
84-
.orElseGet(() -> bindingsFactory.createBindingsFor(domainType));
85-
86-
Predicate result = predicateBuilder.getPredicate(domainType, parameters, bindings);
84+
MultiValueMap<String, String> queryParameters = getQueryParameters(webRequest);
85+
Predicate result = getPredicate(parameter, queryParameters);
8786

8887
if (!parameter.isOptional() && result == null) {
8988
return new BooleanBuilder();
@@ -94,4 +93,16 @@ public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewC
9493
: result;
9594
}
9695

96+
private static MultiValueMap<String, String> getQueryParameters(NativeWebRequest webRequest) {
97+
98+
Map<String, String[]> parameterMap = webRequest.getParameterMap();
99+
MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<>(parameterMap.size());
100+
101+
for (Entry<String, String[]> entry : parameterMap.entrySet()) {
102+
queryParameters.put(entry.getKey(), Arrays.asList(entry.getValue()));
103+
}
104+
105+
return queryParameters;
106+
}
107+
97108
}

0 commit comments

Comments
 (0)