fix (接口定义): 修复导入导出Swagger问题
--bug=1007229 --user=赵勇 [github#6835]接口导出后导入,响应内容中的描述信息没有了,同时请求参数中的值没有了 https://www.tapd.cn/55049933/s/1056772
This commit is contained in:
parent
18848277f4
commit
245af574e0
|
@ -46,7 +46,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
if (StringUtils.isBlank(request.getSwaggerUrl())) {
|
if (StringUtils.isBlank(request.getSwaggerUrl())) {
|
||||||
sourceStr = getApiTestStr(source);
|
sourceStr = getApiTestStr(source);
|
||||||
}
|
}
|
||||||
return parse(sourceStr, request);
|
return parse(sourceStr, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ApiDefinitionImport parse(String sourceStr, ApiTestImportRequest request) {
|
public ApiDefinitionImport parse(String sourceStr, ApiTestImportRequest request) {
|
||||||
|
@ -108,11 +108,11 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
Operation operation = operationsMap.get(method);
|
Operation operation = operationsMap.get(method);
|
||||||
if (operation != null) {
|
if (operation != null) {
|
||||||
MsHTTPSamplerProxy request = buildRequest(operation, pathName, method);
|
MsHTTPSamplerProxy request = buildRequest(operation, pathName, method);
|
||||||
ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method,importRequest);
|
ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method, importRequest);
|
||||||
parseParameters(operation, request);
|
parseParameters(operation, request);
|
||||||
parseRequestBody(operation.getRequestBody(), request.getBody());
|
parseRequestBody(operation.getRequestBody(), request.getBody());
|
||||||
addBodyHeader(request);
|
addBodyHeader(request);
|
||||||
if(request.getBody().getKvs().size() > 1 && request.getBody().getKvs().get(0).getName() == null) {
|
if (request.getBody().getKvs().size() > 1 && request.getBody().getKvs().get(0).getName() == null) {
|
||||||
request.getBody().getKvs().remove(0);
|
request.getBody().getKvs().remove(0);
|
||||||
} // 有数据的话,去掉 Kvs 里初始化的第一个全 null 的数据,否则有空行
|
} // 有数据的话,去掉 Kvs 里初始化的第一个全 null 的数据,否则有空行
|
||||||
apiDefinition.setRequest(JSON.toJSONString(request));
|
apiDefinition.setRequest(JSON.toJSONString(request));
|
||||||
|
@ -129,7 +129,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ApiDefinitionWithBLOBs buildApiDefinition(String id, Operation operation, String path, String method,ApiTestImportRequest importRequest) {
|
private ApiDefinitionWithBLOBs buildApiDefinition(String id, Operation operation, String path, String method, ApiTestImportRequest importRequest) {
|
||||||
String name = "";
|
String name = "";
|
||||||
if (StringUtils.isNotBlank(operation.getSummary())) {
|
if (StringUtils.isNotBlank(operation.getSummary())) {
|
||||||
name = operation.getSummary();
|
name = operation.getSummary();
|
||||||
|
@ -138,7 +138,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
} else {
|
} else {
|
||||||
name = path;
|
name = path;
|
||||||
}
|
}
|
||||||
return buildApiDefinition(id, name, path, method,importRequest);
|
return buildApiDefinition(id, name, path, method, importRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MsHTTPSamplerProxy buildRequest(Operation operation, String path, String method) {
|
private MsHTTPSamplerProxy buildRequest(Operation operation, String path, String method) {
|
||||||
|
@ -175,7 +175,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
|
|
||||||
private void parsePathParameters(Parameter parameter, List<KeyValue> rests) {
|
private void parsePathParameters(Parameter parameter, List<KeyValue> rests) {
|
||||||
PathParameter pathParameter = (PathParameter) parameter;
|
PathParameter pathParameter = (PathParameter) parameter;
|
||||||
rests.add(new KeyValue(pathParameter.getName(), "", getDefaultStringValue(parameter.getDescription())));
|
rests.add(new KeyValue(pathParameter.getName(), String.valueOf(pathParameter.getExample()), getDefaultStringValue(parameter.getDescription())));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getDefaultStringValue(String val) {
|
private String getDefaultStringValue(String val) {
|
||||||
|
@ -184,12 +184,12 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
|
|
||||||
private void parseCookieParameters(Parameter parameter, List<KeyValue> headers) {
|
private void parseCookieParameters(Parameter parameter, List<KeyValue> headers) {
|
||||||
CookieParameter cookieParameter = (CookieParameter) parameter;
|
CookieParameter cookieParameter = (CookieParameter) parameter;
|
||||||
addCookie(headers, cookieParameter.getName(), "", getDefaultStringValue(cookieParameter.getDescription()), parameter.getRequired());
|
addCookie(headers, cookieParameter.getName(), String.valueOf(cookieParameter.getExample()), getDefaultStringValue(cookieParameter.getDescription()), parameter.getRequired());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseHeaderParameters(Parameter parameter, List<KeyValue> headers) {
|
private void parseHeaderParameters(Parameter parameter, List<KeyValue> headers) {
|
||||||
HeaderParameter headerParameter = (HeaderParameter) parameter;
|
HeaderParameter headerParameter = (HeaderParameter) parameter;
|
||||||
addHeader(headers, headerParameter.getName(), "", getDefaultStringValue(headerParameter.getDescription()), "", parameter.getRequired());
|
addHeader(headers, headerParameter.getName(), String.valueOf(headerParameter.getExample()), getDefaultStringValue(headerParameter.getDescription()), "", parameter.getRequired());
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpResponse parseResponse(ApiResponses responses) {
|
private HttpResponse parseResponse(ApiResponses responses) {
|
||||||
|
@ -201,9 +201,9 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
msResponse.setStatusCode(new ArrayList<>());
|
msResponse.setStatusCode(new ArrayList<>());
|
||||||
if (responses != null) {
|
if (responses != null) {
|
||||||
responses.forEach((responseCode, response) -> {
|
responses.forEach((responseCode, response) -> {
|
||||||
msResponse.getStatusCode().add(new KeyValue(responseCode, responseCode));
|
|
||||||
parseResponseHeader(response, msResponse.getHeaders());
|
parseResponseHeader(response, msResponse.getHeaders());
|
||||||
parseResponseBody(response, msResponse.getBody());
|
parseResponseBody(response, msResponse.getBody());
|
||||||
|
parseResponseCode(responseCode, msResponse);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return msResponse;
|
return msResponse;
|
||||||
|
@ -213,11 +213,17 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
Map<String, Header> headers = response.getHeaders();
|
Map<String, Header> headers = response.getHeaders();
|
||||||
if (headers != null) {
|
if (headers != null) {
|
||||||
headers.forEach((k, v) -> {
|
headers.forEach((k, v) -> {
|
||||||
msHeaders.add(new KeyValue(k, "", v.getDescription()));
|
msHeaders.add(new KeyValue(k, String.valueOf(v.getExample()), v.getDescription()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void parseResponseCode(String response, HttpResponse msResponse) {
|
||||||
|
if (StringUtils.isNotEmpty(response)) {
|
||||||
|
msResponse.setStatusCode(JSON.parseObject(response, List.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void parseResponseBody(ApiResponse response, Body body) {
|
private void parseResponseBody(ApiResponse response, Body body) {
|
||||||
body.setRaw(response.getDescription());
|
body.setRaw(response.getDescription());
|
||||||
Content content = response.getContent();
|
Content content = response.getContent();
|
||||||
|
@ -245,7 +251,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
MediaType mediaType = content.get(contentType);
|
MediaType mediaType = content.get(contentType);
|
||||||
if (mediaType == null) {
|
if (mediaType == null) {
|
||||||
Set<String> contentTypes = content.keySet();
|
Set<String> contentTypes = content.keySet();
|
||||||
if(contentTypes.size() == 0) { // 防止空指针
|
if (contentTypes.size() == 0) { // 防止空指针
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
contentType = contentTypes.iterator().next();
|
contentType = contentTypes.iterator().next();
|
||||||
|
@ -295,7 +301,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
kv.setType("file");
|
kv.setType("file");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(body.getKvs() == null) { // 防止空指针
|
if (body.getKvs() == null) { // 防止空指针
|
||||||
body.setKvs(new ArrayList<>());
|
body.setKvs(new ArrayList<>());
|
||||||
}
|
}
|
||||||
body.getKvs().add(kv);
|
body.getKvs().add(kv);
|
||||||
|
@ -359,17 +365,17 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
} else if (schema instanceof BinarySchema) {
|
} else if (schema instanceof BinarySchema) {
|
||||||
return getDefaultValueByPropertyType(schema);
|
return getDefaultValueByPropertyType(schema);
|
||||||
} else {
|
} else {
|
||||||
if(schema.getType() != null) { // 特判属性不是对象的情况,直接将基本类型赋值进去
|
if (schema.getType() != null) { // 特判属性不是对象的情况,直接将基本类型赋值进去
|
||||||
if(StringUtils.equals(schema.getType(), "string")) {
|
if (StringUtils.equals(schema.getType(), "string")) {
|
||||||
Object exampleObj = schema.getExample();
|
Object exampleObj = schema.getExample();
|
||||||
String example = null;
|
String example = null;
|
||||||
if (exampleObj != null) {
|
if (exampleObj != null) {
|
||||||
example = exampleObj.toString();
|
example = exampleObj.toString();
|
||||||
}
|
}
|
||||||
return example == null ? "" : example;
|
return example == null ? "" : example;
|
||||||
} else if(StringUtils.equals(schema.getType(), "boolean")) {
|
} else if (StringUtils.equals(schema.getType(), "boolean")) {
|
||||||
return schema.getExample();
|
return schema.getExample();
|
||||||
} else if(StringUtils.equals(schema.getType(), "double")) {
|
} else if (StringUtils.equals(schema.getType(), "double")) {
|
||||||
return schema.getExample();
|
return schema.getExample();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -388,7 +394,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
}
|
}
|
||||||
JSONObject jsonObject = new JSONObject(true);
|
JSONObject jsonObject = new JSONObject(true);
|
||||||
properties.forEach((key, value) -> {
|
properties.forEach((key, value) -> {
|
||||||
jsonObject.put(key, parseSchema(value, refSet, infoMap));
|
jsonObject.put(key, parseSchema(value, refSet, infoMap));
|
||||||
});
|
});
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
|
@ -406,45 +412,46 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
|
|
||||||
private void parseQueryParameters(Parameter parameter, List<KeyValue> arguments) {
|
private void parseQueryParameters(Parameter parameter, List<KeyValue> arguments) {
|
||||||
QueryParameter queryParameter = (QueryParameter) parameter;
|
QueryParameter queryParameter = (QueryParameter) parameter;
|
||||||
arguments.add(new KeyValue(queryParameter.getName(), "", getDefaultStringValue(queryParameter.getDescription()), parameter.getRequired()));
|
arguments.add(new KeyValue(queryParameter.getName(), String.valueOf(queryParameter.getExample()), getDefaultStringValue(queryParameter.getDescription()), parameter.getRequired()));
|
||||||
}
|
}
|
||||||
/* 导出的 swagger json描述文件样例
|
|
||||||
{
|
/* 导出的 swagger json描述文件样例
|
||||||
"openapi":"3.0.1",
|
{
|
||||||
"info":{},
|
"openapi":"3.0.1",
|
||||||
"externalDocs":{},
|
"info":{},
|
||||||
"servers":{},
|
"externalDocs":{},
|
||||||
"tags":{},
|
"servers":{},
|
||||||
"paths":{ // 对应 SwaggerApiExportResult 类的paths
|
"tags":{},
|
||||||
"/lzx/test/{ball}":{ // key
|
"paths":{ // 对应 SwaggerApiExportResult 类的paths
|
||||||
"get":{ // 对应 SwaggerPath 类的 JSONObject 类型的成员,”get“为key
|
"/lzx/test/{ball}":{ // key
|
||||||
"tags":[
|
"get":{ // 对应 SwaggerPath 类的 JSONObject 类型的成员,”get“为key
|
||||||
"subModule2"
|
"tags":[
|
||||||
],
|
"subModule2"
|
||||||
"summary":"API",
|
],
|
||||||
"parameters":[
|
"summary":"API",
|
||||||
{
|
"parameters":[
|
||||||
"name":"ballName",
|
{
|
||||||
"in":"query",// path,header,query都可选。
|
"name":"ballName",
|
||||||
"description":"描述param",
|
"in":"query",// path,header,query都可选。
|
||||||
"required":true // 是否必填参数
|
"description":"描述param",
|
||||||
}
|
"required":true // 是否必填参数
|
||||||
],
|
}
|
||||||
"requestBody":{
|
],
|
||||||
"content":{
|
"requestBody":{
|
||||||
"application/octet-stream":{ // type
|
"content":{
|
||||||
"schema":{
|
"application/octet-stream":{ // type
|
||||||
"type":null,
|
"schema":{
|
||||||
"format":null
|
"type":null,
|
||||||
|
"format":null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} // SwaggerApiInfo类,为 value
|
||||||
} // SwaggerApiInfo类,为 value
|
}
|
||||||
}
|
},
|
||||||
},
|
"components":{}
|
||||||
"components":{}
|
} */
|
||||||
} */
|
|
||||||
public SwaggerApiExportResult swagger3Export(List<ApiDefinitionWithBLOBs> apiDefinitionList) {
|
public SwaggerApiExportResult swagger3Export(List<ApiDefinitionWithBLOBs> apiDefinitionList) {
|
||||||
SwaggerApiExportResult result = new SwaggerApiExportResult();
|
SwaggerApiExportResult result = new SwaggerApiExportResult();
|
||||||
|
|
||||||
|
@ -456,13 +463,13 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
result.setExternalDocs(new JSONObject());
|
result.setExternalDocs(new JSONObject());
|
||||||
|
|
||||||
JSONObject paths = new JSONObject();
|
JSONObject paths = new JSONObject();
|
||||||
for(ApiDefinitionWithBLOBs apiDefinition : apiDefinitionList) {
|
for (ApiDefinitionWithBLOBs apiDefinition : apiDefinitionList) {
|
||||||
SwaggerApiInfo swaggerApiInfo = new SwaggerApiInfo(); // {tags:, summary:, description:, parameters:}
|
SwaggerApiInfo swaggerApiInfo = new SwaggerApiInfo(); // {tags:, summary:, description:, parameters:}
|
||||||
swaggerApiInfo.setSummary(apiDefinition.getName());
|
swaggerApiInfo.setSummary(apiDefinition.getName());
|
||||||
// 设置导入后的模块名 (根据 api 的 moduleID 查库获得所属模块,作为导出的模块名)
|
// 设置导入后的模块名 (根据 api 的 moduleID 查库获得所属模块,作为导出的模块名)
|
||||||
ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
|
ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
|
||||||
String moduleName = "";
|
String moduleName = "";
|
||||||
if(apiDefinition.getModuleId() != null) { // module_id 可能为空
|
if (apiDefinition.getModuleId() != null) { // module_id 可能为空
|
||||||
ApiModuleDTO node = apiModuleService.getNode(apiDefinition.getModuleId());
|
ApiModuleDTO node = apiModuleService.getNode(apiDefinition.getModuleId());
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
moduleName = node.getName();
|
moduleName = node.getName();
|
||||||
|
@ -480,7 +487,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
List<JSONObject> paramsList = buildParameters(requestObject);
|
List<JSONObject> paramsList = buildParameters(requestObject);
|
||||||
swaggerApiInfo.setParameters(paramsList);
|
swaggerApiInfo.setParameters(paramsList);
|
||||||
JSONObject methodDetail = JSON.parseObject(JSON.toJSONString(swaggerApiInfo));
|
JSONObject methodDetail = JSON.parseObject(JSON.toJSONString(swaggerApiInfo));
|
||||||
if(paths.getJSONObject(apiDefinition.getPath()) == null) {
|
if (paths.getJSONObject(apiDefinition.getPath()) == null) {
|
||||||
paths.put(apiDefinition.getPath(), new JSONObject());
|
paths.put(apiDefinition.getPath(), new JSONObject());
|
||||||
} // 一个路径下有多个发方法,如post,get,因此是一个 JSONObject 类型
|
} // 一个路径下有多个发方法,如post,get,因此是一个 JSONObject 类型
|
||||||
paths.getJSONObject(apiDefinition.getPath()).put(apiDefinition.getMethod().toLowerCase(), methodDetail);
|
paths.getJSONObject(apiDefinition.getPath()).put(apiDefinition.getMethod().toLowerCase(), methodDetail);
|
||||||
|
@ -497,12 +504,12 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
put("arguments", "query");
|
put("arguments", "query");
|
||||||
}};
|
}};
|
||||||
Set<String> typeKeys = typeMap.keySet();
|
Set<String> typeKeys = typeMap.keySet();
|
||||||
for(String type : typeKeys) {
|
for (String type : typeKeys) {
|
||||||
JSONArray params = request.getJSONArray(type); // 获得请求参数列表
|
JSONArray params = request.getJSONArray(type); // 获得请求参数列表
|
||||||
if(params != null) {
|
if (params != null) {
|
||||||
for(int i = 0; i < params.size(); ++i) {
|
for (int i = 0; i < params.size(); ++i) {
|
||||||
JSONObject param = params.getJSONObject(i); // 对于每个参数:
|
JSONObject param = params.getJSONObject(i); // 对于每个参数:
|
||||||
if(param.get("name") == null || StringUtils.isEmpty(((String) param.get("name")))) {
|
if (param.get("name") == null || StringUtils.isEmpty(((String) param.get("name")))) {
|
||||||
continue;
|
continue;
|
||||||
} // 否则无参数的情况,可能多出一行空行
|
} // 否则无参数的情况,可能多出一行空行
|
||||||
SwaggerParams swaggerParam = new SwaggerParams();
|
SwaggerParams swaggerParam = new SwaggerParams();
|
||||||
|
@ -510,6 +517,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
swaggerParam.setDescription((String) param.get("description"));
|
swaggerParam.setDescription((String) param.get("description"));
|
||||||
swaggerParam.setName((String) param.get("name"));
|
swaggerParam.setName((String) param.get("name"));
|
||||||
swaggerParam.setRequired((boolean) param.get("required"));
|
swaggerParam.setRequired((boolean) param.get("required"));
|
||||||
|
swaggerParam.setExample((String) param.get("value"));
|
||||||
// 请求头 value 没有导出
|
// 请求头 value 没有导出
|
||||||
// JSONObject schema = new JSONObject();
|
// JSONObject schema = new JSONObject();
|
||||||
// swaggerParam.setSchema(schema);
|
// swaggerParam.setSchema(schema);
|
||||||
|
@ -533,7 +541,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
|
|
||||||
// 将请求体中的一个 json 对象转换成 swagger 格式的 json 对象返回
|
// 将请求体中的一个 json 对象转换成 swagger 格式的 json 对象返回
|
||||||
private JSONObject buildRequestBodyJsonInfo(JSONObject requestBody) {
|
private JSONObject buildRequestBodyJsonInfo(JSONObject requestBody) {
|
||||||
if(requestBody == null)
|
if (requestBody == null)
|
||||||
return null;
|
return null;
|
||||||
JSONObject schema = new JSONObject();
|
JSONObject schema = new JSONObject();
|
||||||
schema.put("type", "object");
|
schema.put("type", "object");
|
||||||
|
@ -555,32 +563,31 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
}
|
}
|
||||||
} */
|
} */
|
||||||
private JSONObject buildRequestBodyJsonInfo(JSONArray requestBody) {
|
private JSONObject buildRequestBodyJsonInfo(JSONArray requestBody) {
|
||||||
if(requestBody == null)
|
if (requestBody == null)
|
||||||
return null;
|
return null;
|
||||||
JSONObject schema = new JSONObject();
|
JSONObject schema = new JSONObject();
|
||||||
schema.put("type", "array");
|
schema.put("type", "array");
|
||||||
JSONObject items = new JSONObject();
|
JSONObject items = new JSONObject();
|
||||||
|
|
||||||
if(requestBody.size() > 0) {
|
if (requestBody.size() > 0) {
|
||||||
Object example = requestBody.get(0);
|
Object example = requestBody.get(0);
|
||||||
if(example instanceof JSONObject) {
|
if (example instanceof JSONObject) {
|
||||||
items.put("type", "object");
|
items.put("type", "object");
|
||||||
items.put("properties", buildSchema((JSONObject) example));
|
items.put("properties", buildSchema((JSONObject) example));
|
||||||
} else if(example instanceof java.lang.String) {
|
} else if (example instanceof java.lang.String) {
|
||||||
items.put("type", "string");
|
items.put("type", "string");
|
||||||
} else if(example instanceof java.lang.Integer) {
|
} else if (example instanceof java.lang.Integer) {
|
||||||
items.put("type", "integer");
|
items.put("type", "integer");
|
||||||
items.put("format", "int64");
|
items.put("format", "int64");
|
||||||
} else if(example instanceof java.lang.Boolean) {
|
} else if (example instanceof java.lang.Boolean) {
|
||||||
items.put("type", "boolean");
|
items.put("type", "boolean");
|
||||||
} else if(example instanceof java.math.BigDecimal) {
|
} else if (example instanceof java.math.BigDecimal) {
|
||||||
items.put("type", "double");
|
items.put("type", "double");
|
||||||
}
|
} else { // JSONOArray
|
||||||
else { // JSONOArray
|
|
||||||
items.put("type", "array");
|
items.put("type", "array");
|
||||||
JSONObject item = new JSONObject();
|
JSONObject item = new JSONObject();
|
||||||
if(((JSONArray) example).size() > 0) {
|
if (((JSONArray) example).size() > 0) {
|
||||||
if(((JSONArray) example).get(0) instanceof JSONObject) {
|
if (((JSONArray) example).get(0) instanceof JSONObject) {
|
||||||
item = buildRequestBodyJsonInfo((JSONObject) ((JSONArray) example).get(0));
|
item = buildRequestBodyJsonInfo((JSONObject) ((JSONArray) example).get(0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -604,7 +611,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
parsedParam.put("description", requestBody.getString("description"));
|
parsedParam.put("description", requestBody.getString("description"));
|
||||||
}
|
}
|
||||||
parsedParam.put("items", item);
|
parsedParam.put("items", item);
|
||||||
} else if(StringUtils.equals(type, "object")) {
|
} else if (StringUtils.equals(type, "object")) {
|
||||||
parsedParam.put("type", "object");
|
parsedParam.put("type", "object");
|
||||||
JSONObject properties = requestBody.getJSONObject("properties");
|
JSONObject properties = requestBody.getJSONObject("properties");
|
||||||
JSONObject swaggerProperties = new JSONObject();
|
JSONObject swaggerProperties = new JSONObject();
|
||||||
|
@ -619,14 +626,14 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
parsedParam.put("description", requestBody.getString("description"));
|
parsedParam.put("description", requestBody.getString("description"));
|
||||||
}
|
}
|
||||||
parsedParam.put("properties", swaggerProperties);
|
parsedParam.put("properties", swaggerProperties);
|
||||||
} else if(StringUtils.equals(type, "integer")) {
|
} else if (StringUtils.equals(type, "integer")) {
|
||||||
parsedParam.put("type", "integer");
|
parsedParam.put("type", "integer");
|
||||||
parsedParam.put("format", "int64");
|
parsedParam.put("format", "int64");
|
||||||
setCommonJsonSchemaParam(parsedParam, requestBody);
|
setCommonJsonSchemaParam(parsedParam, requestBody);
|
||||||
} else if(StringUtils.equals(type, "boolean")) {
|
} else if (StringUtils.equals(type, "boolean")) {
|
||||||
parsedParam.put("type", "boolean");
|
parsedParam.put("type", "boolean");
|
||||||
setCommonJsonSchemaParam(parsedParam, requestBody);
|
setCommonJsonSchemaParam(parsedParam, requestBody);
|
||||||
} else if(StringUtils.equals(type, "number")) { // double 类型会被 fastJson 转换为 BigDecimal
|
} else if (StringUtils.equals(type, "number")) { // double 类型会被 fastJson 转换为 BigDecimal
|
||||||
parsedParam.put("type", "double");
|
parsedParam.put("type", "double");
|
||||||
setCommonJsonSchemaParam(parsedParam, requestBody);
|
setCommonJsonSchemaParam(parsedParam, requestBody);
|
||||||
} else {
|
} else {
|
||||||
|
@ -636,6 +643,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
}
|
}
|
||||||
return parsedParam;
|
return parsedParam;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCommonJsonSchemaParam(JSONObject parsedParam, JSONObject requestBody) {
|
public void setCommonJsonSchemaParam(JSONObject parsedParam, JSONObject requestBody) {
|
||||||
if (StringUtils.isNotBlank(requestBody.getString("description"))) {
|
if (StringUtils.isNotBlank(requestBody.getString("description"))) {
|
||||||
parsedParam.put("description", requestBody.getString("description"));
|
parsedParam.put("description", requestBody.getString("description"));
|
||||||
|
@ -658,22 +666,22 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
// 设置一个 json 对象的属性在 swagger 格式中的类型、值
|
// 设置一个 json 对象的属性在 swagger 格式中的类型、值
|
||||||
private JSONObject buildSchema(JSONObject requestBody) {
|
private JSONObject buildSchema(JSONObject requestBody) {
|
||||||
JSONObject schema = new JSONObject();
|
JSONObject schema = new JSONObject();
|
||||||
for(String key : requestBody.keySet()) {
|
for (String key : requestBody.keySet()) {
|
||||||
Object param = requestBody.get(key);
|
Object param = requestBody.get(key);
|
||||||
JSONObject parsedParam = new JSONObject();
|
JSONObject parsedParam = new JSONObject();
|
||||||
if(param instanceof java.lang.String) {
|
if (param instanceof java.lang.String) {
|
||||||
parsedParam.put("type", "string");
|
parsedParam.put("type", "string");
|
||||||
parsedParam.put("example", param == null? "" : param);
|
parsedParam.put("example", param == null ? "" : param);
|
||||||
} else if(param instanceof java.lang.Integer) {
|
} else if (param instanceof java.lang.Integer) {
|
||||||
parsedParam.put("type", "integer");
|
parsedParam.put("type", "integer");
|
||||||
parsedParam.put("format", "int64");
|
parsedParam.put("format", "int64");
|
||||||
parsedParam.put("example", param);
|
parsedParam.put("example", param);
|
||||||
} else if(param instanceof JSONObject) {
|
} else if (param instanceof JSONObject) {
|
||||||
parsedParam = buildRequestBodyJsonInfo((JSONObject) param);
|
parsedParam = buildRequestBodyJsonInfo((JSONObject) param);
|
||||||
} else if(param instanceof java.lang.Boolean) {
|
} else if (param instanceof java.lang.Boolean) {
|
||||||
parsedParam.put("type", "boolean");
|
parsedParam.put("type", "boolean");
|
||||||
parsedParam.put("example", param);
|
parsedParam.put("example", param);
|
||||||
} else if(param instanceof java.math.BigDecimal) { // double 类型会被 fastJson 转换为 BigDecimal
|
} else if (param instanceof java.math.BigDecimal) { // double 类型会被 fastJson 转换为 BigDecimal
|
||||||
parsedParam.put("type", "double");
|
parsedParam.put("type", "double");
|
||||||
parsedParam.put("example", param);
|
parsedParam.put("example", param);
|
||||||
} else { // JSONOArray
|
} else { // JSONOArray
|
||||||
|
@ -682,8 +690,8 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
if (param == null) {
|
if (param == null) {
|
||||||
param = new JSONArray();
|
param = new JSONArray();
|
||||||
}
|
}
|
||||||
if(((JSONArray) param).size() > 0) {
|
if (((JSONArray) param).size() > 0) {
|
||||||
if(((JSONArray) param).get(0) instanceof JSONObject) { ///
|
if (((JSONArray) param).get(0) instanceof JSONObject) { ///
|
||||||
item = buildRequestBodyJsonInfo((JSONObject) ((JSONArray) param).get(0));
|
item = buildRequestBodyJsonInfo((JSONObject) ((JSONArray) param).get(0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -697,7 +705,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
private JSONObject buildformDataSchema(JSONObject kvs) {
|
private JSONObject buildformDataSchema(JSONObject kvs) {
|
||||||
JSONObject schema = new JSONObject();
|
JSONObject schema = new JSONObject();
|
||||||
JSONObject properties = new JSONObject();
|
JSONObject properties = new JSONObject();
|
||||||
for(String key : kvs.keySet()) {
|
for (String key : kvs.keySet()) {
|
||||||
JSONObject property = new JSONObject();
|
JSONObject property = new JSONObject();
|
||||||
property.put("type", "string");
|
property.put("type", "string");
|
||||||
property.put("example", kvs.getString(key));
|
property.put("example", kvs.getString(key));
|
||||||
|
@ -709,11 +717,11 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
|
|
||||||
private JSONObject getformDataProperties(JSONArray requestBody) {
|
private JSONObject getformDataProperties(JSONArray requestBody) {
|
||||||
JSONObject result = new JSONObject();
|
JSONObject result = new JSONObject();
|
||||||
for(Object item : requestBody) {
|
for (Object item : requestBody) {
|
||||||
if(item instanceof JSONObject) {
|
if (item instanceof JSONObject) {
|
||||||
String name = ((JSONObject) item).getString("name");
|
String name = ((JSONObject) item).getString("name");
|
||||||
String value = ((JSONObject) item).getString("value");
|
String value = ((JSONObject) item).getString("value");
|
||||||
if(name != null) {
|
if (name != null) {
|
||||||
result.put(name, value);
|
result.put(name, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -721,15 +729,15 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 请求头格式:
|
/* 请求头格式:
|
||||||
"headers":{
|
"headers":{
|
||||||
"headerName":{
|
"headerName":{
|
||||||
"schema":{
|
"schema":{
|
||||||
"type":"string"
|
"type":"string"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
*/
|
||||||
*/
|
|
||||||
private JSONObject buildResponseBody(JSONObject response) {
|
private JSONObject buildResponseBody(JSONObject response) {
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
return new JSONObject();
|
return new JSONObject();
|
||||||
|
@ -739,11 +747,12 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
// build 请求头
|
// build 请求头
|
||||||
JSONObject headers = new JSONObject();
|
JSONObject headers = new JSONObject();
|
||||||
JSONArray headValueList = response.getJSONArray("headers");
|
JSONArray headValueList = response.getJSONArray("headers");
|
||||||
if(headValueList != null) {
|
if (headValueList != null) {
|
||||||
for(Object item : headValueList) {
|
for (Object item : headValueList) {
|
||||||
if(item instanceof JSONObject && ((JSONObject) item).getString("name") != null) {
|
if (item instanceof JSONObject && ((JSONObject) item).getString("name") != null) {
|
||||||
JSONObject head = new JSONObject(), headSchema = new JSONObject();
|
JSONObject head = new JSONObject(), headSchema = new JSONObject();
|
||||||
head.put("description", "");
|
head.put("description", "");
|
||||||
|
head.put("example", ((JSONObject) item).getString("value"));
|
||||||
headSchema.put("type", "string");
|
headSchema.put("type", "string");
|
||||||
head.put("schema", headSchema);
|
head.put("schema", headSchema);
|
||||||
headers.put(((JSONObject) item).getString("name"), head);
|
headers.put(((JSONObject) item).getString("name"), head);
|
||||||
|
@ -752,28 +761,28 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
}
|
}
|
||||||
statusCodeInfo.put("headers", headers);
|
statusCodeInfo.put("headers", headers);
|
||||||
|
|
||||||
|
// 返回code
|
||||||
JSONArray statusCode = response.getJSONArray("statusCode");
|
JSONArray statusCode = response.getJSONArray("statusCode");
|
||||||
|
|
||||||
// build 请求体
|
// build 请求体
|
||||||
if (statusCode == null || statusCode.size() < 1 || statusCode.getJSONObject(0).getString("name") == null) {
|
if (statusCode == null || statusCode.size() < 1 || statusCode.getJSONObject(0).getString("name") == null) {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
statusCodeInfo.put("content", buildContent(response));
|
statusCodeInfo.put("content", buildContent(response));
|
||||||
statusCodeInfo.put("description", "");
|
statusCodeInfo.put("description", "");
|
||||||
responseBody.put(((JSONObject) response.getJSONArray("statusCode").get(0)).getString("name"), statusCodeInfo);
|
responseBody.put(JSON.toJSONString(statusCode), statusCodeInfo);
|
||||||
return responseBody;
|
return responseBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 请求体格式:
|
/* 请求体格式:
|
||||||
"content":{
|
"content":{
|
||||||
"application/json":{
|
"application/json":{
|
||||||
"schema":{
|
"schema":{
|
||||||
"type":"xxx",
|
"type":"xxx",
|
||||||
"xxx":{...}
|
"xxx":{...}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
*/
|
||||||
*/
|
|
||||||
private JSONObject buildContent(JSONObject respOrReq) {
|
private JSONObject buildContent(JSONObject respOrReq) {
|
||||||
Hashtable<String, String> typeMap = new Hashtable<String, String>() {{
|
Hashtable<String, String> typeMap = new Hashtable<String, String>() {{
|
||||||
put("XML", org.springframework.http.MediaType.APPLICATION_XML_VALUE);
|
put("XML", org.springframework.http.MediaType.APPLICATION_XML_VALUE);
|
||||||
|
@ -786,11 +795,11 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
Object bodyInfo = new Object();
|
Object bodyInfo = new Object();
|
||||||
JSONObject body = respOrReq.getJSONObject("body");
|
JSONObject body = respOrReq.getJSONObject("body");
|
||||||
|
|
||||||
if(body != null) { // 将请求体转换成相应的格式导出
|
if (body != null) { // 将请求体转换成相应的格式导出
|
||||||
String bodyType = body.getString("type");
|
String bodyType = body.getString("type");
|
||||||
if(bodyType == null) {
|
if (bodyType == null) {
|
||||||
|
|
||||||
} else if(bodyType.equals("JSON")) {
|
} else if (bodyType.equals("JSON")) {
|
||||||
try {
|
try {
|
||||||
if (StringUtils.equals(body.getString("format"), "JSON-SCHEMA")) {
|
if (StringUtils.equals(body.getString("format"), "JSON-SCHEMA")) {
|
||||||
// String jsonSchema = JSONSchemaGenerator.getJson(body.getString("jsonSchema"));
|
// String jsonSchema = JSONSchemaGenerator.getJson(body.getString("jsonSchema"));
|
||||||
|
@ -811,7 +820,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
bodyInfo = buildJsonSchema(jsonObject, required);
|
bodyInfo = buildJsonSchema(jsonObject, required);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try{ // 若请求体是一个 object
|
try { // 若请求体是一个 object
|
||||||
bodyInfo = buildRequestBodyJsonInfo(body.getJSONArray("raw"));
|
bodyInfo = buildRequestBodyJsonInfo(body.getJSONArray("raw"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
bodyInfo = buildRequestBodyJsonInfo(body.getJSONObject("raw"));
|
bodyInfo = buildRequestBodyJsonInfo(body.getJSONObject("raw"));
|
||||||
|
@ -824,11 +833,11 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
((JSONObject) bodyInfo).put("example", body.get("raw").toString());
|
((JSONObject) bodyInfo).put("example", body.get("raw").toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if(bodyType.equals("XML")) {
|
} else if (bodyType.equals("XML")) {
|
||||||
String xmlText = body.getString("raw");
|
String xmlText = body.getString("raw");
|
||||||
JSONObject xmlToJson = XMLUtils.XmlToJson(xmlText);
|
JSONObject xmlToJson = XMLUtils.XmlToJson(xmlText);
|
||||||
bodyInfo = buildRequestBodyJsonInfo(xmlToJson);
|
bodyInfo = buildRequestBodyJsonInfo(xmlToJson);
|
||||||
} else if(bodyType.equals("WWW_FORM") || bodyType.equals("Form Data") || bodyType.equals("BINARY")) { // key-value 类格式
|
} else if (bodyType.equals("WWW_FORM") || bodyType.equals("Form Data") || bodyType.equals("BINARY")) { // key-value 类格式
|
||||||
JSONObject formData = getformDataProperties(body.getJSONArray("kvs"));
|
JSONObject formData = getformDataProperties(body.getJSONArray("kvs"));
|
||||||
bodyInfo = buildformDataSchema(formData);
|
bodyInfo = buildformDataSchema(formData);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import lombok.*;
|
||||||
@Setter
|
@Setter
|
||||||
public class SwaggerParams {
|
public class SwaggerParams {
|
||||||
private String name; // 对应 API 请求参数名
|
private String name; // 对应 API 请求参数名
|
||||||
|
private String example; // 参数值
|
||||||
private String in; // 参数类型,可选值为 path,header,query 等
|
private String in; // 参数类型,可选值为 path,header,query 等
|
||||||
private String description;
|
private String description;
|
||||||
private boolean required; // 是否是必填参数
|
private boolean required; // 是否是必填参数
|
||||||
|
|
|
@ -1349,9 +1349,7 @@ public class ApiDefinitionService {
|
||||||
}
|
}
|
||||||
} else { // 导出为 Swagger 格式
|
} else { // 导出为 Swagger 格式
|
||||||
Swagger3Parser swagger3Parser = new Swagger3Parser();
|
Swagger3Parser swagger3Parser = new Swagger3Parser();
|
||||||
System.out.println(apiDefinitionMapper.selectByExampleWithBLOBs(example));
|
|
||||||
apiExportResult = swagger3Parser.swagger3Export(apiDefinitionMapper.selectByExampleWithBLOBs(example));
|
apiExportResult = swagger3Parser.swagger3Export(apiDefinitionMapper.selectByExampleWithBLOBs(example));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return apiExportResult;
|
return apiExportResult;
|
||||||
|
|
Loading…
Reference in New Issue