mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
5.0
This commit is contained in:
@@ -49,7 +49,6 @@ import lombok.Data;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
@@ -74,7 +73,6 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
@@ -84,6 +82,8 @@ import java.util.stream.Stream;
|
||||
*/
|
||||
public class SwaggerPluginService {
|
||||
|
||||
public static final String DUBBO = "dubbo";
|
||||
public static final String VERSION = "1.0";
|
||||
private final TornaConfig tornaConfig;
|
||||
private final OpenClient client;
|
||||
private boolean existsApiIgnore = true;
|
||||
@@ -224,22 +224,27 @@ public class SwaggerPluginService {
|
||||
List<DocItem> folders = new ArrayList<>(controllerDocMap.size());
|
||||
for (Map.Entry<String, List<DocItem>> entry : folderDocMap.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
ControllerInfo info = controllerInfoList
|
||||
ControllerInfo ctrlInfo = controllerInfoList
|
||||
.stream()
|
||||
.filter(controllerInfo -> name.equals(controllerInfo.getName()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (info == null) {
|
||||
if (ctrlInfo == null) {
|
||||
continue;
|
||||
}
|
||||
DocItem docItem = new DocItem();
|
||||
docItem.setName(name);
|
||||
docItem.setDefinition(info.getDescription());
|
||||
docItem.setOrderIndex(info.getPosition());
|
||||
docItem.setDefinition(ctrlInfo.getDescription());
|
||||
docItem.setOrderIndex(ctrlInfo.getPosition());
|
||||
docItem.setIsFolder(Booleans.TRUE);
|
||||
List<DocItem> items = entry.getValue();
|
||||
items.sort(Comparator.comparing(DocItem::getOrderIndex));
|
||||
docItem.setItems(items);
|
||||
DubboInfo dubboInfo = new DubboInfo();
|
||||
dubboInfo.setInterfaceName(ctrlInfo.getControllerClass().getName());
|
||||
dubboInfo.setVersion(VERSION);
|
||||
dubboInfo.setProtocol(DUBBO);
|
||||
docItem.setDubboInfo(dubboInfo);
|
||||
folders.add(docItem);
|
||||
}
|
||||
return folders;
|
||||
@@ -416,45 +421,7 @@ public class SwaggerPluginService {
|
||||
}
|
||||
|
||||
protected List<DocParamPath> buildPathParams(Method method) {
|
||||
List<ApiImplicitParam> apiImplicitParamList = buildApiImplicitParams(method, param -> "path".equalsIgnoreCase(param.paramType()));
|
||||
List<DocParamPath> docParamPaths = new ArrayList<>(apiImplicitParamList.size());
|
||||
if (!apiImplicitParamList.isEmpty()) {
|
||||
for (ApiImplicitParam apiImplicitParam : apiImplicitParamList) {
|
||||
DocParamPath docParamPath = new DocParamPath();
|
||||
docParamPath.setName(apiImplicitParam.name());
|
||||
docParamPath.setRequired(Booleans.toValue(apiImplicitParam.required()));
|
||||
docParamPath.setDescription(apiImplicitParam.value());
|
||||
docParamPath.setExample(apiImplicitParam.example());
|
||||
docParamPath.setType(getDataType(apiImplicitParam));
|
||||
docParamPaths.add(docParamPath);
|
||||
}
|
||||
}
|
||||
Parameter[] parameters = method.getParameters();
|
||||
for (Parameter parameter : parameters) {
|
||||
PathVariable pathVariable = parameter.getAnnotation(PathVariable.class);
|
||||
if (pathVariable != null) {
|
||||
String name = pathVariable.value();
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
name = pathVariable.name();
|
||||
}
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
name = parameter.getName();
|
||||
}
|
||||
// 如果已经有了不添加
|
||||
if (containsName(docParamPaths, name) || isIgnoreParameter(parameter)) {
|
||||
continue;
|
||||
}
|
||||
DocParamInfo docParamInfo = buildDocParamInfo(parameter);
|
||||
DocParamPath docParamPath = new DocParamPath();
|
||||
docParamPath.setName(name);
|
||||
docParamPath.setType(docParamInfo.getType());
|
||||
docParamPath.setRequired(Booleans.toValue(pathVariable.required()));
|
||||
docParamPath.setDescription(docParamInfo.getDescription());
|
||||
docParamPath.setExample(docParamInfo.getExample());
|
||||
docParamPaths.add(docParamPath);
|
||||
}
|
||||
}
|
||||
return docParamPaths;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private static DocParamInfo buildDocParamInfo(Parameter parameter) {
|
||||
@@ -487,87 +454,11 @@ public class SwaggerPluginService {
|
||||
}
|
||||
|
||||
protected List<DocParamHeader> buildHeaderParams(Method method) {
|
||||
List<ApiImplicitParam> apiImplicitParamList = buildApiImplicitParams(method, param -> "header".equalsIgnoreCase(param.paramType()));
|
||||
List<DocParamHeader> docParamHeaders = new ArrayList<>(apiImplicitParamList.size());
|
||||
if (!apiImplicitParamList.isEmpty()) {
|
||||
for (ApiImplicitParam apiImplicitParam : apiImplicitParamList) {
|
||||
DocParamHeader docParamHeader = new DocParamHeader();
|
||||
docParamHeader.setName(apiImplicitParam.name());
|
||||
docParamHeader.setRequired(Booleans.toValue(apiImplicitParam.required()));
|
||||
docParamHeader.setDescription(apiImplicitParam.value());
|
||||
docParamHeader.setExample(apiImplicitParam.example());
|
||||
docParamHeaders.add(docParamHeader);
|
||||
}
|
||||
}
|
||||
Parameter[] parameters = method.getParameters();
|
||||
for (Parameter parameter : parameters) {
|
||||
RequestHeader requestHeader = parameter.getAnnotation(RequestHeader.class);
|
||||
if (requestHeader != null) {
|
||||
String name = getParameterName(parameter);
|
||||
// 如果已经有了不添加
|
||||
if (containsName(docParamHeaders, name) || isIgnoreParameter(parameter)) {
|
||||
continue;
|
||||
}
|
||||
DocParamInfo docParamInfo = buildDocParamInfo(parameter);
|
||||
DocParamHeader docParamHeader = new DocParamHeader();
|
||||
docParamHeader.setName(name);
|
||||
docParamHeader.setRequired(Booleans.toValue(requestHeader.required()));
|
||||
docParamHeader.setDescription(docParamInfo.getDescription());
|
||||
docParamHeader.setExample(docParamInfo.getExample());
|
||||
docParamHeaders.add(docParamHeader);
|
||||
}
|
||||
}
|
||||
return docParamHeaders;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
protected List<DocParamReq> buildQueryParams(Method method, String httpMethod) {
|
||||
List<ApiImplicitParam> apiImplicitParamList = buildApiImplicitParams(method, param -> "query".equalsIgnoreCase(param.paramType()));
|
||||
List<DocParamReq> docParamReqs = new ArrayList<>(apiImplicitParamList.size());
|
||||
if (!apiImplicitParamList.isEmpty()) {
|
||||
for (ApiImplicitParam apiImplicitParam : apiImplicitParamList) {
|
||||
DocParamReq paramReq = new DocParamReq();
|
||||
paramReq.setName(apiImplicitParam.name());
|
||||
paramReq.setRequired(Booleans.toValue(apiImplicitParam.required()));
|
||||
paramReq.setDescription(apiImplicitParam.value());
|
||||
paramReq.setExample(apiImplicitParam.example());
|
||||
paramReq.setType(getDataType(apiImplicitParam));
|
||||
docParamReqs.add(paramReq);
|
||||
}
|
||||
}
|
||||
Parameter[] parameters = method.getParameters();
|
||||
for (Parameter parameter : parameters) {
|
||||
PathVariable pathVariable = parameter.getAnnotation(PathVariable.class);
|
||||
if (pathVariable != null) {
|
||||
continue;
|
||||
}
|
||||
RequestHeader requestHeader = parameter.getAnnotation(RequestHeader.class);
|
||||
if (requestHeader != null) {
|
||||
continue;
|
||||
}
|
||||
Class<?> parameterType = parameter.getType();
|
||||
String name = getParameterName(parameter);
|
||||
// 如果已经有了不添加
|
||||
if (containsName(docParamReqs, name) || isIgnoreParameter(parameter)) {
|
||||
continue;
|
||||
}
|
||||
RequestParam requestParam = parameter.getAnnotation(RequestParam.class);
|
||||
// 如果是Get请求
|
||||
if (httpMethod.equalsIgnoreCase(HttpMethod.GET.name()) || requestParam != null) {
|
||||
boolean isPojo = PluginUtil.isPojo(parameterType);
|
||||
// 当get请求时,遇到普通类则认为类中的属性都是query参数
|
||||
if (isPojo) {
|
||||
List<DocParamReq> docParamReqList = buildReqClassParams(parameterType);
|
||||
docParamReqs.addAll(docParamReqList);
|
||||
} else {
|
||||
DocParamReq docParamReq = buildDocParamReq(parameter);
|
||||
Optional<Boolean> requiredOpt = Optional.ofNullable(requestParam).map(RequestParam::required);
|
||||
// 如果定义了RequestParam,required由它来指定
|
||||
requiredOpt.ifPresent(aBoolean -> docParamReq.setRequired(Booleans.toValue(aBoolean)));
|
||||
docParamReqs.add(docParamReq);
|
||||
}
|
||||
}
|
||||
}
|
||||
return docParamReqs;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
protected DocParamReq buildDocParamReq(Parameter parameter) {
|
||||
@@ -594,38 +485,7 @@ public class SwaggerPluginService {
|
||||
if (StringUtils.hasText(fieldName)) {
|
||||
return fieldName;
|
||||
}
|
||||
RequestParam requestParam = parameter.getAnnotation(RequestParam.class);
|
||||
PathVariable pathVariable = parameter.getAnnotation(PathVariable.class);
|
||||
RequestHeader requestHeader = parameter.getAnnotation(RequestHeader.class);
|
||||
String name = parameter.getName();
|
||||
if (requestParam != null) {
|
||||
String val = requestParam.value();
|
||||
if (StringUtils.isEmpty(val)) {
|
||||
val = requestParam.name();
|
||||
}
|
||||
if (StringUtils.hasText(val)) {
|
||||
name = val;
|
||||
}
|
||||
}
|
||||
if (pathVariable != null) {
|
||||
String val = pathVariable.value();
|
||||
if (StringUtils.isEmpty(val)) {
|
||||
val = pathVariable.name();
|
||||
}
|
||||
if (StringUtils.hasText(val)) {
|
||||
name = val;
|
||||
}
|
||||
}
|
||||
if (requestHeader != null) {
|
||||
String val = requestHeader.value();
|
||||
if (StringUtils.isEmpty(val)) {
|
||||
val = requestHeader.name();
|
||||
}
|
||||
if (StringUtils.hasText(val)) {
|
||||
name = val;
|
||||
}
|
||||
}
|
||||
return name;
|
||||
return parameter.getName();
|
||||
}
|
||||
|
||||
protected DocParamReqWrapper buildRequestParams(ControllerInfo controllerInfo, Method method, String httpMethod) {
|
||||
@@ -662,7 +522,6 @@ public class SwaggerPluginService {
|
||||
continue;
|
||||
}
|
||||
int mode = tornaConfig.getMode();
|
||||
RequestBody requestBody = parameter.getAnnotation(RequestBody.class);
|
||||
Class<?> type = parameter.getType();
|
||||
Type parameterizedType = parameter.getParameterizedType();
|
||||
if (parameterizedType instanceof TypeVariable) {
|
||||
@@ -677,7 +536,7 @@ public class SwaggerPluginService {
|
||||
ApiParamWrapper apiParamWrapper = new ApiParamWrapper(parameter.getAnnotation(ApiParam.class));
|
||||
array = PluginUtil.isCollectionOrArray(type);
|
||||
Map<String, Class<?>> genericParamMap = buildParamsByGeneric(controllerInfo, parameterizedType);
|
||||
if (requestBody != null || mode == ModeEnum.DUBBO.getValue()) {
|
||||
if (mode == ModeEnum.DUBBO.getValue()) {
|
||||
List<DocParamReq> docParamReqList;
|
||||
if (array) {
|
||||
// 获取数元素类型
|
||||
@@ -957,7 +816,7 @@ public class SwaggerPluginService {
|
||||
return Collections.emptyMap();
|
||||
} else {
|
||||
ParameterizedType parameterizedType = (ParameterizedType) genType;
|
||||
Class<?> rawType = (Class<?>)parameterizedType.getRawType();
|
||||
Class<?> rawType = (Class<?>) parameterizedType.getRawType();
|
||||
List<String> classGenericParamNameList = PluginUtil.getClassGenericParamName(rawType);
|
||||
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
|
||||
Class<?> superclass = controllerClass.getSuperclass();
|
||||
|
@@ -3,10 +3,6 @@ package cn.torna.swaggerplugin.builder;
|
||||
import cn.torna.swaggerplugin.bean.ControllerInfo;
|
||||
import cn.torna.swaggerplugin.bean.TornaConfig;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -29,19 +25,6 @@ public abstract class HttpMethodInfoBuilder implements RequestInfoBuilder {
|
||||
|
||||
@Override
|
||||
public String getHttpMethod() {
|
||||
String httpMethod = apiOperation.httpMethod();
|
||||
if (StringUtils.hasText(httpMethod)) {
|
||||
return httpMethod;
|
||||
}
|
||||
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
|
||||
if (requestMapping != null) {
|
||||
RequestMethod[] methods = requestMapping.method();
|
||||
if (methods.length == 0) {
|
||||
return this.tornaConfig.getMethodWhenMulti();
|
||||
} else {
|
||||
return methods[0].name();
|
||||
}
|
||||
}
|
||||
return tornaConfig.getDefaultHttpMethod();
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,6 @@
|
||||
package cn.torna.swaggerplugin.util;
|
||||
|
||||
import cn.torna.swaggerplugin.scaner.ClassScanner;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.multipart.MultipartRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -74,12 +71,9 @@ public class ClassUtil {
|
||||
public static boolean isSpecialType(Class<?> paramType) {
|
||||
// 特殊参数
|
||||
boolean special = (
|
||||
WebRequest.class.isAssignableFrom(paramType) ||
|
||||
MultipartRequest.class.isAssignableFrom(paramType) ||
|
||||
Principal.class.isAssignableFrom(paramType) ||
|
||||
InputStream.class.isAssignableFrom(paramType) ||
|
||||
Reader.class.isAssignableFrom(paramType) ||
|
||||
HttpMethod.class == paramType ||
|
||||
Locale.class == paramType ||
|
||||
TimeZone.class == paramType ||
|
||||
ZoneId.class == paramType ||
|
||||
|
Reference in New Issue
Block a user