This commit is contained in:
六如
2024-11-04 16:23:16 +08:00
parent 9b2dc49a03
commit f72fd07ea4
4 changed files with 12 additions and 40 deletions

View File

@@ -11,10 +11,8 @@ import io.swagger.annotations.ApiModelProperty;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import sun.reflect.generics.repository.ClassRepository;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
@@ -33,6 +31,7 @@ import java.util.concurrent.atomic.AtomicInteger;
/**
* 解析文档信息
*
* @author tanghc
*/
public class ApiDocBuilder {
@@ -106,6 +105,7 @@ public class ApiDocBuilder {
/**
* 生成文档信息
*
* @param paramClass 参数类型
* @return 返回文档内容
*/
@@ -120,7 +120,7 @@ public class ApiDocBuilder {
Class<?> targetClassRef = PluginUtil.isCollectionOrArray(clazz) ? getCollectionElementClass(clazz) : clazz;
// 查找泛型
if(targetClassRef.getName().equals("org.springframework.http.ResponseEntity")){
if (targetClassRef.getName().equals("org.springframework.http.ResponseEntity")) {
Field body = ReflectionUtils.findField(targetClassRef, "body");
String typeName = ((TypeVariable<?>) body.getGenericType()).getName();
targetClassRef = getGenericParamClass(targetClassRef, typeName);
@@ -202,7 +202,7 @@ public class ApiDocBuilder {
child.setExample(apiParamInfo.getExample());
child.setDescription(apiParamInfo.getValue());
child.setOrderIndex(apiParamInfo.getPosition());
if(!StringUtils.isEmpty(apiParamInfo.getName())){
if (!StringUtils.isEmpty(apiParamInfo.getName())) {
child.setName(apiParamInfo.getName());
}
}
@@ -264,7 +264,7 @@ public class ApiDocBuilder {
elementClass = PluginUtil.getGenericType(genericType);
if (elementClass instanceof TypeVariable) {
TypeVariable<?> typeVariable = (TypeVariable<?>) elementClass;
Class<?> genericDeclaration = (Class<?>)typeVariable.getGenericDeclaration();
Class<?> genericDeclaration = (Class<?>) typeVariable.getGenericDeclaration();
Class<?> realClass = this.getGenericParamClass(genericDeclaration, typeVariable.getName());
if (realClass != null) {
elementClass = realClass;
@@ -285,7 +285,7 @@ public class ApiDocBuilder {
if (isList) {
Class<?> elType = (Class<?>) genericElType;
boolean primitive = ClassUtil.isPrimitive(elType.getName());
fieldDocInfo.setType("List<List<"+ (primitive ? elType.getSimpleName() : "Object") +">>");
fieldDocInfo.setType("List<List<" + (primitive ? elType.getSimpleName() : "Object") + ">>");
List<FieldDocInfo> fieldDocInfos = buildFieldDocInfosByType(elType, true, null);
fieldDocInfo.setChildren(fieldDocInfos);
}
@@ -336,7 +336,7 @@ public class ApiDocBuilder {
// 解决循环依赖问题
boolean cycle = isCycle(clazz, field);
Type genericType = PluginUtil.getGenericType(field);
Class<?> generic = genericType instanceof Class<?> && genericType != clazz? (Class<?>) genericType : null;
Class<?> generic = genericType instanceof Class<?> && genericType != clazz ? (Class<?>) genericType : null;
List<FieldDocInfo> children = cycle ? Collections.emptyList()
: buildFieldDocInfosByType(clazz, false, generic);
fieldDocInfo.setChildren(children);