fix error of while set allInOne option is false.

This commit is contained in:
oppofind 2020-08-26 22:09:12 +08:00
parent adc5399b19
commit 0ce056087b
4 changed files with 102 additions and 94 deletions

View File

@ -48,7 +48,7 @@ public class AdocDocBuilder {
* *
* @param config ApiConfig * @param config ApiConfig
*/ */
public static void builderApiDoc(ApiConfig config) { public static void buildApiDoc(ApiConfig config) {
JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder(); JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();
buildApiDoc(config, javaProjectBuilder); buildApiDoc(config, javaProjectBuilder);
} }

View File

@ -80,6 +80,8 @@ public class DocBuilderTemplate extends BaseDocBuilderTemplate {
mapper.binding(TemplateVariable.DESC.getVariable(), doc.getDesc()); mapper.binding(TemplateVariable.DESC.getVariable(), doc.getDesc());
mapper.binding(TemplateVariable.NAME.getVariable(), doc.getName()); mapper.binding(TemplateVariable.NAME.getVariable(), doc.getName());
mapper.binding(TemplateVariable.LIST.getVariable(), doc.getList()); mapper.binding(TemplateVariable.LIST.getVariable(), doc.getList());
mapper.binding(TemplateVariable.REQUEST_EXAMPLE.getVariable(), config.isRequestExample());
mapper.binding(TemplateVariable.RESPONSE_EXAMPLE.getVariable(), config.isResponseExample());
FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + FILE_SEPARATOR + doc.getName() + fileExtension); FileUtil.nioWriteFile(mapper.render(), config.getOutPath() + FILE_SEPARATOR + doc.getName() + fileExtension);
} }
} }

View File

@ -93,7 +93,7 @@ public class HtmlApiDocBuilder {
List<ApiDocDict> apiDocDictList = builderTemplate.buildDictionary(config, javaProjectBuilder); List<ApiDocDict> apiDocDictList = builderTemplate.buildDictionary(config, javaProjectBuilder);
buildIndex(apiDocList, config); buildIndex(apiDocList, config);
copyCss(config.getOutPath()); copyCss(config.getOutPath());
buildDoc(apiDocList, config.getOutPath()); buildDoc(apiDocList, config);
buildErrorCodeDoc(config.getErrorCodes(), config.getOutPath()); buildErrorCodeDoc(config.getErrorCodes(), config.getOutPath());
buildDictionary(apiDocDictList, config.getOutPath()); buildDictionary(apiDocDictList, config.getOutPath());
} }
@ -149,20 +149,22 @@ public class HtmlApiDocBuilder {
* build ever controller api * build ever controller api
* *
* @param apiDocList list of api doc * @param apiDocList list of api doc
* @param outPath output path * @param config ApiConfig
*/ */
private static void buildDoc(List<ApiDoc> apiDocList, String outPath) { private static void buildDoc(List<ApiDoc> apiDocList, ApiConfig config) {
FileUtil.mkdirs(outPath); FileUtil.mkdirs(config.getOutPath());
Template htmlApiDoc; Template htmlApiDoc;
for (ApiDoc doc : apiDocList) { for (ApiDoc doc : apiDocList) {
Template apiTemplate = BeetlTemplateUtil.getByName(API_DOC_MD_TPL); Template apiTemplate = BeetlTemplateUtil.getByName(API_DOC_MD_TPL);
apiTemplate.binding(TemplateVariable.REQUEST_EXAMPLE.getVariable(), config.isRequestExample());
apiTemplate.binding(TemplateVariable.RESPONSE_EXAMPLE.getVariable(), config.isResponseExample());
apiTemplate.binding(TemplateVariable.DESC.getVariable(), doc.getDesc()); apiTemplate.binding(TemplateVariable.DESC.getVariable(), doc.getDesc());
apiTemplate.binding(TemplateVariable.NAME.getVariable(), doc.getName()); apiTemplate.binding(TemplateVariable.NAME.getVariable(), doc.getName());
apiTemplate.binding(TemplateVariable.LIST.getVariable(), doc.getList());//类名 apiTemplate.binding(TemplateVariable.LIST.getVariable(), doc.getList());//类名
Map<String, Object> templateVariables = new HashMap<>(); Map<String, Object> templateVariables = new HashMap<>();
templateVariables.put(TemplateVariable.TITLE.getVariable(), doc.getDesc()); templateVariables.put(TemplateVariable.TITLE.getVariable(), doc.getDesc());
htmlApiDoc = initTemplate(apiTemplate, HTML_API_DOC_TPL, templateVariables); htmlApiDoc = initTemplate(apiTemplate, HTML_API_DOC_TPL, templateVariables);
FileUtil.nioWriteFile(htmlApiDoc.render(), outPath + FILE_SEPARATOR + doc.getAlias() + ".html"); FileUtil.nioWriteFile(htmlApiDoc.render(), config.getOutPath() + FILE_SEPARATOR + doc.getAlias() + ".html");
} }
} }

View File

@ -29,7 +29,9 @@ import com.power.common.util.FileUtil;
import com.power.common.util.StringUtil; import com.power.common.util.StringUtil;
import com.power.doc.constants.DocGlobalConstants; import com.power.doc.constants.DocGlobalConstants;
import com.power.doc.constants.TemplateVariable; import com.power.doc.constants.TemplateVariable;
import com.power.doc.model.*; import com.power.doc.model.ApiConfig;
import com.power.doc.model.ApiDoc;
import com.power.doc.model.ApiParam;
import com.power.doc.template.IDocBuildTemplate; import com.power.doc.template.IDocBuildTemplate;
import com.power.doc.template.SpringBootDocBuildTemplate; import com.power.doc.template.SpringBootDocBuildTemplate;
import com.power.doc.utils.BeetlTemplateUtil; import com.power.doc.utils.BeetlTemplateUtil;
@ -46,6 +48,7 @@ import static com.power.doc.constants.DocGlobalConstants.YAPI_RESULT_TPL;
/** /**
* generate yapi's yapi json * generate yapi's yapi json
*
* @author dai19470 2020/08/20. * @author dai19470 2020/08/20.
*/ */
public class YapiJsonBuilder { public class YapiJsonBuilder {
@ -57,7 +60,6 @@ public class YapiJsonBuilder {
* @param config 配置文件 * @param config 配置文件
*/ */
public static void buildYapiCollection(ApiConfig config) { public static void buildYapiCollection(ApiConfig config) {
DocBuilderTemplate builderTemplate = new DocBuilderTemplate(); DocBuilderTemplate builderTemplate = new DocBuilderTemplate();
builderTemplate.checkAndInit(config); builderTemplate.checkAndInit(config);
JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder(); JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();
@ -66,110 +68,110 @@ public class YapiJsonBuilder {
} }
private static Set<String> getUrl(String url,String patter){ private static Set<String> getUrl(String url, String patter) {
Pattern pattern = Pattern.compile(patter); Pattern pattern = Pattern.compile(patter);
Matcher matcher = pattern.matcher(url); Matcher matcher = pattern.matcher(url);
Set<String> result = new HashSet<>(); Set<String> result = new HashSet<>();
while(matcher.find()){ while (matcher.find()) {
result.add(matcher.group()); result.add(matcher.group());
} }
return result; return result;
} }
private static void yapiJsonCreate(ApiConfig config, ProjectDocConfigBuilder configBuilder) { private static void yapiJsonCreate(ApiConfig config, ProjectDocConfigBuilder configBuilder) {
config.setParamsDataToTree(true);
IDocBuildTemplate docBuildTemplate = new SpringBootDocBuildTemplate(); IDocBuildTemplate docBuildTemplate = new SpringBootDocBuildTemplate();
List<ApiDoc> apiDocList = docBuildTemplate.getApiData(configBuilder); List<ApiDoc> apiDocList = docBuildTemplate.getApiData(configBuilder);
List<Map<String,Object>> requestItem = new ArrayList<>(); List<Map<String, Object>> requestItem = new ArrayList<>();
Iterables.forEach(apiDocList, (index, apiDoc) -> { Iterables.forEach(apiDocList, (index, apiDoc) -> {
Map<String,Object> module = new HashMap<>(); Map<String, Object> module = new HashMap<>();
module.put("index",index); module.put("index", index);
module.put("name",apiDoc.getDesc()); module.put("name", apiDoc.getDesc());
module.put("parent_id",-1); module.put("parent_id", -1);
module.put("desc",apiDoc.getDesc()); module.put("desc", apiDoc.getDesc());
module.put("add_time",System.currentTimeMillis() / 1000); module.put("add_time", System.currentTimeMillis() / 1000);
module.put("up_time",System.currentTimeMillis() / 1000); module.put("up_time", System.currentTimeMillis() / 1000);
List<Map<String,Object>> methods = new ArrayList(); List<Map<String, Object>> methods = new ArrayList();
Iterables.forEach(apiDoc.getList(), (idx, apiMethodDoc) -> { Iterables.forEach(apiDoc.getList(), (idx, apiMethodDoc) -> {
Map<String,Object> method = new HashMap<>(); Map<String, Object> method = new HashMap<>();
Map<String,Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("path",apiMethodDoc.getPath()); map.put("path", apiMethodDoc.getPath());
map.put("params",new Object[]{}); map.put("params", new Object[]{});
method.put("query_path",map); method.put("query_path", map);
// method.put("owners",new String[]{apiMethodDoc.getAuthor()}); // method.put("owners",new String[]{apiMethodDoc.getAuthor()});
method.put("owners",new String[]{}); method.put("owners", new String[]{});
method.put("edit_uid",0); method.put("edit_uid", 0);
method.put("status","done"); method.put("status", "done");
method.put("type","static"); method.put("type", "static");
method.put("req_body_is_json_schema",true); method.put("req_body_is_json_schema", true);
method.put("res_body_is_json_schema",true); method.put("res_body_is_json_schema", true);
method.put("api_opened",false); method.put("api_opened", false);
method.put("index",idx); method.put("index", idx);
method.put("tag",new Object[]{}); method.put("tag", new Object[]{});
method.put("method",apiMethodDoc.getType()); method.put("method", apiMethodDoc.getType());
method.put("title",apiMethodDoc.getDesc()); method.put("title", apiMethodDoc.getDesc());
method.put("desc",apiMethodDoc.getDetail()); method.put("desc", apiMethodDoc.getDetail());
method.put("name",apiMethodDoc.getName()); method.put("name", apiMethodDoc.getName());
method.put("path",apiMethodDoc.getPath().replace("//","/")); method.put("path", apiMethodDoc.getPath().replace("//", "/"));
method.put("req_body_form", Arrays.asList()); method.put("req_body_form", Arrays.asList());
List<Map<String,Object>> req_params = new ArrayList(); List<Map<String, Object>> req_params = new ArrayList();
Set<String> req_param = getUrl(apiMethodDoc.getPath(), "(?<=\\{)(.+?)(?=\\})"); Set<String> req_param = getUrl(apiMethodDoc.getPath(), "(?<=\\{)(.+?)(?=\\})");
Iterables.forEach(req_param,(j,param)->{ Iterables.forEach(req_param, (j, param) -> {
ApiParam temp = apiMethodDoc.getRequestParams().stream().filter(apiParam -> apiParam.getField().equals(param)).findFirst().orElse(null); ApiParam temp = apiMethodDoc.getRequestParams().stream().filter(apiParam -> apiParam.getField().equals(param)).findFirst().orElse(null);
if(temp!=null){ if (temp != null) {
Map<String,Object> h = new HashMap<>(); Map<String, Object> h = new HashMap<>();
h.put("example",""); h.put("example", "");
h.put("name",temp.getField()); h.put("name", temp.getField());
h.put("type",temp.getType()); h.put("type", temp.getType());
h.put("desc",temp.getDesc()); h.put("desc", temp.getDesc());
req_params.add(j,h); req_params.add(j, h);
} }
}); });
method.put("req_params",req_params); method.put("req_params", req_params);
method.put("res_body_type","json"); method.put("res_body_type", "json");
List<Map<String,Object>> querys = new ArrayList(); List<Map<String, Object>> querys = new ArrayList();
Iterables.forEach(apiMethodDoc.getRequestParams(),(j,res)->{ Iterables.forEach(apiMethodDoc.getRequestParams(), (j, res) -> {
Map<String,Object> h = new HashMap<>(); Map<String, Object> h = new HashMap<>();
h.put("required",res.isRequired()?"1":"0"); h.put("required", res.isRequired() ? "1" : "0");
h.put("desc",res.getDesc()); h.put("desc", res.getDesc());
h.put("name",res.getField()); h.put("name", res.getField());
h.put("example",""); h.put("example", "");
h.put("type",res.getType()); h.put("type", res.getType());
querys.add(j,h); querys.add(j, h);
}); });
method.put("req_query",querys); method.put("req_query", querys);
List<Map<String,Object>> headers = new ArrayList(); List<Map<String, Object>> headers = new ArrayList();
Iterables.forEach(apiMethodDoc.getRequestHeaders(),(j,res)->{ Iterables.forEach(apiMethodDoc.getRequestHeaders(), (j, res) -> {
Map<String,Object> h = new HashMap<>(); Map<String, Object> h = new HashMap<>();
h.put("required",res.isRequired()?"1":"0"); h.put("required", res.isRequired() ? "1" : "0");
h.put("value",res.getValue()); h.put("value", res.getValue());
h.put("name",res.getName()); h.put("name", res.getName());
h.put("desc",res.getDesc()); h.put("desc", res.getDesc());
headers.add(j,h); headers.add(j, h);
}); });
method.put("req_headers",headers); method.put("req_headers", headers);
Template apiTemplate = BeetlTemplateUtil.getByName(YAPI_RESULT_TPL); Template apiTemplate = BeetlTemplateUtil.getByName(YAPI_RESULT_TPL);
apiTemplate.binding(TemplateVariable.RESPONSELIST.getVariable(), generateJson(apiMethodDoc.getResponseParams())); apiTemplate.binding(TemplateVariable.RESPONSELIST.getVariable(), generateJson(apiMethodDoc.getResponseParams()));
String json = apiTemplate.render(); String json = apiTemplate.render();
method.put("res_body",json); method.put("res_body", json);
if(StringUtil.isNotEmpty(apiMethodDoc.getResponseUsage())){ if (StringUtil.isNotEmpty(apiMethodDoc.getResponseUsage())) {
method.put("desc","<pre><code>\n"+apiMethodDoc.getResponseUsage()+"\n</code></pre>\n"); method.put("desc", "<pre><code>\n" + apiMethodDoc.getResponseUsage() + "\n</code></pre>\n");
} }
methods.add(idx,method); methods.add(idx, method);
}); });
module.put("list",methods); module.put("list", methods);
requestItem.add(module); requestItem.add(module);
}); });
@ -183,26 +185,27 @@ public class YapiJsonBuilder {
private static String generateJson(List<ApiParam> responseParams) { private static String generateJson(List<ApiParam> responseParams) {
StringBuffer re = new StringBuffer("\"type\":\"object\",\n\"properties\":{\n"); StringBuffer re = new StringBuffer("\"type\":\"object\",\n\"properties\":{\n");
HashSet<String> required=new HashSet<>(); HashSet<String> required = new HashSet<>();
responseParams.stream().forEach(apiParam -> { responseParams.stream().forEach(apiParam -> {
re.append(getTypeAndPropertiesJson(apiParam)); re.append(getTypeAndPropertiesJson(apiParam));
if(apiParam.isRequired()){ if (apiParam.isRequired()) {
required.add(apiParam.getField()); required.add(apiParam.getField());
} }
} ); });
Gson gs = new Gson(); Gson gs = new Gson();
re.append("\"required\":\""+gs.toJson(required.toArray())+"\""); re.append("\"required\":\"" + gs.toJson(required.toArray()) + "\"");
re.append("\t}"); re.append("\t}");
return re.toString(); return re.toString();
} }
/** /**
* 将字段类型转换为yapi的字段类型 * 将字段类型转换为yapi的字段类型
* @param type *
* @return * @param type java type
* @return String
*/ */
public static String changeType(String type) { public static String changeType(String type) {
switch (type){ switch (type) {
case "boolean": case "boolean":
return "boolean"; return "boolean";
case "int32": case "int32":
@ -216,25 +219,26 @@ public class YapiJsonBuilder {
/** /**
* 单个参数拼接字符串 * 单个参数拼接字符串
* @param param *
* @return * @param param ApiParam
* @return String
*/ */
public static String getTypeAndPropertiesJson(ApiParam param){ public static String getTypeAndPropertiesJson(ApiParam param) {
StringBuffer resultJson = new StringBuffer( ); StringBuffer resultJson = new StringBuffer();
resultJson.append("\""+param.getField()+"\":{"); resultJson.append("\"" + param.getField() + "\":{");
resultJson.append("\"type\":\""+changeType(param.getType())+"\", "); resultJson.append("\"type\":\"" + changeType(param.getType()) + "\", ");
if(param.getChildren()!=null&&param.getChildren().size()>0){ if (param.getChildren() != null && param.getChildren().size() > 0) {
if(param.getType().equals("object")){ if (param.getType().equals("object")) {
resultJson.append(" \"properties\":{"); resultJson.append(" \"properties\":{");
param.getChildren().forEach(child->{ param.getChildren().forEach(child -> {
resultJson.append(getTypeAndPropertiesJson(child)); resultJson.append(getTypeAndPropertiesJson(child));
}); });
}else if(param.getType().equals("array")){ } else if (param.getType().equals("array")) {
resultJson.append(" \"items\":{"); resultJson.append(" \"items\":{");
resultJson .append("\"type\":\"object\",\n\"properties\":{\n"); resultJson.append("\"type\":\"object\",\n\"properties\":{\n");
param.getChildren().forEach(child->{ param.getChildren().forEach(child -> {
resultJson.append(getTypeAndPropertiesJson(child)); resultJson.append(getTypeAndPropertiesJson(child));
}); });
resultJson.append("\t},"); resultJson.append("\t},");