|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright 2019-2020 the original author or authors. |
| 4 | + * * |
| 5 | + * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * * you may not use this file except in compliance with the License. |
| 7 | + * * You may obtain a copy of the License at |
| 8 | + * * |
| 9 | + * * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * * |
| 11 | + * * Unless required by applicable law or agreed to in writing, software |
| 12 | + * * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * * See the License for the specific language governing permissions and |
| 15 | + * * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.springdoc.core.converters; |
| 20 | + |
| 21 | +import java.lang.reflect.Field; |
| 22 | +import java.lang.reflect.Modifier; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Iterator; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +import com.fasterxml.jackson.databind.JavaType; |
| 31 | +import com.querydsl.core.types.Predicate; |
| 32 | +import io.swagger.v3.core.converter.AnnotatedType; |
| 33 | +import io.swagger.v3.core.converter.ModelConverter; |
| 34 | +import io.swagger.v3.core.converter.ModelConverterContext; |
| 35 | +import io.swagger.v3.core.util.Json; |
| 36 | +import io.swagger.v3.oas.models.media.Schema; |
| 37 | +import javassist.CannotCompileException; |
| 38 | +import javassist.ClassPool; |
| 39 | +import javassist.CtClass; |
| 40 | +import javassist.CtField; |
| 41 | +import org.slf4j.Logger; |
| 42 | +import org.slf4j.LoggerFactory; |
| 43 | + |
| 44 | +import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer; |
| 45 | +import org.springframework.data.querydsl.binding.QuerydslBindings; |
| 46 | +import org.springframework.data.querydsl.binding.QuerydslBindingsFactory; |
| 47 | +import org.springframework.data.querydsl.binding.QuerydslPredicate; |
| 48 | +import org.springframework.data.util.CastUtils; |
| 49 | +import org.springframework.data.util.ClassTypeInformation; |
| 50 | +import org.springframework.data.util.TypeInformation; |
| 51 | + |
| 52 | + |
| 53 | +public class QueryDslPredicateConverter implements ModelConverter { |
| 54 | + |
| 55 | + private static final Logger LOGGER = LoggerFactory.getLogger(QueryDslPredicateConverter.class); |
| 56 | + |
| 57 | + private QuerydslBindingsFactory querydslBindingsFactory; |
| 58 | + |
| 59 | + public QueryDslPredicateConverter(QuerydslBindingsFactory querydslBindingsFactory) { |
| 60 | + this.querydslBindingsFactory = querydslBindingsFactory; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Schema resolve(AnnotatedType annotatedType, ModelConverterContext modelConverterContext, Iterator<ModelConverter> iterator) { |
| 65 | + JavaType javaType = Json.mapper().constructType(annotatedType.getType()); |
| 66 | + if (javaType != null) { |
| 67 | + Class<?> cls = javaType.getRawClass(); |
| 68 | + if (Predicate.class.isAssignableFrom(cls)) { |
| 69 | + Optional<QuerydslPredicate> predicate = Arrays.stream(annotatedType.getCtxAnnotations()).filter(QuerydslPredicate.class::isInstance).map(QuerydslPredicate.class::cast).findAny(); |
| 70 | + if (predicate.isPresent()) { |
| 71 | + try { |
| 72 | + Class<?> tClass = getClassFromPredicate(predicate.get()); |
| 73 | + return this.resolve(new AnnotatedType(tClass).jsonViewAnnotation(annotatedType.getJsonViewAnnotation()).resolveAsRef(true), modelConverterContext, iterator); |
| 74 | + } |
| 75 | + catch (CannotCompileException e) { |
| 76 | + LOGGER.warn("Can not compile : {}", e.getMessage()); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return iterator.hasNext() ? iterator.next().resolve(annotatedType, modelConverterContext, iterator) : null; |
| 82 | + } |
| 83 | + |
| 84 | + private Class<?> getClassFromPredicate(QuerydslPredicate predicate) throws CannotCompileException { |
| 85 | + ClassTypeInformation<?> classTypeInformation = ClassTypeInformation.from(predicate.root()); |
| 86 | + TypeInformation<?> domainType = classTypeInformation.getRequiredActualType(); |
| 87 | + Optional<Class<? extends QuerydslBinderCustomizer<?>>> bindingsAnnotation = Optional.of(predicate) |
| 88 | + .map(QuerydslPredicate::bindings) |
| 89 | + .map(CastUtils::cast); |
| 90 | + QuerydslBindings bindings = bindingsAnnotation |
| 91 | + .map(it -> querydslBindingsFactory.createBindingsFor(domainType, it)) |
| 92 | + .orElseGet(() -> querydslBindingsFactory.createBindingsFor(domainType)); |
| 93 | + String generatedClassName = "com.springdoc.core." + predicate.bindings().getSimpleName() + "G"; |
| 94 | + ClassPool classPool = ClassPool.getDefault(); |
| 95 | + CtClass classPoolOrNull = classPool.getOrNull(generatedClassName); |
| 96 | + if (classPoolOrNull == null) { |
| 97 | + classPoolOrNull = classPool.makeClass(generatedClassName); |
| 98 | + Set<String> fieldsToAdd = Arrays.stream(predicate.root().getDeclaredFields()).map(Field::getName).collect(Collectors.toSet()); |
| 99 | + //remove blacklisted fields |
| 100 | + Set<String> blacklist = getBindingFieldValues(bindings, "blackList"); |
| 101 | + fieldsToAdd.removeIf(blacklist::contains); |
| 102 | + Set<String> whiteList = getBindingFieldValues(bindings, "whiteList"); |
| 103 | + Set<String> aliases = getBindingFieldValues(bindings, "aliases"); |
| 104 | + fieldsToAdd.addAll(aliases); |
| 105 | + fieldsToAdd.addAll(whiteList); |
| 106 | + for (String fieldName : fieldsToAdd) { |
| 107 | + CtField f = new CtField(CtClass.charType, fieldName, classPoolOrNull); |
| 108 | + f.setModifiers(Modifier.PUBLIC); |
| 109 | + classPoolOrNull.addField(f); |
| 110 | + } |
| 111 | + } |
| 112 | + return classPoolOrNull.toClass(this.getClass().getClassLoader(), this.getClass().getProtectionDomain()); |
| 113 | + } |
| 114 | + |
| 115 | + private Set<String> getBindingFieldValues(QuerydslBindings instance, String fieldName) { |
| 116 | + try { |
| 117 | + Field field = instance.getClass().getDeclaredField(fieldName); |
| 118 | + if (Modifier.isPrivate(field.getModifiers())) { |
| 119 | + field.setAccessible(true); |
| 120 | + } |
| 121 | + return (Set<String>) field.get(instance); |
| 122 | + } |
| 123 | + catch (NoSuchFieldException | IllegalAccessException e) { |
| 124 | + LOGGER.warn("NoSuchFieldException or IllegalAccessException occured : {}", e.getMessage()); |
| 125 | + } |
| 126 | + return Collections.emptySet(); |
| 127 | + } |
| 128 | +} |
0 commit comments