mergeSourceBuilder文件

This commit is contained in:
xingzi 2019-10-19 11:51:20 +08:00
commit 7c6c3e0a32
3 changed files with 96 additions and 55 deletions

View File

@ -72,5 +72,16 @@
1. 优化注释换行\n\r问题。
2. 修改bug #I135PG
3. 添加requestHeader功能
#### 版本号1.7.2
- 更新日期:待定
- 更新内容:
1. 优化注释换行\n\r问题。
2. 修改bug #I135PG
3. 添加@requestHeader注解的支持文档自定将参数绑定到请求头列表中。
4. 增加javadoc apiNote tag的支持。
5. 解决扫描分析controller中private方法的问题。
6. 添加支持@RequestParam注解重写参数名和设置默认值的文档解析。
7. 支持使用@PostMapping和@PutMapping请求自定义注解接收单个json参数场景下生成json请求实例。

View File

@ -49,9 +49,6 @@ public class SourceBuilder {
private static final String METHOD_DESCRIPTION = "apiNote";
private Map<String, JavaClass> javaFilesMap = new HashMap<>();
private Map<String, CustomRespField> fieldMap = new HashMap<>();
private JavaProjectBuilder builder;
@ -113,31 +110,14 @@ public class SourceBuilder {
int order = 0;
for (JavaClass cls : javaClasses) {
if (checkController(cls)) {
String controllerName = cls.getName();
if (StringUtil.isNotEmpty(packageMatch)) {
if (DocUtil.isMatch(packageMatch, cls.getCanonicalName())) {
order++;
List<ApiMethodDoc> apiMethodDocs = buildControllerMethod(cls);
ApiDoc apiDoc = new ApiDoc();
apiDoc.setName(controllerName);
apiDoc.setAlias(controllerName);
apiDoc.setOrder(order);
this.handControllerAlias(apiDoc);
apiDoc.setDesc(cls.getComment());
apiDoc.setList(apiMethodDocs);
apiDocList.add(apiDoc);
this.handleApiDoc(cls,apiDocList,order);
}
} else {
order++;
List<ApiMethodDoc> apiMethodDocs = buildControllerMethod(cls);
ApiDoc apiDoc = new ApiDoc();
apiDoc.setOrder(order);
apiDoc.setName(controllerName);
apiDoc.setAlias(controllerName);
this.handControllerAlias(apiDoc);
apiDoc.setDesc(cls.getComment());
apiDoc.setList(apiMethodDocs);
apiDocList.add(apiDoc);
this.handleApiDoc(cls,apiDocList,order);
}
}
}
@ -193,14 +173,15 @@ public class SourceBuilder {
ApiMethodDoc apiMethodDoc = new ApiMethodDoc();
apiMethodDoc.setOrder(methodOrder);
apiMethodDoc.setDesc(method.getComment());
String apiNoteValue = DocUtil.getNormalTagComments(method,DocTags.API_NOTE,cls.getName());
if(StringUtil.isEmpty(apiNoteValue)){
String apiNoteValue = DocUtil.getNormalTagComments(method, DocTags.API_NOTE, cls.getName());
if (StringUtil.isEmpty(apiNoteValue)) {
apiNoteValue = method.getComment();
}
apiMethodDoc.setDetail(apiNoteValue);
List<JavaAnnotation> annotations = method.getAnnotations();
String url = null;
String methodType = null;
boolean isPostMethod = false;
int methodCounter = 0;
for (JavaAnnotation annotation : annotations) {
String annotationName = annotation.getType().getName();
@ -210,6 +191,9 @@ public class SourceBuilder {
if (null != nameParam) {
methodType = nameParam.toString();
methodType = DocUtil.handleHttpMethod(methodType);
if ("POST".equals(methodType) || "PUT".equals(methodType)) {
isPostMethod = true;
}
} else {
methodType = "GET";
}
@ -222,6 +206,7 @@ public class SourceBuilder {
url = DocUtil.handleMappingValue(annotation);
methodType = "POST";
methodCounter++;
isPostMethod = true;
} else if (PUT_MAPPING.equals(annotationName) || DocGlobalConstants.PUT_MAPPING_FULLY.equals(annotationName)) {
url = DocUtil.handleMappingValue(annotation);
methodType = "PUT";
@ -248,10 +233,11 @@ public class SourceBuilder {
}
StringBuilder desc= new StringBuilder();
for (Map.Entry<String,String> map : paramMap.entrySet()){
if(map.getKey().equals(javaParameter.getName())){
desc.append(map.getValue());
}
for (Map.Entry<String, String> map : paramMap.entrySet()) {
if (map.getKey().equals(javaParameter.getName())) {
apiReqHeader.setDesc(map.getValue());
}
}
if(requestHeaderMap.get("defaultValue") != null){
desc.append("<br/>")
@ -289,7 +275,7 @@ public class SourceBuilder {
apiMethodDoc.setUrl(UrlUtil.simplifyUrl(url));
List<ApiParam> requestParams = requestParams(method, DocTags.PARAM, cls.getCanonicalName());
apiMethodDoc.setRequestParams(requestParams);
String requestJson = buildReqJson(method, apiMethodDoc);
String requestJson = buildReqJson(method, apiMethodDoc, isPostMethod);
apiMethodDoc.setRequestUsage(JsonFormatUtil.formatJson(requestJson));
apiMethodDoc.setResponseUsage(buildReturnJson(method, this.fieldMap));
@ -922,7 +908,7 @@ public class SourceBuilder {
return data0.toString();
}
private String buildReqJson(JavaMethod method, ApiMethodDoc apiMethodDoc) {
private String buildReqJson(JavaMethod method, ApiMethodDoc apiMethodDoc, boolean isPostMethod) {
List<JavaParameter> parameterList = method.getParameters();
if (parameterList.size() < 1) {
return apiMethodDoc.getUrl();
@ -938,6 +924,8 @@ public class SourceBuilder {
if (!DocClassUtil.isMvcIgnoreParams(typeName)) {
List<JavaAnnotation> annotations = parameter.getAnnotations();
int requestBodyCounter = 0;
String defaultVal = null;
boolean notHasRequestParams = true;
for (JavaAnnotation annotation : annotations) {
String annotationName = annotation.getType().getSimpleName();
if (REQUEST_BODY.equals(annotationName) || DocGlobalConstants.REQUEST_BODY_FULLY.equals(annotationName)) {
@ -955,15 +943,45 @@ public class SourceBuilder {
return buildJson(typeName, gicTypeName, this.fieldMap, false);
}
}
if (DocAnnotationConstants.SHORT_REQ_PARAM.equals(annotationName)) {
notHasRequestParams = false;
}
AnnotationValue annotationDefaultVal = annotation.getProperty(DocAnnotationConstants.DEFAULT_VALUE_PROP);
if (null != annotationDefaultVal) {
defaultVal = StringUtil.removeQuotes(annotationDefaultVal.toString());
}
AnnotationValue annotationValue = annotation.getProperty(DocAnnotationConstants.VALUE_PROP);
if (null != annotationValue) {
paraName = StringUtil.removeQuotes(annotationValue.toString());
}
AnnotationValue annotationOfName = annotation.getProperty(DocAnnotationConstants.NAME_PROP);
if (null != annotationOfName) {
paraName = StringUtil.removeQuotes(annotationOfName.toString());
}
if (REQUEST_HERDER.equals(annotationName)) {
paraName = null;
}
}
if (requestBodyCounter < 1 && paraName != null) {
paramsMap.put(paraName, DocUtil.getValByTypeAndFieldName(simpleTypeName, paraName,
true));
if (DocClassUtil.isPrimitive(typeName) && parameterList.size() == 1
&& isPostMethod && notHasRequestParams && !containsBrace) {
apiMethodDoc.setContentType(JSON_CONTENT_TYPE);
StringBuilder builder = new StringBuilder();
builder.append("{\"")
.append(paraName)
.append("\":")
.append(DocUtil.jsonValueByType(simpleTypeName))
.append("}");
return builder.toString();
}
if (requestBodyCounter < 1 && paraName != null) {
if (StringUtil.isEmpty(defaultVal)) {
paramsMap.put(paraName, DocUtil.getValByTypeAndFieldName(simpleTypeName, paraName,
true));
} else {
paramsMap.put(paraName, defaultVal);
}
}
}
}
String url;
@ -988,7 +1006,7 @@ public class SourceBuilder {
Map<String, CustomRespField> responseFieldMap = new HashMap<>();
Map<String, String> paramTagMap = DocUtil.getParamsComments(javaMethod,tagName,className);
Map<String, String> paramTagMap = DocUtil.getParamsComments(javaMethod, tagName, className);
List<JavaParameter> parameterList = javaMethod.getParameters();
if (parameterList.size() > 0) {
@ -1050,9 +1068,9 @@ public class SourceBuilder {
}
for (JavaAnnotation annotation : annotations) {
String required = "true";
AnnotationValue annotationValue = annotation.getProperty(DocAnnotationConstants.REQUIRED_PROP);
if (null != annotationValue) {
required = annotationValue.toString();
AnnotationValue annotationRequired = annotation.getProperty(DocAnnotationConstants.REQUIRED_PROP);
if (null != annotationRequired) {
required = annotationRequired.toString();
}
String annotationName = annotation.getType().getName();
if (REQUEST_HERDER.equals(annotationName)) {
@ -1099,6 +1117,15 @@ public class SourceBuilder {
}
requestBodyCounter++;
} else {
AnnotationValue annotationValue = annotation.getProperty(DocAnnotationConstants.VALUE_PROP);
if (null != annotationValue) {
paramName = StringUtil.removeQuotes(annotationValue.toString());
}
AnnotationValue annotationOfName = annotation.getProperty(DocAnnotationConstants.NAME_PROP);
if (null != annotationOfName) {
paramName = StringUtil.removeQuotes(annotationOfName.toString());
}
ApiParam param = ApiParam.of().setField(paramName)
.setType(DocClassUtil.processTypeNameForParams(simpleName))
.setDesc(comment).setRequired(true).setVersion(DocGlobalConstants.DEFAULT_VERSION);
@ -1160,23 +1187,6 @@ public class SourceBuilder {
return false;
}
/**
* is rest controller
*
* @param cls The JavaClass object
* @return boolean
*/
private boolean isRestController(JavaClass cls) {
List<JavaAnnotation> classAnnotations = cls.getAnnotations();
for (JavaAnnotation annotation : classAnnotations) {
String annotationName = annotation.getType().getName();
if (DocAnnotationConstants.SHORT_REST_CONTROLLER.equals(annotationName)) {
return true;
}
}
return false;
}
/**
* handle controller name
*
@ -1203,4 +1213,18 @@ public class SourceBuilder {
paramList.add(param);
}
}
private void handleApiDoc(JavaClass cls,List<ApiDoc> apiDocList,int order){
List<ApiMethodDoc> apiMethodDocs = buildControllerMethod(cls);
String controllerName = cls.getName();
ApiDoc apiDoc = new ApiDoc();
apiDoc.setOrder(order);
apiDoc.setName(controllerName);
apiDoc.setAlias(controllerName);
this.handControllerAlias(apiDoc);
apiDoc.setDesc(cls.getComment());
apiDoc.setList(apiMethodDocs);
apiDocList.add(apiDoc);
}
}

View File

@ -11,6 +11,10 @@ public class DocAnnotationConstants {
public static final String SHORT_REST_CONTROLLER = "RestController";
public static final String SHORT_PATH_VARIABLE = "PathVariable";
public static final String SHORT_REQ_PARAM = "RequestParam";
public static final String SHORT_JSON_IGNORE = "JsonIgnore";
public static final String SHORT_JSON_PROPERTY = "JsonProperty";
@ -25,5 +29,7 @@ public class DocAnnotationConstants {
public static final String VALUE_PROP = "value";
public static final String DEFAULT_VALUE_PROP = "defaultValue";
}