diff --git a/CHANGELOG.md b/CHANGELOG.md index 32cb97e..85d419d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ 2. 修复带正则的path路径参数解析出现截取越界的问题。 3. 添加对默认接口实现中get方法重写忽略的能力解析。 4. 修改数组、map等字段类型的自定义mock值显示错误问题。 + 5. 修复对mapping中headers的处理。 #### 版本号:2.0.6 - 更新日期: 2020-01-15 - 更新内容: diff --git a/src/main/java/com/power/doc/handler/SpringMVCRequestHeaderHandler.java b/src/main/java/com/power/doc/handler/SpringMVCRequestHeaderHandler.java index 28dce79..d92c59f 100644 --- a/src/main/java/com/power/doc/handler/SpringMVCRequestHeaderHandler.java +++ b/src/main/java/com/power/doc/handler/SpringMVCRequestHeaderHandler.java @@ -57,31 +57,18 @@ public class SpringMVCRequestHeaderHandler { if (!isMapping(annotationName) || Objects.isNull(headersObject)) { continue; } + String mappingHeader = StringUtil.removeQuotes(headersObject.toString()); + if (!mappingHeader.startsWith("[")) { + processMappingHeaders(mappingHeader, mappingHeaders); + continue; + } List headers = (LinkedList) headersObject; for (String str : headers) { String header = StringUtil.removeQuotes(str); if (header.startsWith("!")) { continue; } - if (header.contains("!=")) { - String headerName = header.substring(0, header.indexOf("!")); - ApiReqHeader apiReqHeader = ApiReqHeader.builder().setName(headerName) - .setRequired(true).setValue(null).setDesc(header).setType("string"); - mappingHeaders.add(apiReqHeader); - } else { - String headerName; - String headerValue = null; - if (header.contains("=")) { - int index = header.indexOf("="); - headerName = header.substring(0, header.indexOf("=")); - headerValue = header.substring(index + 1); - } else { - headerName = header; - } - ApiReqHeader apiReqHeader = ApiReqHeader.builder().setName(headerName) - .setRequired(true).setValue(headerValue).setDesc(header).setType("string"); - mappingHeaders.add(apiReqHeader); - } + processMappingHeaders(header, mappingHeaders); } } List reqHeaders = new ArrayList<>(); @@ -142,4 +129,26 @@ public class SpringMVCRequestHeaderHandler { return false; } } + + public void processMappingHeaders(String header, List mappingHeaders) { + if (header.contains("!=")) { + String headerName = header.substring(0, header.indexOf("!")); + ApiReqHeader apiReqHeader = ApiReqHeader.builder().setName(headerName) + .setRequired(true).setValue(null).setDesc("header condition").setType("string"); + mappingHeaders.add(apiReqHeader); + } else { + String headerName; + String headerValue = null; + if (header.contains("=")) { + int index = header.indexOf("="); + headerName = header.substring(0, index); + headerValue = header.substring(index + 1); + } else { + headerName = header; + } + ApiReqHeader apiReqHeader = ApiReqHeader.builder().setName(headerName) + .setRequired(true).setValue(headerValue).setDesc("header condition").setType("string"); + mappingHeaders.add(apiReqHeader); + } + } } diff --git a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java index 353f0cf..b4e494b 100644 --- a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java +++ b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java @@ -254,7 +254,26 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { private ApiRequestExample buildReqJson(DocJavaMethod javaMethod, ApiMethodDoc apiMethodDoc, String methodType, ProjectDocConfigBuilder configBuilder) { JavaMethod method = javaMethod.getJavaMethod(); - + Map pathParamsMap = new LinkedHashMap<>(); + List methodAnnotations = method.getAnnotations(); + for (JavaAnnotation annotation : methodAnnotations) { + String annotationName = annotation.getType().getName(); + if (annotationName.contains("Mapping")) { + Object paramsObjects = annotation.getNamedParameter("params"); + if (Objects.isNull(paramsObjects)) { + continue; + } + String params = StringUtil.removeQuotes(paramsObjects.toString()); + if (!params.startsWith("[")) { + mappingParamProcess(paramsObjects.toString(), pathParamsMap); + continue; + } + List headers = (LinkedList) paramsObjects; + for (String str : headers) { + mappingParamProcess(str, pathParamsMap); + } + } + } List parameterList = method.getParameters(); List reqHeaderList = apiMethodDoc.getRequestHeaders(); if (parameterList.size() < 1) { @@ -270,7 +289,6 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { Map constantsMap = configBuilder.getConstantsMap(); boolean requestFieldToUnderline = configBuilder.getApiConfig().isRequestFieldToUnderline(); Map replacementMap = configBuilder.getReplaceClassMap(); - Map pathParamsMap = new LinkedHashMap<>(); Map paramsComments = DocUtil.getParamsComments(method, DocTags.PARAM, null); List springMvcRequestAnnotations = SpringMvcRequestAnnotationsEnum.listSpringMvcRequestAnnotations(); List formDataList = new ArrayList<>(); @@ -492,14 +510,34 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { Map replacementMap = builder.getReplaceClassMap(); Map paramTagMap = DocUtil.getParamsComments(javaMethod, DocTags.PARAM, className); Map paramsComments = DocUtil.getParamsComments(javaMethod, DocTags.PARAM, null); + List paramList = new ArrayList<>(); + Map mappingParams = new HashMap<>(); + List methodAnnotations = javaMethod.getAnnotations(); + for (JavaAnnotation annotation : methodAnnotations) { + String annotationName = annotation.getType().getName(); + if (annotationName.contains("Mapping")) { + Object paramsObjects = annotation.getNamedParameter("params"); + if (Objects.isNull(paramsObjects)) { + continue; + } + String params = StringUtil.removeQuotes(paramsObjects.toString()); + if (!params.startsWith("[")) { + mappingParamToApiParam(paramsObjects.toString(), paramList, mappingParams); + continue; + } + List headers = (LinkedList) paramsObjects; + for (String str : headers) { + mappingParamToApiParam(str, paramList, mappingParams); + } + } + } List parameterList = javaMethod.getParameters(); if (parameterList.size() < 1) { return ApiMethodReqParam.builder() .setPathParams(new ArrayList<>(0)) - .setQueryParams(new ArrayList<>(0)) + .setQueryParams(paramList) .setRequestParams(new ArrayList<>(0)); } - List paramList = new ArrayList<>(); Map constantsMap = builder.getConstantsMap(); boolean requestFieldToUnderline = builder.getApiConfig().isRequestFieldToUnderline(); @@ -508,6 +546,9 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { out: for (JavaParameter parameter : parameterList) { String paramName = parameter.getName(); + if (mappingParams.containsKey(paramName)) { + continue; + } JavaType javaType = parameter.getType(); if (Objects.nonNull(actualTypesMap) && Objects.nonNull(actualTypesMap.get(javaType.getCanonicalName()))) { javaType = actualTypesMap.get(javaType.getCanonicalName()); @@ -783,4 +824,44 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { } return mockValue; } + + private void mappingParamProcess(String str, Map pathParamsMap) { + String param = StringUtil.removeQuotes(str); + String paramName; + String paramValue; + if (param.contains("=")) { + int index = param.indexOf("="); + paramName = param.substring(0, index); + paramValue = param.substring(index + 1); + pathParamsMap.put(paramName, paramValue); + } else { + paramName = param; + pathParamsMap.put(paramName, DocUtil.getValByTypeAndFieldName("string", paramName, Boolean.TRUE)); + } + } + + private void mappingParamToApiParam(String str, List paramList, Map mappingParams) { + String param = StringUtil.removeQuotes(str); + String paramName; + String paramValue; + + if (param.contains("=")) { + int index = param.indexOf("="); + paramName = param.substring(0, index); + paramValue = param.substring(index + 1); + } else { + paramName = param; + paramValue = DocUtil.getValByTypeAndFieldName("string", paramName, Boolean.TRUE); + } + String type = ValidateUtil.isPositiveInteger(paramValue) ? "int32" : "string"; + ApiParam apiParam = ApiParam.of().setField(paramName) + .setId(paramList.size() + 1) + .setQueryParam(true) + .setValue(paramValue) + .setType(type).setDesc("parameter condition") + .setRequired(true) + .setVersion(DocGlobalConstants.DEFAULT_VERSION); + paramList.add(apiParam); + mappingParams.put(paramName, null); + } } diff --git a/src/main/resources/template/debug-all.html b/src/main/resources/template/debug-all.html index 41b7883..70a8e56 100644 --- a/src/main/resources/template/debug-all.html +++ b/src/main/resources/template/debug-all.html @@ -1,4 +1,4 @@ -<%if(isNotEmpty(projectName)){%>${projectName}<%}else{%>API Reference<%}%>
<%if(isNotEmpty(revisionLogList)){%>
<%for(revisionLog in revisionLogList){%><%}%>
VersionUpdate TimeStatusAuthorDescription

${revisionLog.version}

${revisionLog.revisionTime}

${revisionLog.status}

${revisionLog.author}

${revisionLog.remarks}

<%}%><%for(api in apiDocList){%>

${api.order}. ${api.desc}

<%for(doc in api.list){%>

<%if(doc.deprecated){%>${api.order}.${doc.order}. ${doc.desc}<%}else{%>${api.order}.${doc.order}. ${doc.desc}<%}%>

Type: ${doc.type}

<%if(isNotEmpty(doc.author)){%>

Author: ${doc.author}

<%}%>

Content-Type: ${doc.contentType}

Description: ${doc.detail}

<%if(isNotEmpty(doc.requestHeaders)){%>

Request-headers:

<%for(header in doc.requestHeaders){%><%}%>
HeaderValueTypeRequiredDescription

${header.name}

${header.type}

${header.required}

${header.desc}

<%}%><%if(isNotEmpty(doc.pathParams)){%>

Path-parameters:

<%for(param in doc.pathParams){%><%}%>
ParameterValueTypeRequiredDescription

${param.field}

${param.type}

${param.required}

${param.desc}

<%}%><%if(isNotEmpty(doc.queryParams)){%>

Query-parameters:

<%for(param in doc.queryParams){%><%}%>
ParameterValueTypeRequiredDescription

${param.field}

<%if(param.type=="file"){%><%}else{%><%}%>

${param.type}

${param.required}

${param.desc}

<%}%><%if(isNotEmpty(doc.requestParams)){%>

Body-parameters:

<%for(param in doc.requestParams){%><%}%>
ParameterTypeRequiredDescription

${param.field}

${param.type}

${param.required}

${param.desc}

<%}%><%if(isNotEmpty(doc.requestExample.jsonBody)&&isRequestExample){%>

Request-body:

${doc.requestExample.jsonBody}
<%}%><%if(isNotEmpty(doc.responseParams)){%>

Response-fields:

<%for(param in doc.responseParams){%><%}%>
FieldTypeDescriptionSince

${param.field}

${param.type}

${param.desc}

${param.version}

<%}%>

<%if(isNotEmpty(doc.responseUsage)&&isResponseExample){%>

Response-example:

${doc.responseUsage}
<%}%><%if(isNotEmpty(doc.requestUsage)&&isRequestExample){%>

Curl-example:

${doc.requestUsage}
<%}%>
<%}%>
<%}%><%if(isNotEmpty(errorCodeList)){%>

${apiDocList.~size+1}. ${errorListTitle}

<%for(error in errorCodeList){%><%}%>
Error codeDescription

${error.value}

${error.desc}

<%}%><%if(isNotEmpty(dictList)){%>

${dictListOrder}. ${dictListTitle}

<%for(dict in dictList){%>

${dictListOrder}.${dict.order}. ${dict.title}

<%for(dataDict in dict.dataDictList){%><%}%>
CodeTypeDescription

${dataDict.value}

${dataDict.type}

${dataDict.desc}

<%}%>
<%}%>
Generated by smart-doc at ${createTime}Suggestions,contact,support and error reporting on Gitee or Github
+<%if(isNotEmpty(projectName)){%>${projectName}<%}else{%>API Reference<%}%>
<%if(isNotEmpty(revisionLogList)){%>
<%for(revisionLog in revisionLogList){%><%}%>
VersionUpdate TimeStatusAuthorDescription

${revisionLog.version}

${revisionLog.revisionTime}

${revisionLog.status}

${revisionLog.author}

${revisionLog.remarks}

<%}%><%for(api in apiDocList){%>

${api.order}. ${api.desc}

<%for(doc in api.list){%>

<%if(doc.deprecated){%>${api.order}.${doc.order}. ${doc.desc}<%}else{%>${api.order}.${doc.order}. ${doc.desc}<%}%>

Type: ${doc.type}

<%if(isNotEmpty(doc.author)){%>

Author: ${doc.author}

<%}%>

Content-Type: ${doc.contentType}

Description: ${doc.detail}

<%if(isNotEmpty(doc.requestHeaders)){%>

Request-headers:

<%for(header in doc.requestHeaders){%><%}%>
HeaderValueTypeRequiredDescription

${header.name}

${header.type}

${header.required}

${header.desc}

<%}%><%if(isNotEmpty(doc.pathParams)){%>

Path-parameters:

<%for(param in doc.pathParams){%><%}%>
ParameterValueTypeRequiredDescription

${param.field}

${param.type}

${param.required}

${param.desc}

<%}%><%if(isNotEmpty(doc.queryParams)){%>

Query-parameters:

<%for(param in doc.queryParams){%><%if(param.version !='-'){%><%}else{%><%}%><%}%>
ParameterValueTypeRequiredDescription

${param.field}

<%if(param.type=="file"){%><%}else{%><%}%>

${param.type}

${param.required}

${param.desc} @since ${param.version}

${param.desc}

<%}%><%if(isNotEmpty(doc.requestParams)){%>

Body-parameters:

<%for(param in doc.requestParams){%><%if(param.version !='-'){%><%}else{%><%}%><%}%>
ParameterTypeRequiredDescription

${param.field}

${param.type}

${param.required}

${param.desc} @since ${param.version}

${param.desc}

<%}%><%if(isNotEmpty(doc.requestExample.jsonBody)&&isRequestExample){%>

Request-body:

${doc.requestExample.jsonBody}
<%}%><%if(isNotEmpty(doc.responseParams)){%>

Response-fields:

<%for(param in doc.responseParams){%><%}%>
FieldTypeDescriptionSince

${param.field}

${param.type}

${param.desc}

${param.version}

<%}%>

<%if(isNotEmpty(doc.responseUsage)&&isResponseExample){%>

Response-example:

${doc.responseUsage}
<%}%><%if(isNotEmpty(doc.requestUsage)&&isRequestExample){%>

Curl-example:

${doc.requestUsage}
<%}%>
<%}%>
<%}%><%if(isNotEmpty(errorCodeList)){%>

${apiDocList.~size+1}. ${errorListTitle}

<%for(error in errorCodeList){%><%}%>
Error codeDescription

${error.value}

${error.desc}

<%}%><%if(isNotEmpty(dictList)){%>

${dictListOrder}. ${dictListTitle}

<%for(dict in dictList){%>

${dictListOrder}.${dict.order}. ${dict.title}

<%for(dataDict in dict.dataDictList){%><%}%>
CodeTypeDescription

${dataDict.value}

${dataDict.type}

${dataDict.desc}

<%}%>
<%}%>
Generated by smart-doc at ${createTime}Suggestions,contact,support and error reporting on Gitee or Github