Merge remote-tracking branch 'origin/dev'

This commit is contained in:
oppofind 2021-04-25 20:53:44 +08:00
commit 50b0667f7b
8 changed files with 102 additions and 27 deletions

View File

@ -1,5 +1,15 @@
## smart-doc版本 ## smart-doc版本
版本小于1.0都属于试用正式1.0起始发布将会等到文中提到的问题解决后才发布。 版本小于1.0都属于试用正式1.0起始发布将会等到文中提到的问题解决后才发布。
#### 版本号2.1.4
- 更新日期: 2020-04-24
- 更新内容:
1. 修复Controller继承时父类的Mapping未继承的问题。
2. 修复配置responseBodyAdvice后controller中void方法返回显示错误。
3. 修复往torna推送漏掉pathParams的问题。
4. 修复非json请求集合中绑定枚举强制检查错误的问题。
5. 新增requestBodyAdvice支持可以实现请求参数包装。
6. 修复泛型为List数据时类型为object问题。
7. 修复customFiled为继承参数时配置失效问题。
#### 版本号2.1.3 #### 版本号2.1.3
- 更新日期: 2020-04-11 - 更新日期: 2020-04-11
- 更新内容: - 更新内容:

View File

@ -77,14 +77,18 @@ public class HtmlApiDocBuilder {
Template indexCssTemplate = BeetlTemplateUtil.getByName(ALL_IN_ONE_CSS); Template indexCssTemplate = BeetlTemplateUtil.getByName(ALL_IN_ONE_CSS);
FileUtil.nioWriteFile(indexCssTemplate.render(), config.getOutPath() + FILE_SEPARATOR + ALL_IN_ONE_CSS); FileUtil.nioWriteFile(indexCssTemplate.render(), config.getOutPath() + FILE_SEPARATOR + ALL_IN_ONE_CSS);
if (config.isAllInOne()) { if (config.isAllInOne()) {
if (StringUtils.isNotEmpty(config.getAllInOneDocFileName())) {
INDEX_HTML = config.getAllInOneDocFileName();
}
if (config.isCreateDebugPage()) { if (config.isCreateDebugPage()) {
builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, DEBUG_PAGE_ALL_TPL, DEBUG_PAGE_ALL_TPL); INDEX_HTML = DEBUG_PAGE_ALL_TPL;
if (StringUtils.isNotEmpty(config.getAllInOneDocFileName())) {
INDEX_HTML = config.getAllInOneDocFileName();
}
builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, DEBUG_PAGE_ALL_TPL, INDEX_HTML);
Template mockJs = BeetlTemplateUtil.getByName(DEBUG_JS_TPL); Template mockJs = BeetlTemplateUtil.getByName(DEBUG_JS_TPL);
FileUtil.nioWriteFile(mockJs.render(), config.getOutPath() + FILE_SEPARATOR + DEBUG_JS_OUT); FileUtil.nioWriteFile(mockJs.render(), config.getOutPath() + FILE_SEPARATOR + DEBUG_JS_OUT);
} else { } else {
if (StringUtils.isNotEmpty(config.getAllInOneDocFileName())) {
INDEX_HTML = config.getAllInOneDocFileName();
}
builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, ALL_IN_ONE_HTML_TPL, INDEX_HTML); builderTemplate.buildAllInOne(apiDocList, config, javaProjectBuilder, ALL_IN_ONE_HTML_TPL, INDEX_HTML);
} }
builderTemplate.buildSearchJs(config, javaProjectBuilder, apiDocList, SEARCH_ALL_JS_TPL); builderTemplate.buildSearchJs(config, javaProjectBuilder, apiDocList, SEARCH_ALL_JS_TPL);

View File

@ -90,7 +90,8 @@ public class ProjectDocConfigBuilder {
this.initCustomRequestFieldsMap(apiConfig); this.initCustomRequestFieldsMap(apiConfig);
this.initReplaceClassMap(apiConfig); this.initReplaceClassMap(apiConfig);
this.initConstants(apiConfig); this.initConstants(apiConfig);
this.checkResponseBodyAdvice(apiConfig); this.checkBodyAdvice(apiConfig.getRequestBodyAdvice());
this.checkBodyAdvice(apiConfig.getResponseBodyAdvice());
} }
public JavaClass getClassByName(String simpleName) { public JavaClass getClassByName(String simpleName) {
@ -179,20 +180,18 @@ public class ProjectDocConfigBuilder {
} }
} }
private void checkResponseBodyAdvice(ApiConfig config) { private void checkBodyAdvice(BodyAdvice bodyAdvice) {
ResponseBodyAdvice responseBodyAdvice = config.getResponseBodyAdvice(); if (Objects.nonNull(bodyAdvice) && StringUtil.isNotEmpty(bodyAdvice.getClassName())) {
if (Objects.nonNull(responseBodyAdvice) && StringUtil.isNotEmpty(responseBodyAdvice.getClassName())) { if (Objects.nonNull(bodyAdvice.getWrapperClass())) {
if (Objects.nonNull(responseBodyAdvice.getWrapperClass())) {
return; return;
} }
try { try {
Class.forName(responseBodyAdvice.getClassName()); Class.forName(bodyAdvice.getClassName());
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new RuntimeException("Can't find class " + responseBodyAdvice.getClassName() + " for ResponseBodyAdvice."); throw new RuntimeException("Can't find class " + bodyAdvice.getClassName() + " for ResponseBodyAdvice.");
} }
} }
} }
/** /**
* 设置高亮样式 * 设置高亮样式
*/ */

View File

@ -112,4 +112,6 @@ public interface DocTags {
* Ignore ResponseBodyAdvice * Ignore ResponseBodyAdvice
*/ */
String IGNORE_RESPONSE_BODY_ADVICE = "ignoreResponseBodyAdvice"; String IGNORE_RESPONSE_BODY_ADVICE = "ignoreResponseBodyAdvice";
String IGNORE_REQUEST_BODY_ADVICE = "ignoreRequestBodyAdvice";
} }

View File

@ -268,7 +268,9 @@ public class ApiConfig {
* Support Spring MVC ResponseBodyAdvice * Support Spring MVC ResponseBodyAdvice
* @since 1.9.8 * @since 1.9.8
*/ */
private ResponseBodyAdvice responseBodyAdvice; private BodyAdvice responseBodyAdvice;
private BodyAdvice requestBodyAdvice;
private String style; private String style;
@ -718,14 +720,22 @@ public class ApiConfig {
this.displayActualType = displayActualType; this.displayActualType = displayActualType;
} }
public ResponseBodyAdvice getResponseBodyAdvice() { public BodyAdvice getResponseBodyAdvice() {
return responseBodyAdvice; return responseBodyAdvice;
} }
public void setResponseBodyAdvice(ResponseBodyAdvice responseBodyAdvice) { public void setResponseBodyAdvice(BodyAdvice responseBodyAdvice) {
this.responseBodyAdvice = responseBodyAdvice; this.responseBodyAdvice = responseBodyAdvice;
} }
public BodyAdvice getRequestBodyAdvice() {
return requestBodyAdvice;
}
public void setRequestBodyAdvice(BodyAdvice requestBodyAdvice) {
this.requestBodyAdvice = requestBodyAdvice;
}
public String getStyle() { public String getStyle() {
return style; return style;
} }

View File

@ -26,7 +26,7 @@ package com.power.doc.model;
* @since 1.9.8 * @since 1.9.8
* @author yu 2020/11/5. * @author yu 2020/11/5.
*/ */
public class ResponseBodyAdvice { public class BodyAdvice {
private String className; private String className;
@ -34,15 +34,15 @@ public class ResponseBodyAdvice {
private String dataField; private String dataField;
public static ResponseBodyAdvice builder(){ public static BodyAdvice builder(){
return new ResponseBodyAdvice(); return new BodyAdvice();
} }
public String getClassName() { public String getClassName() {
return className; return className;
} }
public ResponseBodyAdvice setClassName(String className) { public BodyAdvice setClassName(String className) {
this.className = className; this.className = className;
return this; return this;
} }
@ -51,7 +51,7 @@ public class ResponseBodyAdvice {
return dataField; return dataField;
} }
public ResponseBodyAdvice setDataField(String dataField) { public BodyAdvice setDataField(String dataField) {
this.dataField = dataField; this.dataField = dataField;
return this; return this;
} }
@ -60,7 +60,7 @@ public class ResponseBodyAdvice {
return wrapperClass; return wrapperClass;
} }
public ResponseBodyAdvice setWrapperClass(Class wrapperClass) { public BodyAdvice setWrapperClass(Class wrapperClass) {
this.wrapperClass = wrapperClass; this.wrapperClass = wrapperClass;
return this; return this;
} }

View File

@ -96,7 +96,7 @@ public interface IDocBuildTemplate<T> {
default List<ApiParam> buildReturnApiParams(DocJavaMethod docJavaMethod, ProjectDocConfigBuilder projectBuilder) { default List<ApiParam> buildReturnApiParams(DocJavaMethod docJavaMethod, ProjectDocConfigBuilder projectBuilder) {
JavaMethod method = docJavaMethod.getJavaMethod(); JavaMethod method = docJavaMethod.getJavaMethod();
if (method.getReturns().isVoid()) { if (method.getReturns().isVoid() && Objects.isNull(projectBuilder.getApiConfig().getResponseBodyAdvice())) {
return new ArrayList<>(0); return new ArrayList<>(0);
} }
String returnTypeGenericCanonicalName = method.getReturnType().getGenericCanonicalName(); String returnTypeGenericCanonicalName = method.getReturnType().getGenericCanonicalName();

View File

@ -46,7 +46,7 @@ import java.util.stream.Stream;
import static com.power.doc.constants.DocGlobalConstants.FILE_CONTENT_TYPE; import static com.power.doc.constants.DocGlobalConstants.FILE_CONTENT_TYPE;
import static com.power.doc.constants.DocGlobalConstants.JSON_CONTENT_TYPE; import static com.power.doc.constants.DocGlobalConstants.JSON_CONTENT_TYPE;
import static com.power.doc.constants.DocTags.IGNORE; import static com.power.doc.constants.DocTags.*;
/** /**
* @author yu 2019/12/21. * @author yu 2019/12/21.
@ -120,7 +120,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
boolean paramsDataToTree = projectBuilder.getApiConfig().isParamsDataToTree(); boolean paramsDataToTree = projectBuilder.getApiConfig().isParamsDataToTree();
String group = JavaClassUtil.getClassTagsValue(cls, DocTags.GROUP, Boolean.TRUE); String group = JavaClassUtil.getClassTagsValue(cls, DocTags.GROUP, Boolean.TRUE);
String classAuthor = JavaClassUtil.getClassTagsValue(cls, DocTags.AUTHOR, Boolean.TRUE); String classAuthor = JavaClassUtil.getClassTagsValue(cls, DocTags.AUTHOR, Boolean.TRUE);
List<JavaAnnotation> classAnnotations = cls.getAnnotations(); List<JavaAnnotation> classAnnotations = this.getAnnotations(cls);
Map<String, String> constantsMap = projectBuilder.getConstantsMap(); Map<String, String> constantsMap = projectBuilder.getConstantsMap();
String baseUrl = ""; String baseUrl = "";
for (JavaAnnotation annotation : classAnnotations) { for (JavaAnnotation annotation : classAnnotations) {
@ -365,6 +365,19 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
} }
if (SpringMvcAnnotations.REQUEST_BODY.equals(annotationName) || DocGlobalConstants.REQUEST_BODY_FULLY.equals(annotationName)) { if (SpringMvcAnnotations.REQUEST_BODY.equals(annotationName) || DocGlobalConstants.REQUEST_BODY_FULLY.equals(annotationName)) {
apiMethodDoc.setContentType(JSON_CONTENT_TYPE); apiMethodDoc.setContentType(JSON_CONTENT_TYPE);
if (Objects.nonNull(configBuilder.getApiConfig().getRequestBodyAdvice())
&& Objects.isNull(method.getTagByName(IGNORE_REQUEST_BODY_ADVICE))) {
String requestBodyAdvice = configBuilder.getApiConfig().getRequestBodyAdvice().getClassName();
gicTypeName = new StringBuffer()
.append(requestBodyAdvice)
.append("<")
.append(gicTypeName).append(">").toString();
typeName = new StringBuffer()
.append(requestBodyAdvice)
.append("<")
.append(typeName).append(">").toString();
}
if (JavaClassValidateUtil.isPrimitive(simpleTypeName)) { if (JavaClassValidateUtil.isPrimitive(simpleTypeName)) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("{\"") builder.append("{\"")
@ -411,7 +424,8 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
if (JavaClassValidateUtil.isArray(gicName)) { if (JavaClassValidateUtil.isArray(gicName)) {
gicName = gicName.substring(0, gicName.indexOf("[")); gicName = gicName.substring(0, gicName.indexOf("["));
} }
if (!JavaClassValidateUtil.isPrimitive(gicName)) { if (!JavaClassValidateUtil.isPrimitive(gicName)
&&!configBuilder.getJavaProjectBuilder().getClassByName(gicName).isEnum()) {
throw new RuntimeException("Spring MVC can't support binding Collection on method " throw new RuntimeException("Spring MVC can't support binding Collection on method "
+ method.getName() + "Check it in " + method.getDeclaringClass().getCanonicalName()); + method.getName() + "Check it in " + method.getDeclaringClass().getCanonicalName());
} }
@ -667,14 +681,29 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
if (JavaClassValidateUtil.isArray(gicName)) { if (JavaClassValidateUtil.isArray(gicName)) {
gicName = gicName.substring(0, gicName.indexOf("[")); gicName = gicName.substring(0, gicName.indexOf("["));
} }
if (JavaClassValidateUtil.isPrimitive(gicName)) { JavaClass gicJavaClass = builder.getJavaProjectBuilder().getClassByName(gicName);
if(gicJavaClass.isEnum()){
Object value = JavaClassUtil.getEnumValue(gicJavaClass, Boolean.TRUE);
ApiParam param = ApiParam.of().setField(paramName).setDesc(comment + ",[array of enum]")
.setRequired(required)
.setPathParam(isPathVariable)
.setQueryParam(queryParam)
.setId(paramList.size() + 1)
.setType("array").setValue(String.valueOf(value));
paramList.add(param);
if (requestBodyCounter > 0) {
Map<String, Object> map = OpenApiSchemaUtil.arrayTypeSchema(gicName);
docJavaMethod.setRequestSchema(map);
}
} else if (JavaClassValidateUtil.isPrimitive(gicName)) {
String shortSimple = DocClassUtil.processTypeNameForParams(gicName); String shortSimple = DocClassUtil.processTypeNameForParams(gicName);
ApiParam param = ApiParam.of().setField(paramName).setDesc(comment + ",[array of " + shortSimple + "]") ApiParam param = ApiParam.of().setField(paramName).setDesc(comment + ",[array of " + shortSimple + "]")
.setRequired(required) .setRequired(required)
.setPathParam(isPathVariable) .setPathParam(isPathVariable)
.setQueryParam(queryParam) .setQueryParam(queryParam)
.setId(paramList.size() + 1) .setId(paramList.size() + 1)
.setType("array"); .setType("array")
.setValue(DocUtil.getValByTypeAndFieldName(gicName,paramName));
paramList.add(param); paramList.add(param);
if (requestBodyCounter > 0) { if (requestBodyCounter > 0) {
Map<String, Object> map = OpenApiSchemaUtil.arrayTypeSchema(gicName); Map<String, Object> map = OpenApiSchemaUtil.arrayTypeSchema(gicName);
@ -878,4 +907,25 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
paramList.add(apiParam); paramList.add(apiParam);
mappingParams.put(paramName, null); mappingParams.put(paramName, null);
} }
private List<JavaAnnotation> getAnnotations(JavaClass cls) {
List<JavaAnnotation> annotationsList = cls.getAnnotations();
boolean flag = annotationsList.stream().anyMatch(item -> {
String annotationName = item.getType().getValue();
if (DocAnnotationConstants.REQUEST_MAPPING.equals(annotationName) ||
DocGlobalConstants.REQUEST_MAPPING_FULLY.equals(annotationName)) {
return true;
}
return false;
});
// child override parent set
if (flag) {
return annotationsList;
}
JavaClass superJavaClass = cls.getSuperJavaClass();
if (!"Object".equals(superJavaClass.getSimpleName())) {
annotationsList.addAll(getAnnotations(superJavaClass));
}
return annotationsList;
}
} }