From 0ce056087bd471e4e2b704148dd4e59b2fb8da2a Mon Sep 17 00:00:00 2001 From: oppofind <836575280@qq.com> Date: Wed, 26 Aug 2020 22:09:12 +0800 Subject: [PATCH] fix error of while set allInOne option is false. --- .../com/power/doc/builder/AdocDocBuilder.java | 2 +- .../power/doc/builder/DocBuilderTemplate.java | 2 + .../power/doc/builder/HtmlApiDocBuilder.java | 12 +- .../power/doc/builder/YapiJsonBuilder.java | 180 +++++++++--------- 4 files changed, 102 insertions(+), 94 deletions(-) diff --git a/src/main/java/com/power/doc/builder/AdocDocBuilder.java b/src/main/java/com/power/doc/builder/AdocDocBuilder.java index 8208b7c..2799f53 100644 --- a/src/main/java/com/power/doc/builder/AdocDocBuilder.java +++ b/src/main/java/com/power/doc/builder/AdocDocBuilder.java @@ -48,7 +48,7 @@ public class AdocDocBuilder { * * @param config ApiConfig */ - public static void builderApiDoc(ApiConfig config) { + public static void buildApiDoc(ApiConfig config) { JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder(); buildApiDoc(config, javaProjectBuilder); } diff --git a/src/main/java/com/power/doc/builder/DocBuilderTemplate.java b/src/main/java/com/power/doc/builder/DocBuilderTemplate.java index ffa9e7a..3bfcceb 100644 --- a/src/main/java/com/power/doc/builder/DocBuilderTemplate.java +++ b/src/main/java/com/power/doc/builder/DocBuilderTemplate.java @@ -80,6 +80,8 @@ public class DocBuilderTemplate extends BaseDocBuilderTemplate { mapper.binding(TemplateVariable.DESC.getVariable(), doc.getDesc()); mapper.binding(TemplateVariable.NAME.getVariable(), doc.getName()); 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); } } diff --git a/src/main/java/com/power/doc/builder/HtmlApiDocBuilder.java b/src/main/java/com/power/doc/builder/HtmlApiDocBuilder.java index 55d5862..7d3ee5f 100644 --- a/src/main/java/com/power/doc/builder/HtmlApiDocBuilder.java +++ b/src/main/java/com/power/doc/builder/HtmlApiDocBuilder.java @@ -93,7 +93,7 @@ public class HtmlApiDocBuilder { List apiDocDictList = builderTemplate.buildDictionary(config, javaProjectBuilder); buildIndex(apiDocList, config); copyCss(config.getOutPath()); - buildDoc(apiDocList, config.getOutPath()); + buildDoc(apiDocList, config); buildErrorCodeDoc(config.getErrorCodes(), config.getOutPath()); buildDictionary(apiDocDictList, config.getOutPath()); } @@ -149,20 +149,22 @@ public class HtmlApiDocBuilder { * build ever controller api * * @param apiDocList list of api doc - * @param outPath output path + * @param config ApiConfig */ - private static void buildDoc(List apiDocList, String outPath) { - FileUtil.mkdirs(outPath); + private static void buildDoc(List apiDocList, ApiConfig config) { + FileUtil.mkdirs(config.getOutPath()); Template htmlApiDoc; for (ApiDoc doc : apiDocList) { 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.NAME.getVariable(), doc.getName()); apiTemplate.binding(TemplateVariable.LIST.getVariable(), doc.getList());//类名 Map templateVariables = new HashMap<>(); templateVariables.put(TemplateVariable.TITLE.getVariable(), doc.getDesc()); 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"); } } diff --git a/src/main/java/com/power/doc/builder/YapiJsonBuilder.java b/src/main/java/com/power/doc/builder/YapiJsonBuilder.java index 392dbf3..671ef3a 100644 --- a/src/main/java/com/power/doc/builder/YapiJsonBuilder.java +++ b/src/main/java/com/power/doc/builder/YapiJsonBuilder.java @@ -29,7 +29,9 @@ import com.power.common.util.FileUtil; import com.power.common.util.StringUtil; import com.power.doc.constants.DocGlobalConstants; 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.SpringBootDocBuildTemplate; 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 + * * @author dai19470 2020/08/20. */ public class YapiJsonBuilder { @@ -57,7 +60,6 @@ public class YapiJsonBuilder { * @param config 配置文件 */ public static void buildYapiCollection(ApiConfig config) { - DocBuilderTemplate builderTemplate = new DocBuilderTemplate(); builderTemplate.checkAndInit(config); JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder(); @@ -66,110 +68,110 @@ public class YapiJsonBuilder { } - private static Set getUrl(String url,String patter){ + private static Set getUrl(String url, String patter) { Pattern pattern = Pattern.compile(patter); Matcher matcher = pattern.matcher(url); Set result = new HashSet<>(); - while(matcher.find()){ + while (matcher.find()) { result.add(matcher.group()); } return result; } - private static void yapiJsonCreate(ApiConfig config, ProjectDocConfigBuilder configBuilder) { + config.setParamsDataToTree(true); IDocBuildTemplate docBuildTemplate = new SpringBootDocBuildTemplate(); List apiDocList = docBuildTemplate.getApiData(configBuilder); - List> requestItem = new ArrayList<>(); + List> requestItem = new ArrayList<>(); Iterables.forEach(apiDocList, (index, apiDoc) -> { - Map module = new HashMap<>(); - module.put("index",index); - module.put("name",apiDoc.getDesc()); - module.put("parent_id",-1); - module.put("desc",apiDoc.getDesc()); - module.put("add_time",System.currentTimeMillis() / 1000); - module.put("up_time",System.currentTimeMillis() / 1000); + Map module = new HashMap<>(); + module.put("index", index); + module.put("name", apiDoc.getDesc()); + module.put("parent_id", -1); + module.put("desc", apiDoc.getDesc()); + module.put("add_time", System.currentTimeMillis() / 1000); + module.put("up_time", System.currentTimeMillis() / 1000); - List> methods = new ArrayList(); + List> methods = new ArrayList(); Iterables.forEach(apiDoc.getList(), (idx, apiMethodDoc) -> { - Map method = new HashMap<>(); - Map map = new HashMap<>(); - map.put("path",apiMethodDoc.getPath()); - map.put("params",new Object[]{}); - method.put("query_path",map); + Map method = new HashMap<>(); + Map map = new HashMap<>(); + map.put("path", apiMethodDoc.getPath()); + map.put("params", new Object[]{}); + method.put("query_path", map); // method.put("owners",new String[]{apiMethodDoc.getAuthor()}); - method.put("owners",new String[]{}); - method.put("edit_uid",0); - method.put("status","done"); - method.put("type","static"); - method.put("req_body_is_json_schema",true); - method.put("res_body_is_json_schema",true); - method.put("api_opened",false); - method.put("index",idx); - method.put("tag",new Object[]{}); - method.put("method",apiMethodDoc.getType()); - method.put("title",apiMethodDoc.getDesc()); - method.put("desc",apiMethodDoc.getDetail()); - method.put("name",apiMethodDoc.getName()); - method.put("path",apiMethodDoc.getPath().replace("//","/")); + method.put("owners", new String[]{}); + method.put("edit_uid", 0); + method.put("status", "done"); + method.put("type", "static"); + method.put("req_body_is_json_schema", true); + method.put("res_body_is_json_schema", true); + method.put("api_opened", false); + method.put("index", idx); + method.put("tag", new Object[]{}); + method.put("method", apiMethodDoc.getType()); + method.put("title", apiMethodDoc.getDesc()); + method.put("desc", apiMethodDoc.getDetail()); + method.put("name", apiMethodDoc.getName()); + method.put("path", apiMethodDoc.getPath().replace("//", "/")); method.put("req_body_form", Arrays.asList()); - List> req_params = new ArrayList(); + List> req_params = new ArrayList(); Set 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); - if(temp!=null){ - Map h = new HashMap<>(); - h.put("example",""); - h.put("name",temp.getField()); - h.put("type",temp.getType()); - h.put("desc",temp.getDesc()); - req_params.add(j,h); + if (temp != null) { + Map h = new HashMap<>(); + h.put("example", ""); + h.put("name", temp.getField()); + h.put("type", temp.getType()); + h.put("desc", temp.getDesc()); + req_params.add(j, h); } }); - method.put("req_params",req_params); + method.put("req_params", req_params); - method.put("res_body_type","json"); - List> querys = new ArrayList(); - Iterables.forEach(apiMethodDoc.getRequestParams(),(j,res)->{ - Map h = new HashMap<>(); - h.put("required",res.isRequired()?"1":"0"); - h.put("desc",res.getDesc()); - h.put("name",res.getField()); - h.put("example",""); - h.put("type",res.getType()); - querys.add(j,h); + method.put("res_body_type", "json"); + List> querys = new ArrayList(); + Iterables.forEach(apiMethodDoc.getRequestParams(), (j, res) -> { + Map h = new HashMap<>(); + h.put("required", res.isRequired() ? "1" : "0"); + h.put("desc", res.getDesc()); + h.put("name", res.getField()); + h.put("example", ""); + h.put("type", res.getType()); + querys.add(j, h); }); - method.put("req_query",querys); + method.put("req_query", querys); - List> headers = new ArrayList(); - Iterables.forEach(apiMethodDoc.getRequestHeaders(),(j,res)->{ - Map h = new HashMap<>(); - h.put("required",res.isRequired()?"1":"0"); - h.put("value",res.getValue()); - h.put("name",res.getName()); - h.put("desc",res.getDesc()); - headers.add(j,h); + List> headers = new ArrayList(); + Iterables.forEach(apiMethodDoc.getRequestHeaders(), (j, res) -> { + Map h = new HashMap<>(); + h.put("required", res.isRequired() ? "1" : "0"); + h.put("value", res.getValue()); + h.put("name", res.getName()); + h.put("desc", res.getDesc()); + headers.add(j, h); }); - method.put("req_headers",headers); + method.put("req_headers", headers); Template apiTemplate = BeetlTemplateUtil.getByName(YAPI_RESULT_TPL); apiTemplate.binding(TemplateVariable.RESPONSELIST.getVariable(), generateJson(apiMethodDoc.getResponseParams())); - String json = apiTemplate.render(); - method.put("res_body",json); + String json = apiTemplate.render(); + method.put("res_body", json); - if(StringUtil.isNotEmpty(apiMethodDoc.getResponseUsage())){ - method.put("desc","
\n"+apiMethodDoc.getResponseUsage()+"\n
\n"); + if (StringUtil.isNotEmpty(apiMethodDoc.getResponseUsage())) { + method.put("desc", "
\n" + apiMethodDoc.getResponseUsage() + "\n
\n"); } - methods.add(idx,method); + methods.add(idx, method); }); - module.put("list",methods); + module.put("list", methods); requestItem.add(module); }); @@ -183,26 +185,27 @@ public class YapiJsonBuilder { private static String generateJson(List responseParams) { StringBuffer re = new StringBuffer("\"type\":\"object\",\n\"properties\":{\n"); - HashSet required=new HashSet<>(); + HashSet required = new HashSet<>(); responseParams.stream().forEach(apiParam -> { re.append(getTypeAndPropertiesJson(apiParam)); - if(apiParam.isRequired()){ + if (apiParam.isRequired()) { required.add(apiParam.getField()); } - } ); + }); Gson gs = new Gson(); - re.append("\"required\":\""+gs.toJson(required.toArray())+"\""); + re.append("\"required\":\"" + gs.toJson(required.toArray()) + "\""); re.append("\t}"); return re.toString(); } /** * 将字段类型转换为yapi的字段类型 - * @param type - * @return + * + * @param type java type + * @return String */ public static String changeType(String type) { - switch (type){ + switch (type) { case "boolean": return "boolean"; case "int32": @@ -216,25 +219,26 @@ public class YapiJsonBuilder { /** * 单个参数拼接字符串 - * @param param - * @return + * + * @param param ApiParam + * @return String */ - public static String getTypeAndPropertiesJson(ApiParam param){ - StringBuffer resultJson = new StringBuffer( ); + public static String getTypeAndPropertiesJson(ApiParam param) { + StringBuffer resultJson = new StringBuffer(); - resultJson.append("\""+param.getField()+"\":{"); - resultJson.append("\"type\":\""+changeType(param.getType())+"\", "); + resultJson.append("\"" + param.getField() + "\":{"); + resultJson.append("\"type\":\"" + changeType(param.getType()) + "\", "); - if(param.getChildren()!=null&¶m.getChildren().size()>0){ - if(param.getType().equals("object")){ + if (param.getChildren() != null && param.getChildren().size() > 0) { + if (param.getType().equals("object")) { resultJson.append(" \"properties\":{"); - param.getChildren().forEach(child->{ + param.getChildren().forEach(child -> { resultJson.append(getTypeAndPropertiesJson(child)); }); - }else if(param.getType().equals("array")){ + } else if (param.getType().equals("array")) { resultJson.append(" \"items\":{"); - resultJson .append("\"type\":\"object\",\n\"properties\":{\n"); - param.getChildren().forEach(child->{ + resultJson.append("\"type\":\"object\",\n\"properties\":{\n"); + param.getChildren().forEach(child -> { resultJson.append(getTypeAndPropertiesJson(child)); }); resultJson.append("\t},");