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

refactor(web): swagger解析

上级 ea3f75eb
package org.matrix.autotest.swaggerEntityVo;
package org.matrix.vo.swaggerEntityVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......
package org.matrix.autotest.swaggerEntityVo.definitionsEntity;
package org.matrix.vo.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 org.matrix.vo.swaggerEntityVo.AttributeInVo;
import java.util.List;
......
package org.matrix.autotest.swaggerEntityVo.definitionsEntity;
package org.matrix.vo.swaggerEntityVo.definitionsEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......
package org.matrix.autotest.swaggerEntityVo.definitionsEntity;
package org.matrix.vo.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 org.matrix.vo.swaggerEntityVo.AttributeInVo;
/**
* 属性名称与属性内部
......
package org.matrix.autotest.swaggerEntityVo.responsesEntity;
package org.matrix.vo.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;
import org.matrix.vo.swaggerEntityVo.AttributeInVo;
/**
* @author mruny
......
package org.matrix.autotest.swaggerEntityVo.responsesEntity;
package org.matrix.vo.swaggerEntityVo.responsesEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......
package org.matrix.autotest.swaggerEntityVo.swaggerEntity;
package org.matrix.vo.swaggerEntityVo.swaggerEntity;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.ApiModel;
......@@ -6,7 +6,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.matrix.autotest.swaggerEntityVo.responsesEntity.ResponsesVo;
import org.matrix.vo.swaggerEntityVo.responsesEntity.ResponsesVo;
import java.util.List;
......
package org.matrix.autotest.swaggerEntityVo.swaggerEntity;
package org.matrix.vo.swaggerEntityVo.swaggerEntity;
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 org.matrix.vo.swaggerEntityVo.definitionsEntity.DefinitionsVo;
import java.util.List;
......
package org.matrix.autotest.swaggerEntityVo.swaggerEntity;
package org.matrix.vo.swaggerEntityVo.swaggerEntity;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.ApiModel;
......
......@@ -5,15 +5,15 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.vo.swaggerEntityVo.AttributeInVo;
import org.matrix.vo.swaggerEntityVo.definitionsEntity.AttributeOutVo;
import org.matrix.vo.swaggerEntityVo.definitionsEntity.DefinitionsVo;
import org.matrix.vo.swaggerEntityVo.definitionsEntity.PropertiesVo;
import org.matrix.vo.swaggerEntityVo.responsesEntity.CodeStatusVo;
import org.matrix.vo.swaggerEntityVo.responsesEntity.ResponsesVo;
import org.matrix.vo.swaggerEntityVo.swaggerEntity.PathVo;
import org.matrix.vo.swaggerEntityVo.swaggerEntity.SwaggerOuter;
import org.matrix.vo.swaggerEntityVo.swaggerEntity.TagSummary;
import org.matrix.exception.GlobalException;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
......@@ -48,68 +48,70 @@ public class SwaggerController {
@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"));
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()) {
String pathUrl = stringObjectEntry.getKey();
//请求方式
JSONObject pathJson = paths.getJSONObject(pathUrl);
Set<String> methodSets = pathJson.keySet();
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");
List<ResponsesVo> responsesVos = handleResponse(methodJson);
String operationId = methodJson.getString("operationId");
String deprecated = methodJson.getString("deprecated");
JSONArray consumes = methodJson.getJSONArray("consumes");
JSONArray produces = methodJson.getJSONArray("produces");
JSONArray security = methodJson.getJSONArray("security");
pathVo.setTags(tags);
pathVo.setHttpMethod(httpMethod);
pathVo.setPathUrl(pathUrl);
pathVo.setSummary(summary);
pathVo.setOperationId(operationId);
pathVo.setConsumes(consumes);
pathVo.setParameters(parameters);
pathVo.setResponses(responsesVos);
pathVo.setDeprecated(deprecated);
pathVo.setSecurity(security);
pathVo.setProduces(produces);
list.add(pathVo);
if (loadJson != null && !"".equals(loadJson)){
JSONObject swaggerJson = JSONObject.parseObject(loadJson);
String host = String.valueOf(swaggerJson.get("host"));
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()) {
String pathUrl = stringObjectEntry.getKey();
//请求方式
JSONObject pathJson = paths.getJSONObject(pathUrl);
Set<String> methodSets = pathJson.keySet();
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");
List<ResponsesVo> responsesVos = handleResponse(methodJson);
String operationId = methodJson.getString("operationId");
String deprecated = methodJson.getString("deprecated");
JSONArray consumes = methodJson.getJSONArray("consumes");
JSONArray produces = methodJson.getJSONArray("produces");
JSONArray security = methodJson.getJSONArray("security");
pathVo.setTags(tags);
pathVo.setHttpMethod(httpMethod);
pathVo.setPathUrl(pathUrl);
pathVo.setSummary(summary);
pathVo.setOperationId(operationId);
pathVo.setConsumes(consumes);
pathVo.setParameters(parameters);
pathVo.setResponses(responsesVos);
pathVo.setDeprecated(deprecated);
pathVo.setSecurity(security);
pathVo.setProduces(produces);
list.add(pathVo);
}
}
}
}
}
List<TagSummary> tagSummaryList = new ArrayList<>();
//stream取出key:tags,value:summary
Map<JSONArray, List<PathVo>> collect = list.stream()
.filter(pathVo -> pathVo != null && pathVo.getTags() != null)
.collect(Collectors.groupingBy(PathVo::getTags));
Set<JSONArray> tags = collect.keySet();
for (JSONArray tag : tags) {
TagSummary tagSummary = new TagSummary();
List<PathVo> pathVos = collect.get(tag);
tagSummary.setTag(tag);
for (PathVo pathVO : pathVos) {
pathVO.setTags(null);
List<TagSummary> tagSummaryList = new ArrayList<>();
//stream取出key:tags,value:summary
Map<JSONArray, List<PathVo>> collect = list.stream()
.filter(pathVo -> pathVo != null && pathVo.getTags() != null)
.collect(Collectors.groupingBy(PathVo::getTags));
Set<JSONArray> tags = collect.keySet();
for (JSONArray tag : tags) {
TagSummary tagSummary = new TagSummary();
List<PathVo> pathVos = collect.get(tag);
tagSummary.setTag(tag);
for (PathVo pathVO : pathVos) {
pathVO.setTags(null);
}
tagSummary.setPaths(pathVos);
tagSummaryList.add(tagSummary);
}
tagSummary.setPaths(pathVos);
tagSummaryList.add(tagSummary);
json.setTagSummaryList(tagSummaryList);
}
json.setTagSummaryList(tagSummaryList);
return JSONObject.parseObject(JSON.toJSONString(json));
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论