Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
shalousun committed May 5, 2021
2 parents 9cc28b2 + a696a24 commit 52811aa
Show file tree
Hide file tree
Showing 13 changed files with 503 additions and 214 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## smart-doc版本
版本小于1.0都属于试用,正式1.0起始发布将会等到文中提到的问题解决后才发布。
#### 版本号:2.1.5
- 更新日期: 2020-05-05
- 更新内容:
1. 修复requestBodyAdvice请求样例丢之。
2. 添加dubbo文档到torna的推送。
#### 版本号:2.1.4
- 更新日期: 2020-04-24
- 更新内容:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>smart-doc</artifactId>
<packaging>jar</packaging>
<version>2.1.4</version>
<version>2.1.5</version>

<name>smart-doc</name>
<url>https://github.com/smart-doc-group/smart-doc.git</url>
Expand Down
196 changes: 5 additions & 191 deletions src/main/java/com/power/doc/builder/TornaBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.power.doc.model.*;
import com.power.doc.model.torna.*;
import com.power.doc.template.SpringBootDocBuildTemplate;
import com.power.doc.utils.TornaUtil;
import com.thoughtworks.qdox.JavaProjectBuilder;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -47,6 +48,7 @@

import static com.power.doc.constants.TornaConstants.CATEGORY_CREATE;
import static com.power.doc.constants.TornaConstants.PUSH;
import static com.power.doc.utils.TornaUtil.*;


/**
Expand Down Expand Up @@ -87,212 +89,24 @@ public static void buildApiDoc(ApiConfig config, JavaProjectBuilder javaProjectB
* @param apiConfig ApiConfig
*/
public static void buildTorna(List<ApiDoc> apiDocs, ApiConfig apiConfig) {

//是否设置测试环境
boolean hasDebugEnv = StringUtils.isNotBlank(apiConfig.getDebugEnvName())
&&
StringUtils.isNotBlank(apiConfig.getDebugEnvUrl());

if (apiConfig.isTornaDebug()) {
String sb = "配置信息列表: \n" +
"OpenUrl: " +
apiConfig.getOpenUrl() +
"\n" +
"appToken: " +
apiConfig.getAppToken() +
"\n" +
"appKey: " +
apiConfig.getAppKey() +
"\n" +
"Secret: " +
apiConfig.getSecret() +
"\n";
System.out.println(sb);
}
TornaApi tornaApi = new TornaApi();
//设置测试环境
List<DebugEnv> debugEnvs = new ArrayList<>();
if (hasDebugEnv) {
DebugEnv debugEnv = new DebugEnv();
debugEnv.setName(apiConfig.getDebugEnvName());
debugEnv.setUrl(apiConfig.getDebugEnvUrl());
debugEnvs.add(debugEnv);

}
//
Apis api;
List<Apis> apisList = new ArrayList<>();
//添加接口数据
for (ApiDoc a : apiDocs) {
api = new Apis();
api.setName(StringUtils.isBlank(a.getDesc()) ? a.getName() : a.getDesc());
api.setItems(buildApis(a, hasDebugEnv));
api.setItems(buildApis(a.getList(), TornaUtil.setDebugEnv(apiConfig,tornaApi)));
api.setIsFolder(TornaConstants.YES);
apisList.add(api);
}
tornaApi.setDebugEnvs(debugEnvs);
tornaApi.setApis(apisList);
//推送文档信息
Map<String, String> requestJson =
TornaConstants.buildParams(PUSH, new Gson().toJson(tornaApi), apiConfig);
Map<String, String> requestJson = TornaConstants.buildParams(PUSH, new Gson().toJson(tornaApi), apiConfig);
//获取返回结果
String responseMsg = OkHttp3Util.syncPostJson(apiConfig.getOpenUrl(), new Gson().toJson(requestJson));
//开启调试时打印请求信息
if (apiConfig.isTornaDebug()) {
JsonElement element = JsonParser.parseString(responseMsg);
TornaRequestInfo info = new TornaRequestInfo()
.of()
.setCategory(PUSH)
.setCode(element.getAsJsonObject().get(TornaConstants.CODE).getAsString())
.setMessage(element.getAsJsonObject().get(TornaConstants.MESSAGE).getAsString())
.setRequestInfo(requestJson)
.setResponseInfo(responseMsg);
System.out.println(info.buildInfo());
}
TornaUtil.printDebugInfo(apiConfig,responseMsg,requestJson);
}

/**
* build apis
* @param a api
* @param hasDebugEnv has debug environment
* @return List of Api
*/
public static List<Apis> buildApis(ApiDoc a, boolean hasDebugEnv) {
List<ApiMethodDoc> apiMethodDocs = a.getList();
//参数列表
List<Apis> apis = new ArrayList<>();
Apis methodApi;
//遍历分类接口
for (ApiMethodDoc apiMethodDoc : apiMethodDocs) {
/**
* "name": "获取商品信息",
* "description": "获取商品信息",
* "url": "/goods/get",
* "httpMethod": "GET",
* "contentType": "application/json",
* "isFolder": "1",
* "parentId": "",
* "isShow": "1",
*/
methodApi = new Apis();
methodApi.setIsFolder(TornaConstants.NO);
methodApi.setName(apiMethodDoc.getDesc());
methodApi.setUrl(hasDebugEnv ? apiMethodDoc.getPath() : apiMethodDoc.getUrl());
methodApi.setHttpMethod(apiMethodDoc.getType());
methodApi.setContentType(apiMethodDoc.getContentType());
methodApi.setDescription(apiMethodDoc.getDetail());
methodApi.setIsShow(TornaConstants.YES);

/**
* {
* "name": "goodsName",
* "type": "string",
* "required": "1",
* "maxLength": "128",
* "example": "iphone12",
* "description": "商品名称描述",
* "parentId": "",
* "enumInfo": {
* "name": "支付枚举",
* "description": "支付状态",
* "items": [
* {
* "name": "WAIT_PAY",
* "type": "string",
* "value": "0",
* "description": "未支付"
* }
* ]
* }
* }
*/
methodApi.setHeaderParams(buildHerder(apiMethodDoc.getRequestHeaders()));
methodApi.setResponseParams(buildParams(apiMethodDoc.getResponseParams()));
//Path
if (CollectionUtil.isNotEmpty(apiMethodDoc.getPathParams())) {
methodApi.setPathParams(buildParams(apiMethodDoc.getPathParams()));
}
//formData
if (CollectionUtil.isNotEmpty(apiMethodDoc.getQueryParams())) {
methodApi.setRequestParams(buildParams(apiMethodDoc.getQueryParams()));
}
//Json
if (CollectionUtil.isNotEmpty(apiMethodDoc.getRequestParams())) {
methodApi.setRequestParams(buildParams(apiMethodDoc.getRequestParams()));
}
apis.add(methodApi);
}
return apis;
}

/**
* build request header
*
* @param apiReqHeaders 请求头参数列表
* @return List of HttpParam
*/
public static List<HttpParam> buildHerder(List<ApiReqHeader> apiReqHeaders) {
/**
* name": "token",
* "required": "1",
* "example": "iphone12",
* "description": "商品名称描述"
*/
HttpParam httpParam;
List<HttpParam> headers = new ArrayList<>();
for (ApiReqHeader header : apiReqHeaders) {
httpParam = new HttpParam();
httpParam.setName(header.getName());
httpParam.setRequired(header.isRequired() ? TornaConstants.YES : TornaConstants.NO);
httpParam.setExample(StringUtil.removeQuotes(header.getValue()));
httpParam.setDescription(header.getDesc());
headers.add(httpParam);
}
return headers;
}

/**
* build request response params
*
* @param apiParams 参数列表
* @return List of HttpParam
*/
public static List<HttpParam> buildParams(List<ApiParam> apiParams) {
HttpParam httpParam;
List<HttpParam> bodies = new ArrayList<>();
/**
* "name": "goodsName",
* "type": "string",
* "required": "1",
* "maxLength": "128",
* "example": "iphone12",
* "description": "商品名称描述",
* "parentId": "",
* "enumInfo": {
* "name": "支付枚举",
* "description": "支付状态",
* "items": [
* {
* "name": "WAIT_PAY",
* "type": "string",
* "value": "0",
* "description": "未支付"
*/
for (ApiParam apiParam : apiParams) {
httpParam = new HttpParam();
httpParam.setName(apiParam.getField());
httpParam.setMaxLength(apiParam.getMaxLength());
httpParam.setType(apiParam.getType());
httpParam.setRequired(apiParam.isRequired() ? TornaConstants.YES : TornaConstants.NO);
httpParam.setExample(StringUtil.removeQuotes(apiParam.getValue()));
httpParam.setDescription(apiParam.getDesc());
if (apiParam.getChildren() != null) {
httpParam.setChildren(buildParams(apiParam.getChildren()));
}
bodies.add(httpParam);
}
return bodies;
}

}

83 changes: 83 additions & 0 deletions src/main/java/com/power/doc/builder/rpc/RpcTornaBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.power.doc.builder.rpc;

import com.google.gson.Gson;
import com.power.common.util.OkHttp3Util;
import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.constants.TornaConstants;
import com.power.doc.model.ApiConfig;
import com.power.doc.model.rpc.RpcApiDoc;
import com.power.doc.model.torna.Apis;
import com.power.doc.model.torna.DubboInfo;
import com.power.doc.model.torna.TornaApi;
import com.power.doc.template.RpcDocBuildTemplate;
import com.power.doc.utils.TornaUtil;
import com.thoughtworks.qdox.JavaProjectBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.power.doc.constants.TornaConstants.PUSH;
import static com.power.doc.utils.TornaUtil.buildDubboApis;

/**
* @author xingzi 2021/4/28 16:14
**/
public class RpcTornaBuilder {

/**
* build controller api
*
* @param config config
*/
public static void buildApiDoc(ApiConfig config) {
JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();
buildApiDoc(config, javaProjectBuilder);
}


/**
* Only for smart-doc maven plugin and gradle plugin.
*
* @param config ApiConfig
* @param javaProjectBuilder ProjectDocConfigBuilder
*/
public static void buildApiDoc(ApiConfig config, JavaProjectBuilder javaProjectBuilder) {
config.setParamsDataToTree(true);
RpcDocBuilderTemplate builderTemplate = new RpcDocBuilderTemplate();
builderTemplate.checkAndInit(config);
ProjectDocConfigBuilder configBuilder = new ProjectDocConfigBuilder(config, javaProjectBuilder);
RpcDocBuildTemplate docBuildTemplate = new RpcDocBuildTemplate();
List<RpcApiDoc> apiDocList = docBuildTemplate.getApiData(configBuilder);
buildTorna(apiDocList, config);
}

public static void buildTorna(List<RpcApiDoc> apiDocs, ApiConfig apiConfig) {
TornaApi tornaApi = new TornaApi();
Apis api;
List<Apis> apisList = new ArrayList<>();
//添加接口数据
for (RpcApiDoc a : apiDocs) {
api = new Apis();
api.setName(StringUtils.isBlank(a.getDesc()) ? a.getName() : a.getDesc());
TornaUtil.setDebugEnv(apiConfig,tornaApi);
api.setItems(buildDubboApis(a.getList()));
api.setIsFolder(TornaConstants.YES);
api.setDubboInfo(new DubboInfo().builder()
.setAuthor(a.getAuthor())
.setProtocol(a.getProtocol())
.setVersion(a.getVersion())
.setDependency(TornaUtil.buildDependencies(apiConfig.getRpcApiDependencies()))
.setInterfaceName(a.getName()));
apisList.add(api);
}
tornaApi.setApis(apisList);
//推送文档信息
Map<String, String> requestJson = TornaConstants.buildParams(PUSH, new Gson().toJson(tornaApi), apiConfig);
//获取返回结果
String responseMsg = OkHttp3Util.syncPostJson(apiConfig.getOpenUrl(), new Gson().toJson(requestJson));
//开启调试时打印请求信息
TornaUtil.printDebugInfo(apiConfig,responseMsg,requestJson);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/power/doc/model/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public class ApiConfig {
/**
* torna调试开关
*/
private boolean tornaDebug;
private boolean tornaDebug = true;
/**
* 是否为dubbo
*/
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/power/doc/model/rpc/RpcApiDependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@ public RpcApiDependency setVersion(String version) {
this.version = version;
return this;
}

@Override
public String toString() {
return "<dependency>"+"\r"+
"\t"+"<groupId>" + groupId + "</groupId>" +"\n"+
"\t"+"<artifactId>" + artifactId + "</artifactId>" +"\n"+
"\t"+ "<version>" + version + "</version>" +""+
"\r"+"</dependency>";
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/power/doc/model/torna/Apis.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class Apis {
private String isFolder;
private String parentId;
private String isShow;
private DubboInfo dubboInfo;
private List<HttpParam> headerParams;
private List<HttpParam> pathParams;
private List<HttpParam> requestParams;
Expand Down Expand Up @@ -153,6 +154,14 @@ public List<Apis> getItems() {
return items;
}

public DubboInfo getDubboInfo() {
return dubboInfo;
}

public void setDubboInfo(DubboInfo dubboInfo) {
this.dubboInfo = dubboInfo;
}

public void setItems(List<Apis> items) {
this.items = items;
}
Expand Down
Loading

0 comments on commit 52811aa

Please sign in to comment.