fix #I22O4F

This commit is contained in:
oppofind 2020-10-30 20:01:29 +08:00
parent 432d7c6bdc
commit c12376e06d
6 changed files with 107 additions and 37 deletions

View File

@ -28,18 +28,11 @@ import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.constants.DocAnnotationConstants;
import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.constants.DocTags;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.ApiReturn;
import com.power.doc.model.CustomRespField;
import com.power.doc.model.DocJavaField;
import com.power.doc.model.*;
import com.power.doc.utils.*;
import com.thoughtworks.qdox.model.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
@ -50,17 +43,25 @@ public class JsonBuildHelper {
/**
* build return json
*
* @param method The JavaMethod object
* @param docJavaMethod The JavaMethod object
* @param builder ProjectDocConfigBuilder builder
* @return String
*/
public static String buildReturnJson(JavaMethod method, ProjectDocConfigBuilder builder) {
public static String buildReturnJson(DocJavaMethod docJavaMethod, ProjectDocConfigBuilder builder) {
JavaMethod method = docJavaMethod.getJavaMethod();
if (method.getReturns().isVoid()) {
return "This api return nothing.";
}
ApiReturn apiReturn = DocClassUtil.processReturnType(method.getReturnType().getGenericCanonicalName());
String returnType = apiReturn.getGenericCanonicalName();
String typeName = apiReturn.getSimpleName();
Map<String, JavaType> actualTypesMap = docJavaMethod.getActualTypesMap();
String returnType = apiReturn.getGenericCanonicalName();
if (Objects.nonNull(actualTypesMap)) {
for (Map.Entry<String, JavaType> entry : actualTypesMap.entrySet()) {
typeName = typeName.replace(entry.getKey(), entry.getValue().getCanonicalName());
returnType = returnType.replace(entry.getKey(), entry.getValue().getCanonicalName());
}
}
return JsonFormatUtil.formatJson(buildJson(typeName, returnType, Boolean.TRUE, 0, new HashMap<>(), builder));
}

View File

@ -0,0 +1,39 @@
package com.power.doc.model;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaType;
import java.util.Map;
/**
* @since 1.9.8
* @author yu 2020/10/30.
*/
public class DocJavaMethod {
private JavaMethod javaMethod;
private Map<String, JavaType> actualTypesMap;
public static DocJavaMethod builder(){
return new DocJavaMethod();
}
public JavaMethod getJavaMethod() {
return javaMethod;
}
public DocJavaMethod setJavaMethod(JavaMethod javaMethod) {
this.javaMethod = javaMethod;
return this;
}
public Map<String, JavaType> getActualTypesMap() {
return actualTypesMap;
}
public DocJavaMethod setActualTypesMap(Map<String, JavaType> actualTypesMap) {
this.actualTypesMap = actualTypesMap;
return this;
}
}

View File

@ -33,10 +33,9 @@ import com.power.doc.utils.JavaClassUtil;
import com.power.doc.utils.JavaClassValidateUtil;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.*;
import static com.power.doc.constants.DocGlobalConstants.NO_COMMENTS_FOUND;
@ -92,14 +91,23 @@ public interface IDocBuildTemplate<T> {
}
default List<ApiParam> buildReturnApiParams(JavaMethod method, ProjectDocConfigBuilder projectBuilder) {
default List<ApiParam> buildReturnApiParams(DocJavaMethod docJavaMethod, ProjectDocConfigBuilder projectBuilder) {
JavaMethod method = docJavaMethod.getJavaMethod();
if (method.getReturns().isVoid()) {
return null;
}
Map<String, JavaType> actualTypesMap = docJavaMethod.getActualTypesMap();
ApiReturn apiReturn = DocClassUtil.processReturnType(method.getReturnType().getGenericCanonicalName());
String returnType = apiReturn.getGenericCanonicalName();
if (Objects.nonNull(actualTypesMap)) {
for (Map.Entry<String, JavaType> entry : actualTypesMap.entrySet()) {
returnType = returnType.replace(entry.getKey(), entry.getValue().getCanonicalName());
}
}
String typeName = apiReturn.getSimpleName();
if (this.ignoreReturnObject(typeName,projectBuilder.getApiConfig().getIgnoreRequestParams())) {
if (this.ignoreReturnObject(typeName, projectBuilder.getApiConfig().getIgnoreRequestParams())) {
return null;
}
if (JavaClassValidateUtil.isPrimitive(typeName)) {
@ -114,7 +122,7 @@ public interface IDocBuildTemplate<T> {
return ParamsBuildHelper.primitiveReturnRespComment("array of " + DocClassUtil.processTypeNameForParams(gicName));
}
return ParamsBuildHelper.buildParams(gicName, "", 0, null, projectBuilder.getCustomRespFieldMap(),
Boolean.TRUE, new HashMap<>(), projectBuilder, null,0);
Boolean.TRUE, new HashMap<>(), projectBuilder, null, 0);
} else {
return null;
}
@ -128,11 +136,11 @@ public interface IDocBuildTemplate<T> {
return ParamsBuildHelper.primitiveReturnRespComment("key value");
}
return ParamsBuildHelper.buildParams(keyValue[1], "", 0, null, projectBuilder.getCustomRespFieldMap(),
Boolean.TRUE, new HashMap<>(), projectBuilder, null,0);
Boolean.TRUE, new HashMap<>(), projectBuilder, null, 0);
}
if (StringUtil.isNotEmpty(returnType)) {
return ParamsBuildHelper.buildParams(returnType, "", 0, null, projectBuilder.getCustomRespFieldMap(),
Boolean.TRUE, new HashMap<>(), projectBuilder, null,0);
Boolean.TRUE, new HashMap<>(), projectBuilder, null, 0);
}
return null;
}
@ -141,6 +149,6 @@ public interface IDocBuildTemplate<T> {
T getSingleApiData(ProjectDocConfigBuilder projectBuilder, String apiClassName);
boolean ignoreReturnObject(String typeName,List<String> ignoreParams);
boolean ignoreReturnObject(String typeName, List<String> ignoreParams);
}

View File

@ -30,10 +30,7 @@ import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.constants.DocTags;
import com.power.doc.constants.DubboAnnotationConstants;
import com.power.doc.helper.ParamsBuildHelper;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.ApiParam;
import com.power.doc.model.CustomRespField;
import com.power.doc.model.JavaMethodDoc;
import com.power.doc.model.*;
import com.power.doc.model.rpc.RpcApiDoc;
import com.power.doc.utils.DocClassUtil;
import com.power.doc.utils.DocUtil;
@ -151,7 +148,7 @@ public class RpcDocBuildTemplate implements IDocBuildTemplate<RpcApiDoc> {
List<ApiParam> requestParams = requestParams(method, projectBuilder);
apiMethodDoc.setRequestParams(requestParams);
// build response params
List<ApiParam> responseParams = buildReturnApiParams(method, projectBuilder);
List<ApiParam> responseParams = buildReturnApiParams(DocJavaMethod.builder().setJavaMethod(method), projectBuilder);
apiMethodDoc.setResponseParams(responseParams);
methodDocList.add(apiMethodDoc);

View File

@ -128,9 +128,22 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
}
}
List<JavaMethod> methods = cls.getMethods();
List<DocJavaMethod> docJavaMethods = new ArrayList<>(methods.size());
for (JavaMethod method : methods) {
docJavaMethods.add(DocJavaMethod.builder().setJavaMethod(method));
}
JavaClass parentClass = cls.getSuperJavaClass();
if (Objects.nonNull(parentClass)) {
Map<String, JavaType> actualTypesMap = JavaClassUtil.getActualTypesMap(parentClass);
List<JavaMethod> parentMethodList = parentClass.getMethods();
for (JavaMethod method : parentMethodList) {
docJavaMethods.add(DocJavaMethod.builder().setJavaMethod(method).setActualTypesMap(actualTypesMap));
}
}
List<ApiMethodDoc> methodDocList = new ArrayList<>(methods.size());
int methodOrder = 0;
for (JavaMethod method : methods) {
for (DocJavaMethod docJavaMethod : docJavaMethods) {
JavaMethod method = docJavaMethod.getJavaMethod();
if (method.isPrivate()) {
continue;
}
@ -173,7 +186,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
apiMethodDoc.setPath(requestMapping.getShortUrl());
apiMethodDoc.setDeprecated(requestMapping.isDeprecated());
// build request params
List<ApiParam> requestParams = requestParams(method, projectBuilder);
List<ApiParam> requestParams = requestParams(docJavaMethod, projectBuilder);
if (paramsDataToTree) {
requestParams = ApiParamTreeUtil.apiParamToTree(requestParams);
}
@ -190,16 +203,16 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
apiMethodDoc.setRequestHeaders(allApiReqHeaders);
// build request json
ApiRequestExample requestExample = buildReqJson(method, apiMethodDoc, requestMapping.getMethodType(),
ApiRequestExample requestExample = buildReqJson(docJavaMethod, apiMethodDoc, requestMapping.getMethodType(),
projectBuilder);
String requestJson = requestExample.getExampleBody();
// set request example detail
apiMethodDoc.setRequestExample(requestExample);
apiMethodDoc.setRequestUsage(requestJson == null ? requestExample.getUrl() : requestJson);
// build response usage
apiMethodDoc.setResponseUsage(JsonBuildHelper.buildReturnJson(method, projectBuilder));
apiMethodDoc.setResponseUsage(JsonBuildHelper.buildReturnJson(docJavaMethod, projectBuilder));
// build response params
List<ApiParam> responseParams = buildReturnApiParams(method, projectBuilder);
List<ApiParam> responseParams = buildReturnApiParams(docJavaMethod, projectBuilder);
if (paramsDataToTree) {
responseParams = ApiParamTreeUtil.apiParamToTree(responseParams);
}
@ -210,8 +223,10 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
return methodDocList;
}
private ApiRequestExample buildReqJson(JavaMethod method, ApiMethodDoc apiMethodDoc, String methodType,
private ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc apiMethodDoc, String methodType,
ProjectDocConfigBuilder configBuilder) {
JavaMethod method = javaMethod.getJavaMethod();
List<JavaParameter> parameterList = method.getParameters();
List<ApiReqHeader> reqHeaderList = apiMethodDoc.getRequestHeaders();
StringBuilder header = new StringBuilder(reqHeaderList.size());
@ -224,7 +239,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
header.toString(), apiMethodDoc.getUrl());
return ApiRequestExample.builder().setUrl(apiMethodDoc.getUrl()).setExampleBody(format);
}
Map<String, JavaType> actualTypesMap = javaMethod.getActualTypesMap();
Map<String, String> constantsMap = configBuilder.getConstantsMap();
boolean requestFieldToUnderline = configBuilder.getApiConfig().isRequestFieldToUnderline();
Map<String, String> replacementMap = configBuilder.getReplaceClassMap();
@ -236,6 +251,9 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
out:
for (JavaParameter parameter : parameterList) {
JavaType javaType = parameter.getType();
if (Objects.nonNull(actualTypesMap) && Objects.nonNull(actualTypesMap.get(javaType.getCanonicalName()))) {
javaType = actualTypesMap.get(javaType.getCanonicalName());
}
String paramName = parameter.getName();
String typeName = javaType.getFullyQualifiedName();
String gicTypeName = javaType.getGenericCanonicalName();
@ -426,7 +444,8 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
return requestExample;
}
private List<ApiParam> requestParams(final JavaMethod javaMethod, ProjectDocConfigBuilder builder) {
private List<ApiParam> requestParams(final DocJavaMethod docJavaMethod, ProjectDocConfigBuilder builder) {
JavaMethod javaMethod = docJavaMethod.getJavaMethod();
boolean isStrict = builder.getApiConfig().isStrict();
Map<String, CustomRespField> responseFieldMap = new HashMap<>();
String className = javaMethod.getDeclaringClass().getCanonicalName();
@ -439,13 +458,18 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
Map<String, String> constantsMap = builder.getConstantsMap();
boolean requestFieldToUnderline = builder.getApiConfig().isRequestFieldToUnderline();
List<ApiParam> paramList = new ArrayList<>();
Map<String, JavaType> actualTypesMap = docJavaMethod.getActualTypesMap();
int requestBodyCounter = 0;
out:
for (JavaParameter parameter : parameterList) {
String paramName = parameter.getName();
String typeName = parameter.getType().getGenericCanonicalName();
String simpleName = parameter.getType().getValue().toLowerCase();
String fullTypeName = parameter.getType().getFullyQualifiedName();
JavaType javaType = parameter.getType();
if (Objects.nonNull(actualTypesMap) && Objects.nonNull(actualTypesMap.get(javaType.getCanonicalName()))) {
javaType = actualTypesMap.get(javaType.getCanonicalName());
}
String typeName = javaType.getGenericCanonicalName();
String simpleName = javaType.getValue().toLowerCase();
String fullTypeName = javaType.getFullyQualifiedName();
String commentClass = paramTagMap.get(paramName);
String rewriteClassName = getRewriteClassName(replacementMap, fullTypeName, commentClass);

View File

@ -246,6 +246,7 @@ public class JavaClassValidateUtil {
switch (typeName) {
case "org.slf4j.Logger":
case "org.apache.ibatis.logging.Log":
case "java.lang.Class":
return true;
default:
return false;