diff --git a/library/build.gradle b/library/build.gradle index 8ec2486bf12d4b5255c730c1be6540f9a21f4451..d927a329c5b17ac9efb319ae3ad2a0338b0c609b 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -36,7 +36,7 @@ test { dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' - + compile 'com.google.guava:guava:22.0' compile 'com.alibaba:fastjson:1.2.70' compile 'com.github.javaparser:javaparser-core:3.6.16' compile group: 'org.freemarker', name: 'freemarker', version: '2.3.30' diff --git a/library/src/main/java/io/github/yedaxia/apidocs/Docs.java b/library/src/main/java/io/github/yedaxia/apidocs/Docs.java index 91ad4aa16f620a55d56b793293c7d854c3b2e351..0a48a8fd9bc2ef89e1f82702e188b32c9ffec33d 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/Docs.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/Docs.java @@ -1,7 +1,7 @@ package io.github.yedaxia.apidocs; -import io.github.yedaxia.apidocs.doc.HtmlDocGenerator; -import io.github.yedaxia.apidocs.plugin.rap.RapSupportPlugin; +import io.github.yedaxia.apidocs.doc.AbsDocGenerator; +import io.github.yedaxia.apidocs.plugin.IPluginSupport; import java.io.File; import java.io.FileReader; @@ -15,29 +15,21 @@ public class Docs { private static final String CONFIG_FILE = "docs.config"; - public static void main(String[] args){ - DocsConfig config = loadProps(); - buildHtmlDocs(config); - } + /** * build html api docs */ public static void buildHtmlDocs(DocsConfig config){ DocContext.init(config); - HtmlDocGenerator docGenerator = new HtmlDocGenerator(); + AbsDocGenerator docGenerator = new AbsDocGenerator(DocContext.controllerParser()); + DocContext.setControllerNodeList(docGenerator.getControllerNodeList()); DocContext.setControllerNodeList(docGenerator.getControllerNodeList()); - docGenerator.generateDocs(); CacheUtils.saveControllerNodes(docGenerator.getControllerNodeList()); - DocsConfig docsConfig = DocContext.getDocsConfig(); - if(docsConfig.getRapProjectId() != null && docsConfig.getRapHost() != null){ - IPluginSupport rapPlugin = new RapSupportPlugin(); - rapPlugin.execute(docGenerator.getControllerNodeList()); - } for(IPluginSupport plugin: config.getPlugins()){ plugin.execute(docGenerator.getControllerNodeList()); } - } + } /** * wrap response into a common structure,don't forget to put responseNode into map. diff --git a/library/src/main/java/io/github/yedaxia/apidocs/DocsConfig.java b/library/src/main/java/io/github/yedaxia/apidocs/DocsConfig.java index 3c6ee3bec22d5755dac34981e02dae25da77cf55..4735c1b43ac68c0c37d190a4ddf91d13bb7be8fc 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/DocsConfig.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/DocsConfig.java @@ -1,5 +1,7 @@ package io.github.yedaxia.apidocs; +import io.github.yedaxia.apidocs.plugin.IPluginSupport; + import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -32,11 +34,30 @@ public class DocsConfig { return plugins; } - /** - * add your own plugin, example: - * @see io.github.yedaxia.apidocs.plugin.rap.RapSupportPlugin - * @param plugin - */ + public String getDocsPath() { + return docsPath; + } + + public void setRapHost(String rapHost) { + this.rapHost = rapHost; + } + + public void setRapLoginCookie(String rapLoginCookie) { + this.rapLoginCookie = rapLoginCookie; + } + + public void setRapProjectId(String rapProjectId) { + this.rapProjectId = rapProjectId; + } + + public void setRapAccount(String rapAccount) { + this.rapAccount = rapAccount; + } + + public void setRapPassword(String rapPassword) { + this.rapPassword = rapPassword; + } + public void addPlugin(IPluginSupport plugin) { this.plugins.add(plugin); } diff --git a/library/src/main/java/io/github/yedaxia/apidocs/Utils.java b/library/src/main/java/io/github/yedaxia/apidocs/Utils.java index 3b3ff3969486267c48ee9b08b668798c258c5bf1..9e33841fb99e97494a8e381b157ef7ee7c211a54 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/Utils.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/Utils.java @@ -1,6 +1,7 @@ package io.github.yedaxia.apidocs; import com.alibaba.fastjson.JSONObject; +import com.google.common.base.Strings; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; @@ -14,6 +15,39 @@ import java.util.List; public class Utils { + /** + * 获取默认值 + * @param type + * @param content + * @return + */ + public static String getDefulatValue(String type, String content) { + if (Strings.isNullOrEmpty(type)) { + return ""; + } + switch (type) { + case "int": + return "0"; + case "long": + return "0"; + case "double": + return "0.0"; + case "byte": + return "0"; + case "char": + return ""; + case "short": + return "0"; + case "boolean": + return "false"; + case "string": + return "string"; + case "_object": + return content; + default: + return ""; + } + } /** * object to pretty json * @param map diff --git a/library/src/main/java/io/github/yedaxia/apidocs/doc/AbsDocGenerator.java b/library/src/main/java/io/github/yedaxia/apidocs/doc/AbsDocGenerator.java index 91ef9b659e8c9e71fcc5ea8acad690d571af3d08..143a8ca7f1611d5fafba799d6a04b0b52cae4e00 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/doc/AbsDocGenerator.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/doc/AbsDocGenerator.java @@ -2,42 +2,26 @@ package io.github.yedaxia.apidocs.doc; import io.github.yedaxia.apidocs.DocContext; import io.github.yedaxia.apidocs.LogUtils; -import io.github.yedaxia.apidocs.Utils; -import io.github.yedaxia.apidocs.codegenerator.ios.ModelCodeGenerator; -import io.github.yedaxia.apidocs.codegenerator.java.JavaCodeGenerator; import io.github.yedaxia.apidocs.parser.AbsControllerParser; import io.github.yedaxia.apidocs.parser.ControllerNode; import io.github.yedaxia.apidocs.parser.RequestNode; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.List; -public abstract class AbsDocGenerator { +public class AbsDocGenerator { private AbsControllerParser controllerParser; - private IControllerDocBuilder controllerDocBuilder; - private List docFileLinkList = new ArrayList<>(); private List controllerNodeList = new ArrayList<>(); - AbsDocGenerator(AbsControllerParser controllerParser, IControllerDocBuilder controllerDocBuilder) { + public AbsDocGenerator(AbsControllerParser controllerParser) { this.controllerParser = controllerParser; - this.controllerDocBuilder = controllerDocBuilder; this.initControllerNodes(); } - /** - * generate api Docs - */ - public void generateDocs() { - LogUtils.info("generate api docs start..."); - generateControllersDocs(); - generateIndex(controllerNodeList); - LogUtils.info("generate api docs done !!!"); - } - private void initControllerNodes(){ + private void initControllerNodes() { File[] controllerFiles = DocContext.getControllerFiles(); for (File controllerFile : controllerFiles) { LogUtils.info("start to parse controller file : %s", controllerFile.getName()); @@ -58,24 +42,9 @@ public abstract class AbsDocGenerator { } } - private void generateControllersDocs() { - File docPath = new File(DocContext.getDocPath()); - for (ControllerNode controllerNode : controllerNodeList) { - try { - LogUtils.info("start to generate docs for controller file : %s", controllerNode.getSrcFileName()); - final String controllerDocs = controllerDocBuilder.buildDoc(controllerNode); - docFileLinkList.add(new Link(controllerNode.getDescription(), String.format("%s", controllerNode.getDocFileName()))); - Utils.writeToDisk(new File(docPath, controllerNode.getDocFileName()), controllerDocs); - LogUtils.info("success to generate docs for controller file : %s", controllerNode.getSrcFileName()); - } catch (IOException e) { - LogUtils.error("generate docs for controller file : " + controllerNode.getSrcFileName() + " fail", e); - } - } - } public List getControllerNodeList() { return controllerNodeList; } - abstract void generateIndex(List controllerNodeList); } diff --git a/library/src/main/java/io/github/yedaxia/apidocs/parser/HeaderNode.java b/library/src/main/java/io/github/yedaxia/apidocs/parser/HeaderNode.java index e861171278da822f33575e8ee3beaac7d2aefc05..97ce550927c0c63506ad8a0e76c1d328a66c5cf1 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/parser/HeaderNode.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/parser/HeaderNode.java @@ -1,7 +1,5 @@ package io.github.yedaxia.apidocs.parser; -import java.util.HashMap; - /** * Created by lzw on 2017/8/23. */ @@ -14,4 +12,11 @@ public class HeaderNode { this.name = name; this.value = value; } + public String getName() { + return name; + } + + public String getValue() { + return value; + } } diff --git a/library/src/main/java/io/github/yedaxia/apidocs/IPluginSupport.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/IPluginSupport.java similarity index 90% rename from library/src/main/java/io/github/yedaxia/apidocs/IPluginSupport.java rename to library/src/main/java/io/github/yedaxia/apidocs/plugin/IPluginSupport.java index 21b582f1d5a8b2082a47c3b3f1df19b9844d6808..548fa931cb2fc664a6f5f19e5f7e85ecf216d994 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/IPluginSupport.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/IPluginSupport.java @@ -1,4 +1,5 @@ -package io.github.yedaxia.apidocs; +package io.github.yedaxia.apidocs.plugin; + import io.github.yedaxia.apidocs.parser.ControllerNode; diff --git a/library/src/main/java/io/github/yedaxia/apidocs/doc/HtmlControllerDocBuilder.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/html/HtmlControllerDocBuilder.java similarity index 91% rename from library/src/main/java/io/github/yedaxia/apidocs/doc/HtmlControllerDocBuilder.java rename to library/src/main/java/io/github/yedaxia/apidocs/plugin/html/HtmlControllerDocBuilder.java index b827310242a5ec11bda2b4dd921850a13035c0fc..0b92dd660f8011311153e1c29e0310747fb1b603 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/doc/HtmlControllerDocBuilder.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/html/HtmlControllerDocBuilder.java @@ -1,21 +1,21 @@ -package io.github.yedaxia.apidocs.doc; +package io.github.yedaxia.apidocs.plugin.html; -import freemarker.template.Configuration; -import freemarker.template.Template; -import freemarker.template.TemplateException; -import freemarker.template.Version; import io.github.yedaxia.apidocs.DocContext; -import io.github.yedaxia.apidocs.parser.ParamNode; import io.github.yedaxia.apidocs.Resources; import io.github.yedaxia.apidocs.Utils; import io.github.yedaxia.apidocs.codegenerator.ios.ModelCodeGenerator; import io.github.yedaxia.apidocs.codegenerator.java.JavaCodeGenerator; import io.github.yedaxia.apidocs.parser.ControllerNode; import io.github.yedaxia.apidocs.parser.RequestNode; +import freemarker.template.Template; +import freemarker.template.TemplateException; -import javax.print.Doc; -import java.io.*; -import java.util.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; /** * build html api docs for a controller diff --git a/library/src/main/java/io/github/yedaxia/apidocs/doc/HtmlDocGenerator.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/html/HtmlPlugin.java similarity index 55% rename from library/src/main/java/io/github/yedaxia/apidocs/doc/HtmlDocGenerator.java rename to library/src/main/java/io/github/yedaxia/apidocs/plugin/html/HtmlPlugin.java index 48efc469c9c754f15d0b54f80e2db7f5b7f2ea81..008f6b3d32ca42d9402f81529b44554f4b12dd6f 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/doc/HtmlDocGenerator.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/html/HtmlPlugin.java @@ -1,18 +1,14 @@ -package io.github.yedaxia.apidocs.doc; +package io.github.yedaxia.apidocs.plugin.html; +import io.github.yedaxia.apidocs.*; +import io.github.yedaxia.apidocs.parser.ControllerNode; +import io.github.yedaxia.apidocs.plugin.IPluginSupport; import freemarker.template.Template; import freemarker.template.TemplateException; -import io.github.yedaxia.apidocs.DocContext; -import io.github.yedaxia.apidocs.LogUtils; -import io.github.yedaxia.apidocs.Resources; -import io.github.yedaxia.apidocs.Utils; -import io.github.yedaxia.apidocs.parser.ControllerNode; -import io.github.yedaxia.apidocs.parser.AbsControllerParser; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -23,13 +19,29 @@ import java.util.Map; * * @author yeguozhong yedaxia.github.com */ -public class HtmlDocGenerator extends AbsDocGenerator { +public class HtmlPlugin implements IPluginSupport { + private IControllerDocBuilder controllerDocBuilder; + private List docFileLinkList = new ArrayList<>(); - public HtmlDocGenerator() { - super(DocContext.controllerParser(), new HtmlControllerDocBuilder()); + public HtmlPlugin() { + this.controllerDocBuilder = new HtmlControllerDocBuilder(); + } + + private void generateControllersDocs(List controllerNodeList) { + File docPath = new File(DocContext.getDocPath()); + for (ControllerNode controllerNode : controllerNodeList) { + try { + LogUtils.info("start to generate docs for controller file : %s", controllerNode.getSrcFileName()); + final String controllerDocs = controllerDocBuilder.buildDoc(controllerNode); + docFileLinkList.add(new Link(controllerNode.getDescription(), String.format("%s", controllerNode.getDocFileName()))); + Utils.writeToDisk(new File(docPath, controllerNode.getDocFileName()), controllerDocs); + LogUtils.info("success to generate docs for controller file : %s", controllerNode.getSrcFileName()); + } catch (IOException e) { + LogUtils.error("generate docs for controller file : " + controllerNode.getSrcFileName() + " fail", e); + } + } } - @Override void generateIndex(List controllerNodeList) { FileWriter docFileWriter = null; try { @@ -67,4 +79,12 @@ public class HtmlDocGenerator extends AbsDocGenerator { private Template getIndexTpl() throws IOException { return Resources.getFreemarkerTemplate("api-index.html.ftl"); } + + @Override + public void execute(List controllerNodeList) { + LogUtils.info("generate api docs start..."); + generateControllersDocs(controllerNodeList); + generateIndex(controllerNodeList); + LogUtils.info("generate api docs done !!!"); + } } diff --git a/library/src/main/java/io/github/yedaxia/apidocs/doc/IControllerDocBuilder.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/html/IControllerDocBuilder.java similarity index 78% rename from library/src/main/java/io/github/yedaxia/apidocs/doc/IControllerDocBuilder.java rename to library/src/main/java/io/github/yedaxia/apidocs/plugin/html/IControllerDocBuilder.java index 887249f025c0cccc985a45e282c9e8632953a245..321199d2e9df98e2ad5945dd823801b927267e7f 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/doc/IControllerDocBuilder.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/html/IControllerDocBuilder.java @@ -1,9 +1,7 @@ -package io.github.yedaxia.apidocs.doc; +package io.github.yedaxia.apidocs.plugin.html; import io.github.yedaxia.apidocs.parser.ControllerNode; -import io.github.yedaxia.apidocs.parser.ResponseNode; -import java.io.File; import java.io.IOException; /** diff --git a/library/src/main/java/io/github/yedaxia/apidocs/doc/Link.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/html/Link.java similarity index 87% rename from library/src/main/java/io/github/yedaxia/apidocs/doc/Link.java rename to library/src/main/java/io/github/yedaxia/apidocs/plugin/html/Link.java index 439ae34cfa304e64c124d96456d2905ca4979941..c044ba0cd80bedf4f391d7d8d28af746707dbe03 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/doc/Link.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/html/Link.java @@ -1,9 +1,9 @@ -package io.github.yedaxia.apidocs.doc; +package io.github.yedaxia.apidocs.plugin.html; /** * @author yeguozhong yedaxia.github.com */ -class Link { +public class Link { private String name; private String url; diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/markdown/MarkdownDocPlugin.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/markdown/MarkdownDocPlugin.java index cf2930c746b165abb64ff2b1ff23698eccb5363a..8fd295f417e2fc55cb2233b8e2fd9e6745b46141 100644 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/markdown/MarkdownDocPlugin.java +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/markdown/MarkdownDocPlugin.java @@ -3,10 +3,10 @@ package io.github.yedaxia.apidocs.plugin.markdown; import freemarker.template.Template; import freemarker.template.TemplateException; import io.github.yedaxia.apidocs.DocContext; -import io.github.yedaxia.apidocs.IPluginSupport; import io.github.yedaxia.apidocs.Resources; import io.github.yedaxia.apidocs.Utils; import io.github.yedaxia.apidocs.parser.ControllerNode; +import io.github.yedaxia.apidocs.plugin.IPluginSupport; import java.io.File; import java.io.FileWriter; diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/PostmanPlugin.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/PostmanPlugin.java new file mode 100644 index 0000000000000000000000000000000000000000..b914059cadfb7c0f2da7b8dbaadb40d38b6b973f --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/PostmanPlugin.java @@ -0,0 +1,32 @@ +package io.github.yedaxia.apidocs.plugin.postman; + +import io.github.yedaxia.apidocs.DocContext; +import io.github.yedaxia.apidocs.DocsConfig; +import io.github.yedaxia.apidocs.plugin.IPluginSupport; +import io.github.yedaxia.apidocs.Utils; +import io.github.yedaxia.apidocs.parser.ControllerNode; +import io.github.yedaxia.apidocs.plugin.postman.domain.PostmanExportFormat; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.List; + +public class PostmanPlugin implements IPluginSupport { + @Override + public void execute(List controllerNodeList) { + PostmanExportFormat exportFormat = new PostmanExportFormat(); + DocsConfig docsConfig = DocContext.getDocsConfig(); + exportFormat.load(docsConfig, controllerNodeList); + String postmanFileName = String.format("postman-%s-%s.json", docsConfig.getProjectName(), docsConfig.getApiVersion()); + try { + File saveFile = new File(Paths.get(docsConfig.getDocsPath(), docsConfig.getApiVersion(), postmanFileName).toUri()); + if (saveFile.exists()) { + saveFile.delete(); + } + Utils.writeToDisk(saveFile, Utils.toJson(exportFormat)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Body.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Body.java new file mode 100644 index 0000000000000000000000000000000000000000..4a3e0a28642e89592041d14e85408cbe1685c257 --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Body.java @@ -0,0 +1,38 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.postman.domain; + +/** + * Auto-generated: 2020-10-11 10:6:58 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class Body { + + private String mode; + private String raw; + private Options options; + public void setMode(String mode) { + this.mode = mode; + } + public String getMode() { + return mode; + } + + public void setRaw(String raw) { + this.raw = raw; + } + public String getRaw() { + return raw; + } + + public void setOptions(Options options) { + this.options = options; + } + public Options getOptions() { + return options; + } + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Header.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Header.java new file mode 100644 index 0000000000000000000000000000000000000000..353f77280381eb5dd039e768d37b5e90c1dd32c8 --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Header.java @@ -0,0 +1,38 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.postman.domain; + +/** + * Auto-generated: 2020-10-11 10:6:58 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class Header { + + private String key; + private String value; + private String type; + public void setKey(String key) { + this.key = key; + } + public String getKey() { + return key; + } + + public void setValue(String value) { + this.value = value; + } + public String getValue() { + return value; + } + + public void setType(String type) { + this.type = type; + } + public String getType() { + return type; + } + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Info.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Info.java new file mode 100644 index 0000000000000000000000000000000000000000..bbc6ff5a2bb289c84f986979e8ba799575a9324b --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Info.java @@ -0,0 +1,38 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.postman.domain; + +/** + * Auto-generated: 2020-10-11 10:6:58 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class Info { + + private String _postman_id; + private String name; + private String schema; + public void set_postman_id(String _postman_id) { + this._postman_id = _postman_id; + } + public String get_postman_id() { + return _postman_id; + } + + public void setName(String name) { + this.name = name; + } + public String getName() { + return name; + } + + public void setSchema(String schema) { + this.schema = schema; + } + public String getSchema() { + return schema; + } + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Item.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Item.java new file mode 100644 index 0000000000000000000000000000000000000000..90784d3106907b803c698d4ee13d89678351474c --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Item.java @@ -0,0 +1,39 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.postman.domain; +import java.util.List; + +/** + * Auto-generated: 2020-10-11 10:6:58 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class Item { + + private String name; + private Request request; + private List response; + public void setName(String name) { + this.name = name; + } + public String getName() { + return name; + } + + public void setRequest(Request request) { + this.request = request; + } + public Request getRequest() { + return request; + } + + public void setResponse(List response) { + this.response = response; + } + public List getResponse() { + return response; + } + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Options.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Options.java new file mode 100644 index 0000000000000000000000000000000000000000..195ef70b80ca22327bb9305d8e4235b44c1140ec --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Options.java @@ -0,0 +1,22 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.postman.domain; + +/** + * Auto-generated: 2020-10-11 10:6:58 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class Options { + + private Raw raw; + public void setRaw(Raw raw) { + this.raw = raw; + } + public Raw getRaw() { + return raw; + } + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/PostmanExportFormat.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/PostmanExportFormat.java new file mode 100644 index 0000000000000000000000000000000000000000..deb8dafb160baa6178369fc1158353786b1f72fa --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/PostmanExportFormat.java @@ -0,0 +1,136 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.postman.domain; + +import com.google.common.base.Splitter; +import io.github.yedaxia.apidocs.DocsConfig; +import io.github.yedaxia.apidocs.parser.ControllerNode; +import io.github.yedaxia.apidocs.parser.HeaderNode; +import io.github.yedaxia.apidocs.parser.ParamNode; +import io.github.yedaxia.apidocs.parser.RequestNode; + +import java.util.*; + +import static io.github.yedaxia.apidocs.Utils.getDefulatValue; + +/** + * Auto-generated: 2020-10-11 10:6:58 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class PostmanExportFormat { + private String serverHost = "{{server_host}}:{{server_port}}"; + private String serverContextPath = "{{server_context_path}}"; + private Info info = new Info(); + private List item = new ArrayList<>(); + private ProtocolProfileBehavior protocolProfileBehavior = new ProtocolProfileBehavior(); + + public void setInfo(Info info) { + this.info = info; + } + + public Info getInfo() { + return info; + } + + public void setItem(List item) { + this.item = item; + } + + public List getItem() { + return item; + } + + public void setProtocolProfileBehavior(ProtocolProfileBehavior protocolProfileBehavior) { + this.protocolProfileBehavior = protocolProfileBehavior; + } + + public ProtocolProfileBehavior getProtocolProfileBehavior() { + return protocolProfileBehavior; + } + + public void load(DocsConfig docsConfig, List controllerNodeList) { + info.setName(docsConfig.getProjectName()); + info.setSchema("https://schema.getpostman.com/json/collection/v2.1.0/collection.json"); + for (ControllerNode controllerNode : controllerNodeList) { + for (RequestNode requestNode : controllerNode.getRequestNodes()) { + /** + * 生成接口 + */ + Item itemObj = new Item(); + itemObj.setName(Optional.of(requestNode.getDescription()).orElse(requestNode.getMethodName())); + Request request = new Request(); + request.setMethod(requestNode.getMethod().get(0)); + request.setHeader(genHeader(requestNode)); + request.setUrl(genUrl(requestNode)); + request.setBody(genBody(requestNode)); + itemObj.setRequest(request); + itemObj.setResponse(Collections.emptyList()); + item.add(itemObj); + } + } + } + + private List
genHeader(RequestNode requestNode) { + List
headers = new ArrayList<>(); + for (HeaderNode headerNode : requestNode.getHeader()) { + Header header = new Header(); + header.setKey(headerNode.getName()); + header.setValue(headerNode.getValue()); + header.setType("string"); + headers.add(header); + } + return headers; + } + + private Body genBody(RequestNode requestNode) { + if (requestNode.getParamNodes().size() == 0 || !requestNode.getParamNodes().get(0).getJsonBody()) { + return null; + } + Body body = new Body(); + setOption(body); + List paramNodes = requestNode.getParamNodes(); + for (ParamNode paramNode : paramNodes) { + if (paramNode.getJsonBody()) { + body.setRaw(paramNode.getDescription()); + } + } + return body; + } + + private void setOption(Body body) { + body.setMode("raw"); + Options option = new Options(); + Raw raw = new Raw(); + raw.setLanguage("json"); + option.setRaw(raw); + body.setOptions(option); + } + + + + private Url genUrl(RequestNode requestNode) { + Url url = new Url(); + url.setRaw(String.format("http://%s/%s/%s", serverHost, serverContextPath, requestNode.getUrl())); + url.setHost(Arrays.asList(serverHost)); + url.setProtocol("http"); + List paths = new ArrayList<>(); + paths.add(serverContextPath); + paths.addAll(Splitter.on("/").omitEmptyStrings().splitToList(requestNode.getUrl())); + url.setPath(paths); + for (ParamNode paramNode : requestNode.getParamNodes()) { + if (!paramNode.getJsonBody()) { + Query query = new Query(); + query.setKey(paramNode.getName()); + query.setValue(getDefulatValue(paramNode.getType(), "")); + query.setDescription(paramNode.getDescription()); + url.getQuery().add(query); + } + } + return url; + } + + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/ProtocolProfileBehavior.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/ProtocolProfileBehavior.java new file mode 100644 index 0000000000000000000000000000000000000000..697343c5feb78c39716c4c3776792a6a03768f2b --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/ProtocolProfileBehavior.java @@ -0,0 +1,14 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.postman.domain; + +/** + * Auto-generated: 2020-10-11 10:6:58 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class ProtocolProfileBehavior { + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Query.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Query.java new file mode 100644 index 0000000000000000000000000000000000000000..b842e3d11b42bdb55e307a13a1258402b15723b0 --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Query.java @@ -0,0 +1,33 @@ +package io.github.yedaxia.apidocs.plugin.postman.domain; + +public class Query { + + private String key; + private String value; + private String description; + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setKey(String key) { + this.key = key; + } + + public String getKey() { + return key; + } + + public void setValue(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + +} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Raw.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Raw.java new file mode 100644 index 0000000000000000000000000000000000000000..0d94f4b5214a70174fcc15b3031907e214bf9113 --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Raw.java @@ -0,0 +1,22 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.postman.domain; + +/** + * Auto-generated: 2020-10-11 10:6:58 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class Raw { + + private String language; + public void setLanguage(String language) { + this.language = language; + } + public String getLanguage() { + return language; + } + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Request.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Request.java new file mode 100644 index 0000000000000000000000000000000000000000..8f58d1507dc83442b64ab95719e0003f066772b6 --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Request.java @@ -0,0 +1,47 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.postman.domain; +import java.util.List; + +/** + * Auto-generated: 2020-10-11 10:6:58 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class Request { + + private String method; + private List
header; + private Body body; + private Url url; + public void setMethod(String method) { + this.method = method; + } + public String getMethod() { + return method; + } + + public void setHeader(List
header) { + this.header = header; + } + public List
getHeader() { + return header; + } + + public void setBody(Body body) { + this.body = body; + } + public Body getBody() { + return body; + } + + public void setUrl(Url url) { + this.url = url; + } + public Url getUrl() { + return url; + } + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Url.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Url.java new file mode 100644 index 0000000000000000000000000000000000000000..59b4e7a1ce586f17245b50c84972e96d510b666c --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/postman/domain/Url.java @@ -0,0 +1,71 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.postman.domain; + +import java.util.ArrayList; +import java.util.List; + +/** + * Auto-generated: 2020-10-11 10:6:58 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class Url { + + private String raw; + private String protocol; + private List host; + private String port; + private List path = new ArrayList<>(); + private List query = new ArrayList<>(); + + public String getRaw() { + return raw; + } + + public void setRaw(String raw) { + this.raw = raw; + } + + public String getProtocol() { + return protocol; + } + + public void setProtocol(String protocol) { + this.protocol = protocol; + } + + public List getHost() { + return host; + } + + public void setHost(List host) { + this.host = host; + } + + public String getPort() { + return port; + } + + public void setPort(String port) { + this.port = port; + } + + public List getPath() { + return path; + } + + public void setPath(List path) { + this.path = path; + } + + public List getQuery() { + return query; + } + + public void setQuery(List query) { + this.query = query; + } +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Action.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Action.java deleted file mode 100644 index 01d0bcdd2311857a9882c4ba697767132e322553..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Action.java +++ /dev/null @@ -1,116 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -import java.util.HashSet; -import java.util.Set; - -/** - * @author yeguozhong yedaxia.github.com - */ -class Action { - - private int id; - private int disableCache; - private String name; - private String description; - private String requestType = "1"; - private String requestUrl; - private Set requestParameterList = new HashSet(); - private Set responseParameterList = new HashSet(); - private String responseTemplate; - private Set pageList = new HashSet(); - private String remarks; - - public static Action newAction(){ - Action action = new Action(); - action.setId(-1); - return action; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public int getDisableCache() { - return disableCache; - } - - public void setDisableCache(int disableCache) { - this.disableCache = disableCache; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getRequestType() { - return requestType; - } - - public void setRequestType(String requestType) { - this.requestType = requestType; - } - - public String getRequestUrl() { - return requestUrl; - } - - public void setRequestUrl(String requestUrl) { - this.requestUrl = requestUrl; - } - - public Set getRequestParameterList() { - return requestParameterList; - } - - public void setRequestParameterList(Set requestParameterList) { - this.requestParameterList = requestParameterList; - } - - public Set getResponseParameterList() { - return responseParameterList; - } - - public void setResponseParameterList(Set responseParameterList) { - this.responseParameterList = responseParameterList; - } - - public String getResponseTemplate() { - return responseTemplate; - } - - public void setResponseTemplate(String responseTemplate) { - this.responseTemplate = responseTemplate; - } - - public Set getPageList() { - return pageList; - } - - public void setPageList(Set pageList) { - this.pageList = pageList; - } - - public String getRemarks() { - return remarks; - } - - public void setRemarks(String remarks) { - this.remarks = remarks; - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/ActionType.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/ActionType.java deleted file mode 100644 index 97d238f9fcb6a401d14485a4f51978580ff7a332..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/ActionType.java +++ /dev/null @@ -1,18 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -/** - * @author yeguozhong yedaxia.github.com - */ -enum ActionType { - - GET("1"), - POST("2"), - PUT("3"), - DELETE("4"); - - public final String type; - - ActionType(String type) { - this.type = type; - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/DataType.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/DataType.java deleted file mode 100644 index f0b5a2c555126187458c48aa44294456d60a1c78..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/DataType.java +++ /dev/null @@ -1,143 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -import io.github.yedaxia.apidocs.parser.ParamNode; - -/** - * @author yeguozhong yedaxia.github.com - */ -class DataType { - - public static String STRING = "string"; - public static String NUMBER = "number"; - public static String BOOLEAN = "boolean"; - public static String OBJECT = "object"; - public static String ARRAY = "array"; - public static String ARRAY_NUMBER = "array"; - public static String ARRAY_STRING = "array"; - public static String ARRAY_BOOLEAN = "array"; - public static String ARRAY_OBJECT = "array"; - - public static final String MOCK = "@mock="; - - /** - * get rap type of param node - * - * @param nodeType - * @return - */ - public static String rapTypeOfNode(String nodeType) { - String pType = nodeType; - if (pType == null || pType.length() == 0) { - return STRING; - } - - if (isBooleanType(pType)) { - return BOOLEAN; - } - - if (isStringType(pType)) { - return STRING; - } - - if (isNumberType(pType)) { - return NUMBER; - } - - if (pType.endsWith("[]")) { - String cType = pType.replace("[]", ""); - - if (isBooleanType(cType)) { - return ARRAY_BOOLEAN; - } - - if (isStringType(cType)) { - return ARRAY_STRING; - } - - if (isNumberType(cType)) { - return ARRAY_NUMBER; - } - - return ARRAY_OBJECT; - - } else { - return OBJECT; - } - } - - /** - * is nodeType string an array or not - * @param nodeType - * @return - */ - public static boolean isArrayType(String nodeType){ - return nodeType != null && nodeType.endsWith("[]"); - } - - /** - * get mock type of param node - * - * @param nodeType - * @return - */ - public static String mockTypeOfNode(String nodeType) { - if(isArrayType(nodeType)){ - nodeType = nodeType.replace("[]", ""); - } - - if (isBooleanType(nodeType)) { - return MOCK + "@boolean".toUpperCase(); - } else if (isFloatType(nodeType)) { - return MOCK + "@float".toUpperCase(); - } else if (isIntType(nodeType)) { - return MOCK + "@integer".toUpperCase(); - } else if (isCharType(nodeType)) { - return MOCK + "@character".toUpperCase(); - } else if ("date".equalsIgnoreCase(nodeType)) { - return MOCK + "@datetime".toUpperCase(); - } else if("string".equalsIgnoreCase(nodeType)){ - return MOCK+ "@string".toUpperCase(); - } else { - return ""; - } - } - - /** - * return mock value - * @param value - * @return - */ - public static String mockValue(Object value){ - return MOCK + value; - } - - private static boolean isBooleanType(String pType) { - return pType != null && pType.equalsIgnoreCase("boolean"); - } - - private static boolean isNumberType(String pType) { - return pType != null && isFloatType(pType) || isIntType(pType); - } - - private static boolean isFloatType(String pType) { - return pType != null && (pType.equalsIgnoreCase("float") - || pType.equalsIgnoreCase("double")); - } - - private static boolean isIntType(String pType) { - return pType != null && (pType.equalsIgnoreCase("int") - || pType.equalsIgnoreCase("byte") - || pType.equalsIgnoreCase("short") - || pType.equalsIgnoreCase("long")); - } - - private static boolean isStringType(String pType) { - return pType != null && (pType.equalsIgnoreCase("date") - || pType.equalsIgnoreCase("string")); - } - - private static boolean isCharType(String pType) { - return pType != null && ("char".equalsIgnoreCase(pType) - || "Character".equalsIgnoreCase(pType)); - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/DeleteActionFrom.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/DeleteActionFrom.java deleted file mode 100644 index a9e220618afeccf465a706dbd23395bc554d1a21..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/DeleteActionFrom.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -/** - * @author yeguozhong yedaxia.github.com - */ -class DeleteActionFrom { - - private String className; - private Integer id; - - public String getClassName() { - return className; - } - - public void setClassName(String className) { - this.className = className; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/ModelResponse.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/ModelResponse.java deleted file mode 100644 index b04de4c81332d21df57d43d28c49738e1b6f78e2..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/ModelResponse.java +++ /dev/null @@ -1,35 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -/** - * @author yeguozhong yedaxia.github.com - */ -class ModelResponse { - - private int code; - private String msg; - private Project model; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getMsg() { - return msg; - } - - public void setMsg(String msg) { - this.msg = msg; - } - - public Project getModel() { - return model; - } - - public void setModel(Project model) { - this.model = model; - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Module.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Module.java deleted file mode 100644 index c61f9fcfac32891003c47c08ebfa9117b4c19996..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Module.java +++ /dev/null @@ -1,74 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -import java.util.HashSet; -import java.util.Set; - -/** - * @author yeguozhong yedaxia.github.com - */ -class Module { - - public static final String NAME = "API List"; - - private int id; - private int projectId; - private String name; - private String introduction; - private Project project; - private Set pageList = new HashSet(); - - public static Module newModule(){ - Module module = new Module(); - module.setId(-1); - module.setName(NAME); - return module; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public int getProjectId() { - return projectId; - } - - public void setProjectId(int projectId) { - this.projectId = projectId; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getIntroduction() { - return introduction; - } - - public void setIntroduction(String introduction) { - this.introduction = introduction; - } - - public Project getProject() { - return project; - } - - public void setProject(Project project) { - this.project = project; - } - - public Set getPageList() { - return pageList; - } - - public void setPageList(Set pageList) { - this.pageList = pageList; - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Page.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Page.java deleted file mode 100644 index 9b1ef46691fa13e3b695d9148433f4759abc9ae9..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Page.java +++ /dev/null @@ -1,71 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -import java.util.HashSet; -import java.util.Set; - -/** - * @author yeguozhong yedaxia.github.com - */ -class Page { - - private int id; - private String name; - private String introduction; - private Module module; - private Set actionList = new HashSet(); - private String template; - - public static Page newPage(){ - Page page = new Page(); - page.setId(-1); - return page; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getIntroduction() { - return introduction; - } - - public void setIntroduction(String introduction) { - this.introduction = introduction; - } - - public Module getModule() { - return module; - } - - public void setModule(Module module) { - this.module = module; - } - - public Set getActionList() { - return actionList; - } - - public void setActionList(Set actionList) { - this.actionList = actionList; - } - - public String getTemplate() { - return template; - } - - public void setTemplate(String template) { - this.template = template; - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Parameter.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Parameter.java deleted file mode 100644 index 03d363d274df5e122e6738f62bee0a74fc9d4ccb..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Parameter.java +++ /dev/null @@ -1,134 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -import java.util.HashSet; -import java.util.Set; - -/** - * @author yeguozhong yedaxia.github.com - */ -class Parameter { - - private int id; - private String mockData; - private String name; - private String identifier; - private String identifierChange; - private String remarkChange; - private String dataType; - private String remark; - private Set actionRequestList = new HashSet(); - private Set actionResponseList = new HashSet(); - private String validator = ""; - private Set parameterList = new HashSet(); - private Set complexParamerterList = new HashSet(); - - public static Parameter newParameter(){ - Parameter p = new Parameter(); - p.setId(-1); - return p; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getMockData() { - return mockData; - } - - public void setMockData(String mockData) { - this.mockData = mockData; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getIdentifier() { - return identifier; - } - - public void setIdentifier(String identifier) { - this.identifier = identifier; - } - - public String getIdentifierChange() { - return identifierChange; - } - - public void setIdentifierChange(String identifierChange) { - this.identifierChange = identifierChange; - } - - public String getRemarkChange() { - return remarkChange; - } - - public void setRemarkChange(String remarkChange) { - this.remarkChange = remarkChange; - } - - public String getDataType() { - return dataType; - } - - public void setDataType(String dataType) { - this.dataType = dataType; - } - - public String getRemark() { - return remark; - } - - public void setRemark(String remark) { - this.remark = remark; - } - - public Set getActionRequestList() { - return actionRequestList; - } - - public void setActionRequestList(Set actionRequestList) { - this.actionRequestList = actionRequestList; - } - - public Set getActionResponseList() { - return actionResponseList; - } - - public void setActionResponseList(Set actionResponseList) { - this.actionResponseList = actionResponseList; - } - - public String getValidator() { - return validator; - } - - public void setValidator(String validator) { - this.validator = validator; - } - - public Set getParameterList() { - return parameterList; - } - - public void setParameterList(Set parameterList) { - this.parameterList = parameterList; - } - - public Set getComplexParamerterList() { - return complexParamerterList; - } - - public void setComplexParamerterList(Set complexParamerterList) { - this.complexParamerterList = complexParamerterList; - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Project.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Project.java deleted file mode 100644 index a2eea2072ea19fea4e00a6728963e895135b9959..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/Project.java +++ /dev/null @@ -1,277 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -import io.github.yedaxia.apidocs.DocContext; -import io.github.yedaxia.apidocs.IResponseWrapper; -import io.github.yedaxia.apidocs.ParseUtils; -import io.github.yedaxia.apidocs.Utils; -import io.github.yedaxia.apidocs.parser.*; - -import java.util.*; - -/** - * @author yeguozhong yedaxia.github.com - */ -class Project { - - private int id; - private int userId; - private String name; - private Date createDate; - private Date updateTime; - private String introduction; - private int workspaceModeInt; - private String relatedIds = ""; - private int groupId; - private int mockNum; - private int teamId; - private short accessType; - private Set moduleList = new HashSet(); - private String projectData; - private List memberAccountList; - private String version; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public int getUserId() { - return userId; - } - - public void setUserId(int userId) { - this.userId = userId; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Date getCreateDate() { - return createDate; - } - - public void setCreateDate(Date createDate) { - this.createDate = createDate; - } - - public Date getUpdateTime() { - return updateTime; - } - - public void setUpdateTime(Date updateTime) { - this.updateTime = updateTime; - } - - public String getIntroduction() { - return introduction; - } - - public void setIntroduction(String introduction) { - this.introduction = introduction; - } - - public int getWorkspaceModeInt() { - return workspaceModeInt; - } - - public void setWorkspaceModeInt(int workspaceModeInt) { - this.workspaceModeInt = workspaceModeInt; - } - - public String getRelatedIds() { - return relatedIds; - } - - public void setRelatedIds(String relatedIds) { - this.relatedIds = relatedIds; - } - - public int getGroupId() { - return groupId; - } - - public void setGroupId(int groupId) { - this.groupId = groupId; - } - - public int getMockNum() { - return mockNum; - } - - public void setMockNum(int mockNum) { - this.mockNum = mockNum; - } - - public int getTeamId() { - return teamId; - } - - public void setTeamId(int teamId) { - this.teamId = teamId; - } - - public short getAccessType() { - return accessType; - } - - public void setAccessType(short accessType) { - this.accessType = accessType; - } - - public Set getModuleList() { - return moduleList; - } - - public void setModuleList(Set moduleList) { - this.moduleList = moduleList; - } - - public String getProjectData() { - return projectData; - } - - public void setProjectData(String projectData) { - this.projectData = projectData; - } - - public List getMemberAccountList() { - return memberAccountList; - } - - public void setMemberAccountList(List memberAccountList) { - this.memberAccountList = memberAccountList; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - public static Project valueOf(int id, List controllerNodeList) { - Project project = new Project(); - project.setId(id); - Module module = Module.newModule(); - project.getModuleList().add(module); - - for(ControllerNode controllerNode : controllerNodeList){ - Page page = Page.newPage(); - module.getPageList().add(page); - page.setName(controllerNode.getDescription()); - - for (RequestNode requestNode : controllerNode.getRequestNodes()) { - Action action = Action.newAction(); - action.setName(requestNode.getDescription()); - String requestUrl = requestNode.getUrl(); - action.setRequestUrl(supportRestfulUrl(requestUrl)); - - List methods = requestNode.getMethod(); - action.setRequestType(ActionType.valueOf(methods.get(0)).type); - - for (ParamNode paramNode : requestNode.getParamNodes()) { - Parameter parameter = Parameter.newParameter(); - if (DataType.isArrayType(paramNode.getType())) { - parameter.setIdentifier(getArrayIdentifier(paramNode.getName())); - } else { - parameter.setIdentifier(paramNode.getName()); - } - parameter.setName(paramNode.getDescription()); - parameter.setDataType(DataType.rapTypeOfNode(paramNode.getType())); -// parameter.setRemark(DataType.mockTypeOfNode(paramNode.getType())); - action.getRequestParameterList().add(parameter); - } - - IResponseWrapper responseWrapper = DocContext.getResponseWrapper(); - Map resultMap = responseWrapper.wrapResponse(requestNode.getResponseNode()); - setResultMapToAction(resultMap, action.getResponseParameterList()); - - page.getActionList().add(action); - } - } - - return project; - } - - private static void setResultMapToAction(Map resultMap, Set parameterSet) { - for (Map.Entry entry : resultMap.entrySet()) { - Parameter parameter = Parameter.newParameter(); - parameter.setIdentifier(entry.getKey()); - if (Utils.isValueType(entry.getValue())) { - String uType = unifyType(entry.getValue()); - parameter.setDataType(DataType.rapTypeOfNode(uType)); - parameter.setRemark(DataType.mockValue(entry.getValue())); - parameterSet.add(parameter); - } else if (entry.getValue() instanceof Map) { - parameter.setDataType(DataType.OBJECT); - parameterSet.add(parameter); - setResultMapToAction((Map) entry.getValue(), parameter.getParameterList()); - } else if (entry.getValue() instanceof ResponseNode){ - ResponseNode responseNode = (ResponseNode)entry.getValue(); - if(responseNode.isList()){ - parameter.setIdentifier(getArrayIdentifier(entry.getKey())); - parameter.setDataType(DataType.ARRAY_OBJECT); - }else{ - parameter.setDataType(DataType.OBJECT); - } - parameterSet.add(parameter); - setResponseToAction(responseNode, parameter.getParameterList()); - } - } - } - - private static void setResponseToAction(ClassNode responseNode, Set parameterSet) { - for(FieldNode fieldNode : responseNode.getChildNodes()){ - Parameter parameter = Parameter.newParameter(); - parameter.setName(fieldNode.getDescription()); - MockNode mockNode = fieldNode.getMockNode(); - - if (DataType.isArrayType(fieldNode.getType())) { - parameter.setIdentifier(getArrayIdentifier(fieldNode.getName())); - } else { - parameter.setIdentifier(fieldNode.getName()); - } - parameter.setRemark(DataType.mockTypeOfNode(fieldNode.getType())); - parameter.setDataType(DataType.rapTypeOfNode(fieldNode.getType())); - - // cover - if(mockNode != null){ - if(Utils.isNotEmpty(mockNode.getLimit())){ - parameter.setIdentifier(String.format("%s|%s", fieldNode.getName(), mockNode.getLimit())); - } - if(Utils.isNotEmpty(mockNode.getValue())){ - parameter.setRemark(DataType.mockValue(mockNode.getValue())); - } - } - - parameterSet.add(parameter); - if(fieldNode.getChildNode() != null){ - setResponseToAction(fieldNode.getChildNode(), parameter.getParameterList()); - } - } - } - - private static String unifyType(Object value) { - return ParseUtils.unifyType(value.getClass().getSimpleName()); - } - - private static String getArrayIdentifier(String name){ - return String.format("%s|1-10", name); - } - - private static String supportRestfulUrl(String url){ - if(url.contains("{") && url.contains("}")){ - url = "reg:" + url.replaceAll("\\{.+?\\}", ".+"); - } - return url; - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/ProjectForm.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/ProjectForm.java deleted file mode 100644 index 5cd05b5cb4968f9e1330f9474557f4446adc2baa..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/ProjectForm.java +++ /dev/null @@ -1,53 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -/** - * @author yeguozhong yedaxia.github.com - */ -class ProjectForm { - - private Integer id; - private String projectData; - private String deletedObjectListData; - private String versionPosition; - private String description; - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getProjectData() { - return projectData; - } - - public void setProjectData(String projectData) { - this.projectData = projectData; - } - - public String getDeletedObjectListData() { - return deletedObjectListData; - } - - public void setDeletedObjectListData(String deletedObjectListData) { - this.deletedObjectListData = deletedObjectListData; - } - - public String getVersionPosition() { - return versionPosition == null ? "4" : versionPosition; - } - - public void setVersionPosition(String versionPosition) { - this.versionPosition = versionPosition; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/RapPlugin.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/RapPlugin.java new file mode 100644 index 0000000000000000000000000000000000000000..0db30b246ac5945ba0421f609ea5273d2e5f2c14 --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/RapPlugin.java @@ -0,0 +1,39 @@ +package io.github.yedaxia.apidocs.plugin.rap; + +import com.alibaba.fastjson.JSONObject; +import io.github.yedaxia.apidocs.DocsConfig; +import io.github.yedaxia.apidocs.plugin.IPluginSupport; +import io.github.yedaxia.apidocs.Utils; +import io.github.yedaxia.apidocs.parser.ControllerNode; +import io.github.yedaxia.apidocs.plugin.rap.domain.Project; +import io.github.yedaxia.apidocs.DocContext; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.List; + +/** + * post request to rap : http://rapapi.org/ + * + * @author yeguozhong yedaxia.github.com + */ +public class RapPlugin implements IPluginSupport { + @Override + public void execute(List controllerNodeList) { + DocsConfig docsConfig = DocContext.getDocsConfig(); + String rapFileName = String.format("rap-%s-%s.json", docsConfig.getProjectName(), docsConfig.getApiVersion()); + try { + File saveFile = new File(Paths.get(docsConfig.getDocsPath(), docsConfig.getApiVersion(), rapFileName).toUri()); + if (saveFile.exists()) { + saveFile.delete(); + } + Project project = Project.load(docsConfig,controllerNodeList); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("modelJSON", Utils.toJson(project)); + Utils.writeToDisk(saveFile, jsonObject.toJSONString()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/RapSupportPlugin.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/RapSupportPlugin.java deleted file mode 100644 index cfbb5875407aedc8906888be5d61fd48f1613921..0000000000000000000000000000000000000000 --- a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/RapSupportPlugin.java +++ /dev/null @@ -1,157 +0,0 @@ -package io.github.yedaxia.apidocs.plugin.rap; - -import io.github.yedaxia.apidocs.*; -import io.github.yedaxia.apidocs.http.DHttpRequest; -import io.github.yedaxia.apidocs.http.DHttpResponse; -import io.github.yedaxia.apidocs.http.DHttpUtils; -import io.github.yedaxia.apidocs.parser.ControllerNode; - -import java.io.IOException; -import java.util.*; - -/** - * post request to rap : http://rapapi.org/ - * - * @author yeguozhong yedaxia.github.com - */ -public class RapSupportPlugin implements IPluginSupport { - - private String rapHost; - private Integer projectId; // project id in rap - private String cookie; - - private List controllerNodeList; - - @Override - public void execute(List controllerNodeList) { - this.controllerNodeList = controllerNodeList; - postToRap(); - } - - /** - * do post - */ - private void postToRap(){ - - DocsConfig docsConfig = DocContext.getDocsConfig(); - if(controllerNodeList == null - || controllerNodeList.isEmpty() - || docsConfig == null - || docsConfig.getRapHost() == null - || docsConfig.getRapProjectId() == null){ - LogUtils.warn("docs config properties miss, we don't think you want to post to rap!"); - return; - } - - this.rapHost = docsConfig.getRapHost(); - this.projectId = Integer.valueOf(docsConfig.getRapProjectId()); - this.cookie = docsConfig.getRapLoginCookie(); - - if(!Utils.isNotEmpty(cookie)){ - String account = docsConfig.getRapAccount(); - String password = docsConfig.getRapPassword(); - DHttpResponse response = doLogin(loginUrl(rapHost),account, password); - this.cookie = response.getHeader("Set-Cookie"); - } - - Set moduleSet = getModuleList(); - - ProjectForm projectForm = new ProjectForm(); - projectForm.setId(projectId); - - Set deleteModuleForms = new HashSet<>(moduleSet.size()); - if(moduleSet != null && !moduleSet.isEmpty()){ - for(Module module : moduleSet){ - if(Module.NAME.equalsIgnoreCase(module.getName())){ - DeleteActionFrom delForm = new DeleteActionFrom(); - delForm.setClassName("Module"); - delForm.setId(module.getId()); - deleteModuleForms.add(delForm); - } - } - } - projectForm.setDeletedObjectListData(Utils.toJson(deleteModuleForms)); - - Project project = Project.valueOf(projectId, controllerNodeList); - projectForm.setProjectData(Utils.toJson(project)); - - postProject(projectForm); - } - - public DHttpResponse doLogin(String loginUrl, String userName, String password){ - DHttpRequest request = new DHttpRequest(); - request.setAutoRedirect(false); - request.setUrl(loginUrl); - request.addParam("account", userName); - request.addParam("password", password); - try { - return DHttpUtils.httpPost(request); - }catch (IOException ex){ - LogUtils.error("login rap fail , userName : %s, pass : %s", userName, password); - throw new RuntimeException(ex); - } - } - - private Set getModuleList(){ - try{ - DHttpResponse modelResp = DHttpUtils.httpGet(queryModelUrl(rapHost, projectId)); - if(modelResp.getCode() == 200){ - ModelResponse model = Utils.jsonToObject(modelResp.streamAsString(), ModelResponse.class); - return model.getModel().getModuleList(); - }else{ - LogUtils.error("request module data fail, rapHost : %s, projectId : %s", rapHost, projectId); - throw new RuntimeException("request module data fail , code : " + modelResp.getCode()); - } - }catch (IOException e){ - LogUtils.error("get rap models fail", e); - throw new RuntimeException(e); - } - } - - private void postProject(ProjectForm projectForm){ - DHttpRequest request = new DHttpRequest(); - request.setUrl(checkInUrl(rapHost)); - Map params = new HashMap<>(); - params.put("id", String.valueOf(projectForm.getId())); - params.put("projectData", projectForm.getProjectData()); - if(projectForm.getDeletedObjectListData() != null){ - params.put("deletedObjectListData", projectForm.getDeletedObjectListData()); - } - if(projectForm.getDescription() != null){ - params.put("description", projectForm.getDescription()); - } - if(projectForm.getVersionPosition() != null){ - params.put("versionPosition", projectForm.getVersionPosition()); - } - request.setParams(params); - - Map headers = new HashMap<>(); - headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); - headers.put("Cookie", cookie); - request.setHeaders(headers); - - try{ - DHttpResponse response = DHttpUtils.httpPost(request); - if(response.getCode() == 200){ - LogUtils.info("post project to rap success, response : %s " , response.streamAsString()); - }else{ - LogUtils.error("post project to rap fail !!! code : %s", response.streamAsString()); - } - }catch (IOException e){ - LogUtils.error("post project to rap fail", e); - throw new RuntimeException(e); - } - } - - private String queryModelUrl(String host, Integer projectId){ - return String.format("%s/api/queryModel.do?projectId=%s",host, projectId); - } - - private String checkInUrl(String host){ - return String.format("%s/workspace/checkIn.do", host); - } - - private String loginUrl(String host){ - return String.format("%s/account/doLogin.do", host); - } -} diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/ActionList.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/ActionList.java new file mode 100644 index 0000000000000000000000000000000000000000..f8ce0c7986e9ae6fdf0c4e6a79d4082e8d1aaa8a --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/ActionList.java @@ -0,0 +1,82 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.rap.domain; +import java.util.ArrayList; +import java.util.List; + +/** + * Auto-generated: 2020-10-14 14:29:54 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class ActionList { + + private int id = -1; + private String name; + private String description; + private String requestType; + private String requestUrl; + private String responseTemplate; + private List requestParameterList = new ArrayList<>(); + private List responseParameterList = new ArrayList<>(); + public void setId(int id) { + this.id = id; + } + public int getId() { + return id; + } + + public void setName(String name) { + this.name = name; + } + public String getName() { + return name; + } + + public void setDescription(String description) { + this.description = description; + } + public String getDescription() { + return description; + } + + public void setRequestType(String requestType) { + this.requestType = requestType; + } + public String getRequestType() { + return requestType; + } + + public void setRequestUrl(String requestUrl) { + this.requestUrl = requestUrl; + } + public String getRequestUrl() { + return requestUrl; + } + + public void setResponseTemplate(String responseTemplate) { + this.responseTemplate = responseTemplate; + } + public String getResponseTemplate() { + return responseTemplate; + } + + + + public void setResponseParameterList(List responseParameterList) { + this.responseParameterList = responseParameterList; + } + public List getResponseParameterList() { + return responseParameterList; + } + + public List getRequestParameterList() { + return requestParameterList; + } + + public void setRequestParameterList(List requestParameterList) { + this.requestParameterList = requestParameterList; + } +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/ModuleList.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/ModuleList.java new file mode 100644 index 0000000000000000000000000000000000000000..af64edbd1f9bc30acb0a6703d2436b71dccf8e41 --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/ModuleList.java @@ -0,0 +1,48 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.rap.domain; +import java.util.ArrayList; +import java.util.List; + +/** + * Auto-generated: 2020-10-14 14:29:54 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class ModuleList { + + private int id = -1; + private String introduction; + private String name; + private List pageList = new ArrayList<>(); + public void setId(int id) { + this.id = id; + } + public int getId() { + return id; + } + + public void setIntroduction(String introduction) { + this.introduction = introduction; + } + public String getIntroduction() { + return introduction; + } + + public void setName(String name) { + this.name = name; + } + public String getName() { + return name; + } + + public void setPageList(List pageList) { + this.pageList = pageList; + } + public List getPageList() { + return pageList; + } + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/PageList.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/PageList.java new file mode 100644 index 0000000000000000000000000000000000000000..a7191749aa24fc65032fdd07d5d4a91891308c1d --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/PageList.java @@ -0,0 +1,48 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.rap.domain; +import java.util.ArrayList; +import java.util.List; + +/** + * Auto-generated: 2020-10-14 14:29:54 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class PageList { + + private int id = -1; + private String introduction; + private String name; + private List actionList = new ArrayList<>(); + public void setId(int id) { + this.id = id; + } + public int getId() { + return id; + } + + public void setIntroduction(String introduction) { + this.introduction = introduction; + } + public String getIntroduction() { + return introduction; + } + + public void setName(String name) { + this.name = name; + } + public String getName() { + return name; + } + + public void setActionList(List actionList) { + this.actionList = actionList; + } + public List getActionList() { + return actionList; + } + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/ParameterList.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/ParameterList.java new file mode 100644 index 0000000000000000000000000000000000000000..90112466deab14957298ee428a41ca0154772628 --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/ParameterList.java @@ -0,0 +1,72 @@ +/** + * Copyright 2020 bejson.com + */ +package io.github.yedaxia.apidocs.plugin.rap.domain; +import java.util.ArrayList; +import java.util.List; + +/** + * Auto-generated: 2020-10-14 14:29:54 + * + * @author bejson.com (i@bejson.com) + * @website http://www.bejson.com/java2pojo/ + */ +public class ParameterList { + + private long id = -1; + private String identifier; + private String name; + private String remark; + private List parameterList = new ArrayList<>(); + private String validator; + private String dataType; + public void setId(long id) { + this.id = id; + } + public long getId() { + return id; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + public String getIdentifier() { + return identifier; + } + + public void setName(String name) { + this.name = name; + } + public String getName() { + return name; + } + + public void setRemark(String remark) { + this.remark = remark; + } + public String getRemark() { + return remark; + } + + public void setParameterList(List parameterList) { + this.parameterList = parameterList; + } + public List getParameterList() { + return parameterList; + } + + public void setValidator(String validator) { + this.validator = validator; + } + public String getValidator() { + return validator; + } + + public void setDataType(String dataType) { + this.dataType = dataType; + } + public String getDataType() { + return dataType; + } + +} \ No newline at end of file diff --git a/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/Project.java b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/Project.java new file mode 100644 index 0000000000000000000000000000000000000000..14f4f941a0495946adf752b27af1be892b2cd533 --- /dev/null +++ b/library/src/main/java/io/github/yedaxia/apidocs/plugin/rap/domain/Project.java @@ -0,0 +1,167 @@ +package io.github.yedaxia.apidocs.plugin.rap.domain; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.google.common.base.Strings; +import io.github.yedaxia.apidocs.DocsConfig; +import io.github.yedaxia.apidocs.ParseUtils; +import io.github.yedaxia.apidocs.Utils; +import io.github.yedaxia.apidocs.parser.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + + +public class Project { + private List moduleList = new ArrayList<>(); + + public List getModuleList() { + return moduleList; + } + + public static Project load(DocsConfig docsConfig, List controllerNodeList) { + Project project = new Project(); + /** + * moduleList + */ + ModuleList module = new ModuleList(); + module.setName(String.format("%s-%s", docsConfig.getProjectName(), docsConfig.getApiVersion())); + project.getModuleList().add(module); + for (ControllerNode controllerNode : controllerNodeList) { + /** + * pageList + */ + PageList page = new PageList(); + module.getPageList().add(page); + page.setName(Optional.ofNullable(controllerNode.getDescription()).orElse(controllerNode.getClassName())); + page.setIntroduction(controllerNode.getClassName()); + for (RequestNode requestNode : controllerNode.getRequestNodes()) { + /** + * actionList + */ + ActionList action = new ActionList(); + page.getActionList().add(action); + if(!Strings.isNullOrEmpty(requestNode.getDescription().trim())){ + action.setName(requestNode.getDescription()); + }else{ + action.setName(requestNode.getMethodName()); + } + action.setRequestUrl(supportRestfulUrl(requestNode.getUrl())); + List methods = requestNode.getMethod(); + action.setRequestType(ActionType.valueOf(methods.get(0)).type); + /** + * requestParameterList + */ + for (ParamNode paramNode : requestNode.getParamNodes()) { + ParameterList parameter = genRequestParameter(paramNode); + action.getRequestParameterList().add(parameter); + } + /** + * responseParameterList + */ + action.getResponseParameterList().add(genResponseParameter(requestNode.getResponseNode())); + } + } + return project; + } + + private static ParameterList genRequestParameter(ParamNode paramNode) { + ParameterList parameter = new ParameterList(); + if (DataType.isArrayType(paramNode.getType())) { + parameter.setIdentifier(getArrayIdentifier(paramNode.getName())); + } else { + parameter.setIdentifier(paramNode.getName()); + } + parameter.setDataType(DataType.rapTypeOfNode(paramNode.getType())); + parameter.setName(paramNode.getDescription()); + parameter.setRemark(DataType.mockValue(Utils.getDefulatValue(paramNode.getType(),""))); + if (paramNode.getJsonBody()) { + parameter.setName(""); + JSONObject jsonObject = null; + try { + /** + * 分析结果可能为空 + */ + jsonObject = JSON.parseObject(paramNode.getDescription()); + } catch (Exception e) { + return parameter; + } + parameter.getParameterList().addAll(jsontoParameter(jsonObject)); + } + return parameter; + } + + private static List jsontoParameter(JSONObject jsonObject) { + List list = new ArrayList<>(); + for (String key : jsonObject.keySet()) { + ParameterList param = new ParameterList(); + Object o = jsonObject.get(key); + if (o instanceof JSONObject) { + param.getParameterList().addAll(jsontoParameter((JSONObject) o)); + } + param.setIdentifier(key); + param.setDataType(DataType.rapTypeOfNode(o.toString())); + param.setRemark(DataType.mockValue(o)); + list.add(param); + } + return list; + } + + + /** + * + * @param responseNode + * @return + */ + private static ParameterList genResponseParameter(ClassNode responseNode) { + ParameterList root = new ParameterList(); + if (responseNode.isList()) { + root.setIdentifier(getArrayIdentifier("object")); + root.setDataType(DataType.ARRAY_OBJECT); + } else { + root.setIdentifier("object"); + root.setDataType(responseNode.getClassName()); + } + for (FieldNode fieldNode : responseNode.getChildNodes()) { + ParameterList subParam = new ParameterList(); + subParam.setName(fieldNode.getDescription()); + MockNode mockNode = fieldNode.getMockNode(); + if (DataType.isArrayType(fieldNode.getType())) { + subParam.setIdentifier(getArrayIdentifier(fieldNode.getName())); + } else { + subParam.setIdentifier(fieldNode.getName()); + } + subParam.setRemark(DataType.mockTypeOfNode(fieldNode.getType())); + subParam.setDataType(DataType.rapTypeOfNode(fieldNode.getType())); + root.getParameterList().add(subParam); + if (mockNode != null) { + if (Utils.isNotEmpty(mockNode.getLimit())) { + subParam.setIdentifier(String.format("%s|%s", fieldNode.getName(), mockNode.getLimit())); + } + if (Utils.isNotEmpty(mockNode.getValue())) { + subParam.setRemark(DataType.mockValue(mockNode.getValue())); + } + } + if (fieldNode.getChildNode() != null) { + root.getParameterList().add(genResponseParameter(fieldNode.getChildNode())); + } + } + return root; + } + + private static String unifyType(Object value) { + return ParseUtils.unifyType(value.getClass().getSimpleName()); + } + + private static String getArrayIdentifier(String name) { + return String.format("%s|1-10", name); + } + + private static String supportRestfulUrl(String url) { + if (url.contains("{") && url.contains("}")) { + url = "reg:" + url.replaceAll("\\{.+?\\}", ".+"); + } + return url; + } +} diff --git a/library/src/test/java/ControllerDocTest.java b/library/src/test/java/ControllerDocTest.java index a860bde354d187f3c5ea8e841175927c80735a5e..069c62dbb41dee1165dd42e4ea6be8f308788aaa 100644 --- a/library/src/test/java/ControllerDocTest.java +++ b/library/src/test/java/ControllerDocTest.java @@ -1,11 +1,10 @@ import io.github.yedaxia.apidocs.DocContext; -import io.github.yedaxia.apidocs.Docs; import io.github.yedaxia.apidocs.DocsConfig; import io.github.yedaxia.apidocs.Resources; -import io.github.yedaxia.apidocs.doc.HtmlControllerDocBuilder; -import io.github.yedaxia.apidocs.doc.IControllerDocBuilder; import io.github.yedaxia.apidocs.parser.ControllerNode; import io.github.yedaxia.apidocs.parser.SpringControllerParser; +import io.github.yedaxia.apidocs.plugin.html.HtmlControllerDocBuilder; +import io.github.yedaxia.apidocs.plugin.html.IControllerDocBuilder; import org.junit.Before; import org.junit.Test; diff --git a/library/src/test/java/DocsTest.java b/library/src/test/java/DocsTest.java index f82b855f2413a58d9de156387b3f7d95fb67b3a2..ef19ad1ba8a7abb43d10d995d09d535ee2ff135a 100644 --- a/library/src/test/java/DocsTest.java +++ b/library/src/test/java/DocsTest.java @@ -3,7 +3,11 @@ import io.github.yedaxia.apidocs.DocsConfig; import io.github.yedaxia.apidocs.IResponseWrapper; import io.github.yedaxia.apidocs.Resources; import io.github.yedaxia.apidocs.parser.ResponseNode; +import io.github.yedaxia.apidocs.plugin.IPluginSupport; +import io.github.yedaxia.apidocs.plugin.html.HtmlPlugin; import io.github.yedaxia.apidocs.plugin.markdown.MarkdownDocPlugin; +import io.github.yedaxia.apidocs.plugin.postman.PostmanPlugin; +import io.github.yedaxia.apidocs.plugin.rap.RapPlugin; import org.junit.Test; import java.util.HashMap; @@ -16,43 +20,52 @@ import java.util.Map; public class DocsTest { @Test - public void test_generatePlayApiDocs(){ + public void test_generatePlayApiDocs() { Resources.setDebug(); Docs.buildHtmlDocs(getDocsConfig(Projects.PlayProject)); } - @Test - public void test_generateSpringApiDocs(){ - Resources.setDebug(); - Docs.buildHtmlDocs(getDocsConfig(Projects.SpringProject)); - } @Test - public void test_generateGenericApiDocs(){ + public void test_generateGenericApiDocs() { Resources.setDebug(); Docs.buildHtmlDocs(getDocsConfig(Projects.GenericProject)); } @Test - public void test_generateJFinalApiDocs(){ - Resources.setDebug(); + public void test_generateJFinalApiDocs() { +// Resources.setDebug(); Docs.buildHtmlDocs(getDocsConfig(Projects.JFinalProject)); } + @Test - public void test_generateMultiModuleDocs(){ - Resources.setDebug(); - Docs.buildHtmlDocs(getDocsConfig("/Users/yeguozhong/Desktop/svnLibrary/jap")); + public void test_generateSpringApiDocs() { +// Resources.setDebug(); + Docs.buildHtmlDocs(getDocsConfig(Projects.SpringProject,new PostmanPlugin())); } - private DocsConfig getDocsConfig(String projectPath){ + private DocsConfig getDocsConfig(String projectPath) { + return getDocsConfig(projectPath, new HtmlPlugin()); + } + + private DocsConfig getDocsConfig(String projectPath, IPluginSupport pluginSupport) { DocsConfig config = new DocsConfig(); + config.addPlugin(pluginSupport); config.setProjectPath(projectPath); config.setAutoGenerate(Boolean.TRUE); + config.setOpenReflection(Boolean.FALSE); config.setLocale(Locale.SIMPLIFIED_CHINESE); - config.setApiVersion("V2.0"); + config.setApiVersion("V1"); config.setProjectName("ProjectName"); - config.addPlugin(new MarkdownDocPlugin()); + config.addPlugin(new PostmanPlugin()); return config; } + + @Test + public void testGeneratePostmanDocWithSpring() { + Resources.setDebug(); + Docs.buildHtmlDocs(getDocsConfig(Projects.SpringProject)); + } + }