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

refactor(web): swagger解析

上级 c03fc597
......@@ -3,13 +3,19 @@ package org.matrix.autotest.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.matrix.autotest.swaggerEntity.PathVo;
import org.matrix.autotest.swaggerEntity.SwaggerOuter;
import org.matrix.autotest.swaggerEntity.TagSummary;
import org.matrix.autotest.swaggerEntityVo.AttributeInVo;
import org.matrix.autotest.swaggerEntityVo.definitionsEntity.AttributeOutVo;
import org.matrix.autotest.swaggerEntityVo.definitionsEntity.DefinitionsVo;
import org.matrix.autotest.swaggerEntityVo.definitionsEntity.PropertiesVo;
import org.matrix.autotest.swaggerEntityVo.responsesEntity.CodeStatusVo;
import org.matrix.autotest.swaggerEntityVo.responsesEntity.ResponsesVo;
import org.matrix.autotest.swaggerEntityVo.swaggerEntity.PathVo;
import org.matrix.autotest.swaggerEntityVo.swaggerEntity.SwaggerOuter;
import org.matrix.autotest.swaggerEntityVo.swaggerEntity.TagSummary;
import org.matrix.exception.GlobalException;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.io.BufferedReader;
......@@ -36,49 +42,21 @@ import java.util.stream.Collectors;
public class SwaggerController {
/**
* 读取swagger地址里的JSON信息
*
* @param url swagger地址
* @return swagger中的JSON数据
*/
public String loadJson(String url) {
StringBuilder json = new StringBuilder();
try {
URL urlObject = new URL(url);
URLConnection uc = urlObject.openConnection();
InputStream inputStream = uc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
json.append(inputLine);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
throw new GlobalException(String.format("请求swagger数据失败,您读的swagger地址ip为 %s", url));
}
return json.toString();
}
/**
* 解析所有的接口
*
* @param url swagger中ip:端口/v2/api-docs地址
* @return 封装好的接口信息
* 解析swaggerJson
*/
@GetMapping
@ApiOperation("解析swagger")
public JSONObject getPaths(@RequestParam String url) {
@ApiOperation("获取并解析swagger数据")
public JSONObject handleSwagger(@RequestParam String url) {
String loadJson = loadJson(url);
JSONObject swaggerJson = JSONObject.parseObject(loadJson);
SwaggerOuter json = new SwaggerOuter();
String host = String.valueOf(swaggerJson.get("host"));
String basePath = String.valueOf(swaggerJson.get("basePath"));
JSONObject paths = swaggerJson.getJSONObject("paths");
JSONObject definitions = swaggerJson.getJSONObject("definitions");
json.setHost(host);
String basePath = String.valueOf(swaggerJson.get("basePath"));
json.setBasePath(basePath);
List<DefinitionsVo> definitions = handleDefinitions(swaggerJson);
json.setDefinitions(definitions);
JSONObject paths = swaggerJson.getJSONObject("paths");
List<PathVo> list = new ArrayList<>();
if (paths != null) {
for (Map.Entry<String, Object> stringObjectEntry : paths.entrySet()) {
......@@ -86,14 +64,14 @@ public class SwaggerController {
//请求方式
JSONObject pathJson = paths.getJSONObject(pathUrl);
Set<String> methodSets = pathJson.keySet();
if (CollectionUtils.isNotEmpty(methodSets)) {
if (!CollectionUtils.isEmpty(methodSets)) {
for (String httpMethod : methodSets) {
PathVo pathVo = new PathVo();
JSONObject methodJson = pathJson.getJSONObject(httpMethod);
JSONArray tags = methodJson.getJSONArray("tags");
String summary = methodJson.getString("summary");
JSONArray parameters = methodJson.getJSONArray("parameters");
JSONObject responses = methodJson.getJSONObject("responses");
List<ResponsesVo> responsesVos = handleResponse(methodJson);
String operationId = methodJson.getString("operationId");
String deprecated = methodJson.getString("deprecated");
JSONArray consumes = methodJson.getJSONArray("consumes");
......@@ -106,7 +84,7 @@ public class SwaggerController {
pathVo.setOperationId(operationId);
pathVo.setConsumes(consumes);
pathVo.setParameters(parameters);
pathVo.setResponses(responses);
pathVo.setResponses(responsesVos);
pathVo.setDeprecated(deprecated);
pathVo.setSecurity(security);
pathVo.setProduces(produces);
......@@ -135,4 +113,231 @@ public class SwaggerController {
return JSONObject.parseObject(JSON.toJSONString(json));
}
/**
* 读取swagger地址里的JSON信息
*
* @param url swagger地址
* @return swagger中的JSON数据
*/
public String loadJson(String url) {
StringBuilder json = new StringBuilder();
try {
URL urlObject = new URL(url);
URLConnection uc = urlObject.openConnection();
InputStream inputStream = uc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
json.append(inputLine);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
throw new GlobalException(String.format("请求swagger数据失败,您读的swagger地址ip为 %s", url));
}
return json.toString();
}
/**
* 处理responses中的数据
*/
public List<ResponsesVo> handleResponse(JSONObject methodJson) {
List<ResponsesVo> list = new ArrayList<>();
JSONObject responses = methodJson.getJSONObject("responses");
if (responses != null) {
//所有状态码
Set<String> codes = responses.keySet();
for (String code : codes) {
ResponsesVo responsesVo = new ResponsesVo();
JSONObject responseInside = responses.getJSONObject(code);
responsesVo.setCode(code);
CodeStatusVo codeStatusVo = new CodeStatusVo();
//获取描述
String description = responseInside.getString("description");
codeStatusVo.setDescription(description);
if ("200".equals(code)) {
JSONObject schema = responseInside.getJSONObject("schema");
AttributeInVo attributeInVo = handleItems(schema);
codeStatusVo.setAttributeInVo(attributeInVo);
} else {
//非200时状态码
responsesVo.setCodeStatusVo(codeStatusVo);
}
responsesVo.setCodeStatusVo(codeStatusVo);
list.add(responsesVo);
}
}
return list;
}
/**
* 解析host解析definitions
*/
public List<DefinitionsVo> handleDefinitions(JSONObject swaggerJson) {
//多个definitions,最外层
List<DefinitionsVo> list = new ArrayList<>();
//取到definitions内部的对象
JSONObject definitions = swaggerJson.getJSONObject("definitions");
if (definitions != null) {
//对象名称集合
Set<String> objNames = definitions.keySet();
//获取到每个对象
for (String objName : objNames) {
//List内的最外层结构
DefinitionsVo definitionsVo = new DefinitionsVo();
//definitionsVo的内部是attributeVo
AttributeOutVo attributeOutVo = new AttributeOutVo();
//根据对象名称可以取到,所有对象内部的信息,包括对象的类型type,properties,对象名称title,以及描述description
JSONObject objInside = definitions.getJSONObject(objName);
String type = objInside.getString("type");
String title = objInside.getString("title");
String description = objInside.getString("description");
attributeOutVo.setType(type);
attributeOutVo.setTitle(title);
attributeOutVo.setDescription(description);
//取到每个对象的properties
JSONObject properties = objInside.getJSONObject("properties");
if (properties != null) {
List<PropertiesVo> propertiesVoList = handleProperties(properties);
attributeOutVo.setPropertiesVoList(propertiesVoList);
}
//可能出现直接就是additionalProperties,
JSONObject additionalProperties = objInside.getJSONObject("additionalProperties");
if (additionalProperties != null) {
AttributeInVo attributeInVo = handleItems(additionalProperties);
attributeOutVo.setAdditionalProperties(attributeInVo);
}
definitionsVo.setObjName(objName);
definitionsVo.setAttributeOutVo(attributeOutVo);
list.add(definitionsVo);
}
}
return list;
}
/**
* 处理definitions内第一层的properties
*/
public List<PropertiesVo> handleProperties(JSONObject properties) {
List<PropertiesVo> list = new ArrayList<>();
if (properties != null) {
Set<String> attributeNames = properties.keySet();
//每个对象中,可能存在多个属性
for (String attributeName : attributeNames) {
//获取到对象中属性的类型等信息
JSONObject attributeInside = properties.getJSONObject(attributeName);
//处理properties以及items
AttributeInVo attributeInVo = handleItems(attributeInside);
PropertiesVo propertiesVo = new PropertiesVo();
propertiesVo.setName(attributeName);
propertiesVo.setAttributeInVo(attributeInVo);
list.add(propertiesVo);
}
}
return list;
}
/**
* 处理items
*/
public AttributeInVo handleItems(JSONObject attributeInside) {
AttributeInVo attributeInVo = new AttributeInVo();
if (attributeInside != null) {
//获取到属性的类型,根据不同的类型进行处理
String type = attributeInside.getString("type");
if (type != null && !"".equals(type)) {
switch (type) {
case "string":
attributeInVo = handleString(attributeInside);
attributeInVo.setType(type);
break;
case "integer":
attributeInVo = handleInteger(attributeInside);
attributeInVo.setType(type);
case "object":
attributeInVo = handleObject(attributeInside);
attributeInVo.setType(type);
break;
case "array":
attributeInVo = handleArray(attributeInside);
attributeInVo.setType(type);
break;
case "boolean":
attributeInVo.setType(type);
break;
default:
break;
}
} else {
String ref = attributeInside.getString("$ref");
if (ref != null) {
attributeInVo.setRef(ref);
}
}
}
return attributeInVo;
}
/**
* 处理String类型的情况
*/
public AttributeInVo handleString(JSONObject attributeInside) {
AttributeInVo attributeInVo = new AttributeInVo();
JSONArray anEnum = attributeInside.getJSONArray("enum");
String description = attributeInside.getString("description");
attributeInVo.setDescription(description);
if (anEnum != null) {
List<String> enumTypes = anEnum.toJavaList(String.class);
attributeInVo.setEnumType(enumTypes);
}
return attributeInVo;
}
/**
* 处理integer类型的情况
*/
public AttributeInVo handleInteger(JSONObject attributeInside) {
AttributeInVo attributeInVo = new AttributeInVo();
String description = attributeInside.getString("description");
attributeInVo.setDescription(description);
String format = attributeInside.getString("format");
attributeInVo.setFormat(format);
return attributeInVo;
}
/**
* 处理array类型的情况
*/
public AttributeInVo handleArray(JSONObject attributeInside) {
AttributeInVo attributeInVo = new AttributeInVo();
JSONArray example = attributeInside.getJSONArray("example");
String description = attributeInside.getString("description");
attributeInVo.setDescription(description);
if (example != null) {
List<String> javaList = example.toJavaList(String.class);
attributeInVo.setExample(javaList);
}
JSONObject items = attributeInside.getJSONObject("items");
if (items != null) {
AttributeInVo child = handleItems(items);
attributeInVo.setItems(child);
}
return attributeInVo;
}
/**
* 处理object类型的情况
*/
public AttributeInVo handleObject(JSONObject attributeInside) {
AttributeInVo attributeInVo = new AttributeInVo();
String description = attributeInside.getString("description");
attributeInVo.setDescription(description);
JSONObject additionalProperties = attributeInside.getJSONObject("additionalProperties");
if (additionalProperties != null) {
AttributeInVo child = handleItems(additionalProperties);
attributeInVo.setItems(child);
}
return attributeInVo;
}
}
\ No newline at end of file
package org.matrix.autotest.swaggerEntityVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* 内部属性,对象的字段的属性包括 : 类型,描述等
*
* @author mruny
* @create 2022/7/8 15:20:32
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("AttributeInVo内部属性,对象的字段的属性包括 : 类型,描述等")
public class AttributeInVo {
@ApiModelProperty("类型")
private String type;
@ApiModelProperty("描述")
private String description;
@ApiModelProperty("格式,可能为null")
private String format;
@ApiModelProperty("枚举,可能为null")
private List<String> enumType;
@ApiModelProperty("map类型出现的情况")
private String additionalProperties;
@ApiModelProperty("实例")
private List<String> example;
@ApiModelProperty("$ref")
private String ref;
@ApiModelProperty("子类")
private AttributeInVo items;
}
package org.matrix.autotest.swaggerEntityVo.definitionsEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.matrix.autotest.swaggerEntityVo.AttributeInVo;
import java.util.List;
/**
* 外部属性,对象中的属性,以及对象中属性的类型描述等
*
* @author mruny
* @create 2022/7/8 14:34:05
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("AttributeOutVo外部属性,对象中的属性,以及对象中属性的类型描述等")
public class AttributeOutVo {
@ApiModelProperty("类型")
private String type;
@ApiModelProperty("描述")
private String description;
@ApiModelProperty("对象名称")
private String title;
@ApiModelProperty("具体的参数详情")
private List<PropertiesVo> propertiesVoList;
@ApiModelProperty("map")
private AttributeInVo additionalProperties;
}
package org.matrix.autotest.swaggerEntityVo.definitionsEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* definitions,对象,以及对象中的属性及具体信息
*
* @author mruny
* @create 2022/7/8 14:31:28
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("DefinitionsVo对象,以及对象中的属性及具体信息")
public class DefinitionsVo {
@ApiModelProperty("对象名称")
private String objName;
@ApiModelProperty("对象中的属性以及具体信息")
private AttributeOutVo attributeOutVo;
}
package org.matrix.autotest.swaggerEntityVo.definitionsEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.matrix.autotest.swaggerEntityVo.AttributeInVo;
/**
* 属性名称与属性内部
*
* @author mruny
* @create 2022/7/8 13:55:05
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("PropertiesVo属性名称与属性内部")
public class PropertiesVo {
@ApiModelProperty("属性名称")
private String name;
@ApiModelProperty("属性对应的类型,以及描述等")
private AttributeInVo attributeInVo;
}
package org.matrix.autotest.swaggerEntityVo.responsesEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.matrix.autotest.swaggerEntityVo.AttributeInVo;
/**
* @author mruny
* @create 2022/7/11 14:13:55
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("状态码对应具体信息")
public class CodeStatusVo {
@ApiModelProperty("描述")
private String description;
@ApiModelProperty("对象中的属性以及具体信息")
private AttributeInVo attributeInVo;
}
package org.matrix.autotest.swaggerEntityVo.responsesEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* responses最外层
*
* @author mruny
* @create 2022/7/11 14:07:25
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("response返回值信息")
public class ResponsesVo {
@ApiModelProperty("状态码")
private String code;
@ApiModelProperty("状态信息")
private CodeStatusVo codeStatusVo;
}
package org.matrix.autotest.swaggerEntity;
package org.matrix.autotest.swaggerEntityVo.swaggerEntity;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.matrix.autotest.swaggerEntityVo.responsesEntity.ResponsesVo;
import java.util.List;
/**
* @author MRY
......@@ -14,7 +16,7 @@ import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("接口信息")
@ApiModel("PathVo接口信息")
public class PathVo {
@ApiModelProperty("url地址")
......@@ -33,7 +35,7 @@ public class PathVo {
private JSONArray parameters;
@ApiModelProperty("返回值")
private JSONObject responses;
private List<ResponsesVo> responses;
@ApiModelProperty("operationId")
private String operationId;
......
package org.matrix.autotest.swaggerEntity;
package org.matrix.autotest.swaggerEntityVo.swaggerEntity;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.matrix.autotest.swaggerEntityVo.definitionsEntity.DefinitionsVo;
import java.util.List;
......@@ -26,7 +26,7 @@ public class SwaggerOuter {
private String basePath;
@ApiModelProperty("参数")
private JSONObject definitions;
private List<DefinitionsVo> definitions;
@ApiModelProperty("一级标签和二级标签集合")
private List<TagSummary> tagSummaryList;
......
package org.matrix.autotest.swaggerEntity;
package org.matrix.autotest.swaggerEntityVo.swaggerEntity;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.ApiModel;
......@@ -16,7 +16,7 @@ import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("一级标签与二级标签")
@ApiModel("TagSummary一级标签与二级标签")
public class TagSummary {
@ApiModelProperty("一级标签")
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论