Search in sources :

Example 1 with QuerydslBindings

use of org.springframework.data.querydsl.binding.QuerydslBindings in project spring-data-commons by spring-projects.

the class QuerydslPredicateArgumentResolverSupport method getPredicate.

Predicate getPredicate(MethodParameter parameter, MultiValueMap<String, String> queryParameters) {
    MergedAnnotations annotations = MergedAnnotations.from(parameter.getParameter());
    MergedAnnotation<QuerydslPredicate> predicateAnnotation = annotations.get(QuerydslPredicate.class);
    TypeInformation<?> domainType = extractTypeInfo(parameter, predicateAnnotation).getRequiredActualType();
    Optional<Class<? extends QuerydslBinderCustomizer<?>>> bindingsAnnotation = // 
    predicateAnnotation.getValue("bindings").map(CastUtils::cast);
    QuerydslBindings bindings = // 
    bindingsAnnotation.map(// 
    it -> bindingsFactory.createBindingsFor(domainType, it)).orElseGet(() -> bindingsFactory.createBindingsFor(domainType));
    return predicateBuilder.getPredicate(domainType, queryParameters, bindings);
}
Also used : HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) MultiValueMap(org.springframework.util.MultiValueMap) TypeInformation(org.springframework.data.util.TypeInformation) MergedAnnotation(org.springframework.core.annotation.MergedAnnotation) QuerydslPredicateBuilder(org.springframework.data.querydsl.binding.QuerydslPredicateBuilder) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) MergedAnnotations(org.springframework.core.annotation.MergedAnnotations) MethodParameter(org.springframework.core.MethodParameter) Optional(java.util.Optional) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) QuerydslBindingsFactory(org.springframework.data.querydsl.binding.QuerydslBindingsFactory) Predicate(com.querydsl.core.types.Predicate) Method(java.lang.reflect.Method) ConversionService(org.springframework.core.convert.ConversionService) QuerydslPredicate(org.springframework.data.querydsl.binding.QuerydslPredicate) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) CastUtils(org.springframework.data.util.CastUtils) Assert(org.springframework.util.Assert) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) QuerydslPredicate(org.springframework.data.querydsl.binding.QuerydslPredicate) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) MergedAnnotations(org.springframework.core.annotation.MergedAnnotations) CastUtils(org.springframework.data.util.CastUtils)

Example 2 with QuerydslBindings

use of org.springframework.data.querydsl.binding.QuerydslBindings in project spring-graphql by spring-projects.

the class QuerydslDataFetcher method buildPredicate.

/**
 * Prepare a {@link Predicate} from GraphQL request arguments, also applying
 * any {@link QuerydslBinderCustomizer} that may have been configured.
 * @param environment contextual info for the GraphQL request
 * @return the resulting predicate
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Predicate buildPredicate(DataFetchingEnvironment environment) {
    MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
    QuerydslBindings bindings = new QuerydslBindings();
    EntityPath<?> path = SimpleEntityPathResolver.INSTANCE.createPath(this.domainType.getType());
    this.customizer.customize(bindings, path);
    for (Map.Entry<String, Object> entry : environment.getArguments().entrySet()) {
        Object value = entry.getValue();
        List<Object> values = (value instanceof List ? (List<Object>) value : Collections.singletonList(value));
        parameters.put(entry.getKey(), values);
    }
    return BUILDER.getPredicate(this.domainType, (MultiValueMap) parameters, bindings);
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 3 with QuerydslBindings

use of org.springframework.data.querydsl.binding.QuerydslBindings in project springdoc-openapi by springdoc.

the class QuerydslPredicateOperationCustomizer method customize.

@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
    if (operation.getParameters() == null) {
        return operation;
    }
    MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
    int parametersLength = methodParameters.length;
    List<Parameter> parametersToAddToOperation = new ArrayList<>();
    for (int i = 0; i < parametersLength; i++) {
        MethodParameter parameter = methodParameters[i];
        QuerydslPredicate predicate = parameter.getParameterAnnotation(QuerydslPredicate.class);
        if (predicate == null)
            continue;
        QuerydslBindings bindings = extractQdslBindings(predicate);
        Set<String> fieldsToAdd = Arrays.stream(predicate.root().getDeclaredFields()).filter(field -> !Modifier.isStatic(field.getModifiers())).map(Field::getName).collect(Collectors.toSet());
        Map<String, Object> pathSpecMap = getPathSpec(bindings, "pathSpecs");
        // remove blacklisted fields
        Set<String> blacklist = getFieldValues(bindings, "denyList", "blackList");
        fieldsToAdd.removeIf(blacklist::contains);
        Set<String> whiteList = getFieldValues(bindings, "allowList", "whiteList");
        Set<String> aliases = getFieldValues(bindings, "aliases", null);
        fieldsToAdd.addAll(aliases);
        fieldsToAdd.addAll(whiteList);
        // if only listed properties should be included, remove all other fields from fieldsToAdd
        if (getFieldValueOfBoolean(bindings, "excludeUnlistedProperties")) {
            fieldsToAdd.removeIf(s -> !whiteList.contains(s) && !aliases.contains(s));
        }
        for (String fieldName : fieldsToAdd) {
            Type type = getFieldType(fieldName, pathSpecMap, predicate.root());
            if (type != null) {
                Parameter newParameter = buildParam(type, fieldName);
                parametersToAddToOperation.add(newParameter);
            }
        }
    }
    operation.getParameters().addAll(parametersToAddToOperation);
    return operation;
}
Also used : QuerydslPredicate(org.springframework.data.querydsl.binding.QuerydslPredicate) ArrayList(java.util.ArrayList) PrimitiveType(io.swagger.v3.core.util.PrimitiveType) Type(java.lang.reflect.Type) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) Parameter(io.swagger.v3.oas.models.parameters.Parameter) MethodParameter(org.springframework.core.MethodParameter) MethodParameter(org.springframework.core.MethodParameter)

Aggregations

QuerydslBindings (org.springframework.data.querydsl.binding.QuerydslBindings)3 MethodParameter (org.springframework.core.MethodParameter)2 QuerydslPredicate (org.springframework.data.querydsl.binding.QuerydslPredicate)2 MultiValueMap (org.springframework.util.MultiValueMap)2 Predicate (com.querydsl.core.types.Predicate)1 PrimitiveType (io.swagger.v3.core.util.PrimitiveType)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ResolvableType (org.springframework.core.ResolvableType)1 MergedAnnotation (org.springframework.core.annotation.MergedAnnotation)1 MergedAnnotations (org.springframework.core.annotation.MergedAnnotations)1 ConversionService (org.springframework.core.convert.ConversionService)1 QuerydslBinderCustomizer (org.springframework.data.querydsl.binding.QuerydslBinderCustomizer)1 QuerydslBindingsFactory (org.springframework.data.querydsl.binding.QuerydslBindingsFactory)1