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