提交 dc6544b5 authored 作者: mry's avatar mry

feat(web): 代码优化

上级 bbe13e5e
...@@ -4,14 +4,14 @@ import com.alibaba.fastjson.JSON; ...@@ -4,14 +4,14 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature; import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.models.Swagger; import org.matrix.autotest.swaggerEntity.ParameterVo;
import org.matrix.autotest.swaggerEntity.*; import org.matrix.autotest.swaggerEntity.PathInfoVo;
import org.matrix.autotest.swaggerEntity.ResponseVo;
import org.matrix.database.entity.Environment; import org.matrix.database.entity.Environment;
import org.matrix.database.service.IEnvironmentService; import org.matrix.database.service.IEnvironmentService;
import org.matrix.database.vo.CommonPage; import org.matrix.database.vo.CommonPage;
...@@ -52,7 +52,7 @@ public class SwaggerController { ...@@ -52,7 +52,7 @@ public class SwaggerController {
* @return 封装好的接口信息 * @return 封装好的接口信息
*/ */
public static List<PathInfoVo> getPathInfo(JSONObject swaggerJson) { public static List<PathInfoVo> getPathInfo(JSONObject swaggerJson) {
Long i = 0L; long i = 0L;
JSONObject paths = swaggerJson.getJSONObject("paths"); JSONObject paths = swaggerJson.getJSONObject("paths");
String host = String.valueOf(swaggerJson.get("host")); String host = String.valueOf(swaggerJson.get("host"));
String basePath = String.valueOf(swaggerJson.get("basePath")); String basePath = String.valueOf(swaggerJson.get("basePath"));
...@@ -94,6 +94,17 @@ public class SwaggerController { ...@@ -94,6 +94,17 @@ public class SwaggerController {
return list; return list;
} }
/**
* 提取公共的$ref解析
*
* @param childMap properties中封装的json
* @return swagger中$ref值对应的value对象名称
*/
private static String getRef(JSONObject childMap) {
String childRef = childMap.getString("$ref");
return childRef.substring(14);
}
/** /**
* 解析响应数据 * 解析响应数据
* *
...@@ -138,16 +149,14 @@ public class SwaggerController { ...@@ -138,16 +149,14 @@ public class SwaggerController {
resp.setType(StringUtils.isBlank(respType) ? "object" : respType); resp.setType(StringUtils.isBlank(respType) ? "object" : respType);
resp.setRequired(respMap.getBooleanValue("required")); resp.setRequired(respMap.getBooleanValue("required"));
if (respMap.containsKey("$ref")) { if (respMap.containsKey("$ref")) {
String childRef = respMap.getString("$ref"); String childDef = getRef(respMap);
String childDef = childRef.substring(14);
JSONObject childDefJson = refMap.get(childDef); JSONObject childDefJson = refMap.get(childDef);
JSONObject childProperties = childDefJson.getJSONObject("properties"); JSONObject childProperties = childDefJson.getJSONObject("properties");
getRef(refMap, childProperties, resp, childDef, childDefJson); getRef(refMap, childProperties, resp, childDef, childDefJson);
} else if ("array".equalsIgnoreCase(respType)) { } else if ("array".equalsIgnoreCase(respType)) {
JSONObject items = respMap.getJSONObject("items"); JSONObject items = respMap.getJSONObject("items");
if (items.containsKey("$ref")) { if (items.containsKey("$ref")) {
String itemRef = items.getString("$ref"); String itemDef = getRef(items);
String itemDef = itemRef.substring(14);
JSONObject itemDefJson = refMap.get(itemDef); JSONObject itemDefJson = refMap.get(itemDef);
JSONObject childProperties = itemDefJson.getJSONObject("properties"); JSONObject childProperties = itemDefJson.getJSONObject("properties");
getRef(refMap, childProperties, resp, itemDef, itemDefJson); getRef(refMap, childProperties, resp, itemDef, itemDefJson);
...@@ -182,8 +191,7 @@ public class SwaggerController { ...@@ -182,8 +191,7 @@ public class SwaggerController {
childResp.add(resp); childResp.add(resp);
parentResp.setChildResp(childResp); parentResp.setChildResp(childResp);
if (childMap.containsKey("$ref")) { if (childMap.containsKey("$ref")) {
String childRef = childMap.getString("$ref"); String childDef = getRef(childMap);
String childDef = childRef.substring(14);
JSONObject childDefJson = refMap.get(childDef); JSONObject childDefJson = refMap.get(childDef);
JSONObject pro = childDefJson.getJSONObject("properties"); JSONObject pro = childDefJson.getJSONObject("properties");
//additionalProperties //additionalProperties
...@@ -193,8 +201,7 @@ public class SwaggerController { ...@@ -193,8 +201,7 @@ public class SwaggerController {
} else if ("array".equalsIgnoreCase(childType)) { } else if ("array".equalsIgnoreCase(childType)) {
JSONObject items = childMap.getJSONObject("items"); JSONObject items = childMap.getJSONObject("items");
if (items.containsKey("$ref")) { if (items.containsKey("$ref")) {
String itemRef = items.getString("$ref"); String itemDef = getRef(items);
String itemDef = itemRef.substring(14);
JSONObject itemDefJson = refMap.get(itemDef); JSONObject itemDefJson = refMap.get(itemDef);
JSONObject pro = itemDefJson.getJSONObject("properties"); JSONObject pro = itemDefJson.getJSONObject("properties");
if (pro != null && !itemDef.equalsIgnoreCase(parentVoName)) { if (pro != null && !itemDef.equalsIgnoreCase(parentVoName)) {
...@@ -259,8 +266,7 @@ public class SwaggerController { ...@@ -259,8 +266,7 @@ public class SwaggerController {
childParam.setType(StringUtils.isBlank(type) ? "object" : type); childParam.setType(StringUtils.isBlank(type) ? "object" : type);
childParam.setRequired(proMap.getBooleanValue("required")); childParam.setRequired(proMap.getBooleanValue("required"));
if (proMap.containsKey("$ref")) { if (proMap.containsKey("$ref")) {
String childRef = proMap.getString("$ref"); String childDef = getRef(proMap);
String childDef = childRef.substring(14);
JSONObject childDefJson = refMap.get(childDef); JSONObject childDefJson = refMap.get(childDef);
JSONObject childProperties = childDefJson.getJSONObject("properties"); JSONObject childProperties = childDefJson.getJSONObject("properties");
if (childProperties != null) { if (childProperties != null) {
...@@ -269,8 +275,7 @@ public class SwaggerController { ...@@ -269,8 +275,7 @@ public class SwaggerController {
} else if ("array".equalsIgnoreCase(type)) { } else if ("array".equalsIgnoreCase(type)) {
JSONObject items = proMap.getJSONObject("items"); JSONObject items = proMap.getJSONObject("items");
if (items.containsKey("$ref")) { if (items.containsKey("$ref")) {
String itemRef = items.getString("$ref"); String itemDef = getRef(items);
String itemDef = itemRef.substring(14);
JSONObject itemDefJson = refMap.get(itemDef); JSONObject itemDefJson = refMap.get(itemDef);
JSONObject pro = itemDefJson.getJSONObject("properties"); JSONObject pro = itemDefJson.getJSONObject("properties");
if (pro != null) { if (pro != null) {
...@@ -296,16 +301,15 @@ public class SwaggerController { ...@@ -296,16 +301,15 @@ public class SwaggerController {
for (String key : childSet) { for (String key : childSet) {
JSONObject childMap = childProperties.getJSONObject(key); JSONObject childMap = childProperties.getJSONObject(key);
ParameterVo resp = new ParameterVo(); ParameterVo resp = new ParameterVo();
resp.setName(key);
resp.setDescription(childMap.getString("description")); resp.setDescription(childMap.getString("description"));
String childType = childMap.getString("type"); String childType = childMap.getString("type");
resp.setName(key);
resp.setType(StringUtils.isNotBlank(childType) ? childType : childJson.getString("type")); resp.setType(StringUtils.isNotBlank(childType) ? childType : childJson.getString("type"));
resp.setRequired(childMap.getBooleanValue("required")); resp.setRequired(childMap.getBooleanValue("required"));
paramList.add(resp); paramList.add(resp);
parentResp.setChildParam(paramList); parentResp.setChildParam(paramList);
if (childMap.containsKey("$ref")) { if (childMap.containsKey("$ref")) {
String childRef = childMap.getString("$ref"); String childDef = getRef(childMap);
String childDef = childRef.substring(14);
JSONObject childDefJson = refMap.get(childDef); JSONObject childDefJson = refMap.get(childDef);
JSONObject pro = childDefJson.getJSONObject("properties"); JSONObject pro = childDefJson.getJSONObject("properties");
//additionalProperties //additionalProperties
...@@ -315,8 +319,7 @@ public class SwaggerController { ...@@ -315,8 +319,7 @@ public class SwaggerController {
} else if ("array".equalsIgnoreCase(childType)) { } else if ("array".equalsIgnoreCase(childType)) {
JSONObject items = childMap.getJSONObject("items"); JSONObject items = childMap.getJSONObject("items");
if (items.containsKey("$ref")) { if (items.containsKey("$ref")) {
String itemRef = items.getString("$ref"); String itemDef = getRef(items);
String itemDef = itemRef.substring(14);
JSONObject itemDefJson = refMap.get(itemDef); JSONObject itemDefJson = refMap.get(itemDef);
JSONObject pro = itemDefJson.getJSONObject("properties"); JSONObject pro = itemDefJson.getJSONObject("properties");
if (pro != null && !itemDef.equalsIgnoreCase(parentVoName)) { if (pro != null && !itemDef.equalsIgnoreCase(parentVoName)) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论