change error dict
This commit is contained in:
parent
3f9ace5ad2
commit
246db12a72
|
@ -33,7 +33,7 @@ public class AdocDocBuilder {
|
||||||
builderTemplate.buildAllInOne(apiDocList, config, ALL_IN_ONE_ADOC_TPL, INDEX_DOC);
|
builderTemplate.buildAllInOne(apiDocList, config, ALL_IN_ONE_ADOC_TPL, INDEX_DOC);
|
||||||
} else {
|
} else {
|
||||||
builderTemplate.buildApiDoc(apiDocList, config, API_DOC_ADOC_TPL, API_EXTENSION);
|
builderTemplate.buildApiDoc(apiDocList, config, API_DOC_ADOC_TPL, API_EXTENSION);
|
||||||
builderTemplate.buildErrorCodeDoc(config.getErrorCodes(), config, ERROR_CODE_LIST_ADOC_TPL, ERROR_CODE_LIST_ADOC);
|
builderTemplate.buildErrorCodeDoc(config, ERROR_CODE_LIST_ADOC_TPL, ERROR_CODE_LIST_ADOC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ public class ApiDocBuilder {
|
||||||
builderTemplate.buildAllInOne(apiDocList, config, ALL_IN_ONE_MD_TPL, "AllInOne" + version + ".md");
|
builderTemplate.buildAllInOne(apiDocList, config, ALL_IN_ONE_MD_TPL, "AllInOne" + version + ".md");
|
||||||
} else {
|
} else {
|
||||||
builderTemplate.buildApiDoc(apiDocList, config, API_DOC_MD_TPL, API_EXTENSION);
|
builderTemplate.buildApiDoc(apiDocList, config, API_DOC_MD_TPL, API_EXTENSION);
|
||||||
builderTemplate.buildErrorCodeDoc(config.getErrorCodes(), config, ERROR_CODE_LIST_MD_TPL, ERROR_CODE_LIST_MD);
|
builderTemplate.buildErrorCodeDoc(config, ERROR_CODE_LIST_MD_TPL, ERROR_CODE_LIST_MD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -133,18 +133,19 @@ public class DocBuilderTemplate {
|
||||||
/**
|
/**
|
||||||
* build error_code adoc
|
* build error_code adoc
|
||||||
*
|
*
|
||||||
* @param errorCodeList list data of Api doc
|
|
||||||
* @param config api config
|
* @param config api config
|
||||||
* @param template template
|
* @param template template
|
||||||
* @param outPutFileName output file
|
* @param outPutFileName output file
|
||||||
*/
|
*/
|
||||||
public void buildErrorCodeDoc(List<ApiErrorCode> errorCodeList, ApiConfig config, String template, String outPutFileName) {
|
public void buildErrorCodeDoc(ApiConfig config, String template, String outPutFileName) {
|
||||||
if (CollectionUtil.isNotEmpty(errorCodeList)) {
|
List<ApiErrorCode> errorCodeList = config.getErrorCodes();
|
||||||
|
if (CollectionUtil.isEmpty(errorCodeList)) {
|
||||||
|
errorCodeList = errorCodeDictToList(config);
|
||||||
|
}
|
||||||
Template mapper = BeetlTemplateUtil.getByName(template);
|
Template mapper = BeetlTemplateUtil.getByName(template);
|
||||||
mapper.binding(TemplateVariable.LIST.getVariable(), errorCodeList);
|
mapper.binding(TemplateVariable.LIST.getVariable(), errorCodeList);
|
||||||
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + FILE_SEPARATOR + outPutFileName);
|
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + FILE_SEPARATOR + outPutFileName);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a single controller api document
|
* Generate a single controller api document
|
||||||
|
@ -221,4 +222,43 @@ public class DocBuilderTemplate {
|
||||||
}
|
}
|
||||||
return apiDocDictList;
|
return apiDocDictList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<ApiErrorCode> errorCodeDictToList(ApiConfig config) {
|
||||||
|
List<ApiErrorCodeDictionary> errorCodeDictionaries = config.getErrorCodeDictionaries();
|
||||||
|
if (CollectionUtil.isEmpty(errorCodeDictionaries)) {
|
||||||
|
return new ArrayList<>(0);
|
||||||
|
} else {
|
||||||
|
List<ApiErrorCode> errorCodeList = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
for (ApiErrorCodeDictionary dictionary : errorCodeDictionaries) {
|
||||||
|
Class<?> clzz = dictionary.getEnumClass();
|
||||||
|
if (Objects.isNull(clzz)) {
|
||||||
|
if (StringUtil.isEmpty(dictionary.getEnumClassName())) {
|
||||||
|
throw new RuntimeException(" enum class name can't be null.");
|
||||||
|
}
|
||||||
|
clzz = Class.forName(dictionary.getEnumClassName());
|
||||||
|
}
|
||||||
|
if (!clzz.isEnum()) {
|
||||||
|
throw new RuntimeException(clzz.getCanonicalName() + " is not an enum class.");
|
||||||
|
}
|
||||||
|
Object[] objects = clzz.getEnumConstants();
|
||||||
|
String valueMethodName = "get" + StringUtil.firstToUpperCase(dictionary.getCodeField());
|
||||||
|
String descMethodName = "get" + StringUtil.firstToUpperCase(dictionary.getDescField());
|
||||||
|
Method valueMethod = clzz.getMethod(valueMethodName);
|
||||||
|
Method descMethod = clzz.getMethod(descMethodName);
|
||||||
|
for (Object object : objects) {
|
||||||
|
Object val = valueMethod.invoke(object);
|
||||||
|
Object desc = descMethod.invoke(object);
|
||||||
|
ApiErrorCode errorCode = new ApiErrorCode();
|
||||||
|
errorCode.setDesc(String.valueOf(desc));
|
||||||
|
errorCode.setValue(String.valueOf(val));
|
||||||
|
errorCodeList.add(errorCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return errorCodeList;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ public class PostmanJsonBuilder {
|
||||||
*
|
*
|
||||||
* @param config 配置文件
|
* @param config 配置文件
|
||||||
*/
|
*/
|
||||||
public static void BuildPostmanApi(ApiConfig config) {
|
public static void buildPostmanApi(ApiConfig config) {
|
||||||
DocBuilderTemplate builderTemplate = new DocBuilderTemplate();
|
DocBuilderTemplate builderTemplate = new DocBuilderTemplate();
|
||||||
builderTemplate.checkAndInit(config);
|
builderTemplate.checkAndInit(config);
|
||||||
SourceBuilder sourceBuilder = new SourceBuilder(config);
|
SourceBuilder sourceBuilder = new SourceBuilder(config);
|
||||||
|
|
|
@ -97,6 +97,12 @@ public class ApiConfig {
|
||||||
*/
|
*/
|
||||||
private List<ApiDataDictionary> dataDictionaries;
|
private List<ApiDataDictionary> dataDictionaries;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 1.7.9
|
||||||
|
* api error code dictionary
|
||||||
|
*/
|
||||||
|
private List<ApiErrorCodeDictionary> errorCodeDictionaries;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @since 1.7.5
|
* @since 1.7.5
|
||||||
* project name
|
* project name
|
||||||
|
@ -225,6 +231,14 @@ public class ApiConfig {
|
||||||
this.dataDictionaries = CollectionUtil.asList(dataDictConfigs);
|
this.dataDictionaries = CollectionUtil.asList(dataDictConfigs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<ApiErrorCodeDictionary> getErrorCodeDictionaries() {
|
||||||
|
return errorCodeDictionaries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorCodeDictionaries(ApiErrorCodeDictionary... errorCodeDictConfigs) {
|
||||||
|
this.errorCodeDictionaries = CollectionUtil.asList(errorCodeDictConfigs);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isCoverOld() {
|
public boolean isCoverOld() {
|
||||||
return coverOld;
|
return coverOld;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.power.doc.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yu 2019/12/7.
|
||||||
|
*/
|
||||||
|
public class ApiErrorCodeDictionary {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* enumClass
|
||||||
|
*/
|
||||||
|
private Class enumClass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* enum class name
|
||||||
|
*/
|
||||||
|
private String enumClassName;
|
||||||
|
/**
|
||||||
|
* code field
|
||||||
|
*/
|
||||||
|
private String codeField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* description field
|
||||||
|
*/
|
||||||
|
private String descField;
|
||||||
|
|
||||||
|
public Class getEnumClass() {
|
||||||
|
return enumClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApiErrorCodeDictionary setEnumClass(Class enumClass) {
|
||||||
|
this.enumClass = enumClass;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCodeField() {
|
||||||
|
return codeField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApiErrorCodeDictionary setCodeField(String codeField) {
|
||||||
|
this.codeField = codeField;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescField() {
|
||||||
|
return descField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApiErrorCodeDictionary setDescField(String descField) {
|
||||||
|
this.descField = descField;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEnumClassName() {
|
||||||
|
return enumClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApiErrorCodeDictionary setEnumClassName(String enumClassName) {
|
||||||
|
this.enumClassName = enumClassName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
|
@ -58,6 +58,7 @@ public class ApiDocTest {
|
||||||
CustomRespField.field().setName("code").setValue("00000")
|
CustomRespField.field().setName("code").setValue("00000")
|
||||||
//.setDesc("响应代码")
|
//.setDesc("响应代码")
|
||||||
);*/
|
);*/
|
||||||
|
config.setErrorCodes();
|
||||||
|
|
||||||
//非必须只有当setAllInOne设置为true时文档变更记录才生效,https://gitee.com/sunyurepository/ApplicationPower/issues/IPS4O
|
//非必须只有当setAllInOne设置为true时文档变更记录才生效,https://gitee.com/sunyurepository/ApplicationPower/issues/IPS4O
|
||||||
config.setRevisionLogs(
|
config.setRevisionLogs(
|
||||||
|
|
Loading…
Reference in New Issue