fix (接口定义): 修复导入导出Swagger问题

--bug=1007229 --user=赵勇 [github#6835]接口导出后导入,响应内容中的描述信息没有了,同时请求参数中的值没有了 https://www.tapd.cn/55049933/s/1056772
This commit is contained in:
fit2-zhao 2021-10-19 17:51:56 +08:00 committed by fit2-zhao
parent 18848277f4
commit 245af574e0
3 changed files with 124 additions and 116 deletions

View File

@ -175,7 +175,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
private void parsePathParameters(Parameter parameter, List<KeyValue> rests) {
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) {
@ -184,12 +184,12 @@ public class Swagger3Parser extends SwaggerAbstractParser {
private void parseCookieParameters(Parameter parameter, List<KeyValue> headers) {
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) {
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) {
@ -201,9 +201,9 @@ public class Swagger3Parser extends SwaggerAbstractParser {
msResponse.setStatusCode(new ArrayList<>());
if (responses != null) {
responses.forEach((responseCode, response) -> {
msResponse.getStatusCode().add(new KeyValue(responseCode, responseCode));
parseResponseHeader(response, msResponse.getHeaders());
parseResponseBody(response, msResponse.getBody());
parseResponseCode(responseCode, msResponse);
});
}
return msResponse;
@ -213,11 +213,17 @@ public class Swagger3Parser extends SwaggerAbstractParser {
Map<String, Header> headers = response.getHeaders();
if (headers != null) {
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) {
body.setRaw(response.getDescription());
Content content = response.getContent();
@ -406,8 +412,9 @@ public class Swagger3Parser extends SwaggerAbstractParser {
private void parseQueryParameters(Parameter parameter, List<KeyValue> arguments) {
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描述文件样例
{
"openapi":"3.0.1",
@ -510,6 +517,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
swaggerParam.setDescription((String) param.get("description"));
swaggerParam.setName((String) param.get("name"));
swaggerParam.setRequired((boolean) param.get("required"));
swaggerParam.setExample((String) param.get("value"));
// 请求头 value 没有导出
// JSONObject schema = new JSONObject();
// swaggerParam.setSchema(schema);
@ -575,8 +583,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
items.put("type", "boolean");
} else if (example instanceof java.math.BigDecimal) {
items.put("type", "double");
}
else { // JSONOArray
} else { // JSONOArray
items.put("type", "array");
JSONObject item = new JSONObject();
if (((JSONArray) example).size() > 0) {
@ -636,6 +643,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
}
return parsedParam;
}
public void setCommonJsonSchemaParam(JSONObject parsedParam, JSONObject requestBody) {
if (StringUtils.isNotBlank(requestBody.getString("description"))) {
parsedParam.put("description", requestBody.getString("description"));
@ -744,6 +752,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
if (item instanceof JSONObject && ((JSONObject) item).getString("name") != null) {
JSONObject head = new JSONObject(), headSchema = new JSONObject();
head.put("description", "");
head.put("example", ((JSONObject) item).getString("value"));
headSchema.put("type", "string");
head.put("schema", headSchema);
headers.put(((JSONObject) item).getString("name"), head);
@ -752,15 +761,15 @@ public class Swagger3Parser extends SwaggerAbstractParser {
}
statusCodeInfo.put("headers", headers);
// 返回code
JSONArray statusCode = response.getJSONArray("statusCode");
// build 请求体
if (statusCode == null || statusCode.size() < 1 || statusCode.getJSONObject(0).getString("name") == null) {
return response;
}
statusCodeInfo.put("content", buildContent(response));
statusCodeInfo.put("description", "");
responseBody.put(((JSONObject) response.getJSONArray("statusCode").get(0)).getString("name"), statusCodeInfo);
responseBody.put(JSON.toJSONString(statusCode), statusCodeInfo);
return responseBody;
}

View File

@ -7,6 +7,7 @@ import lombok.*;
@Setter
public class SwaggerParams {
private String name; // 对应 API 请求参数名
private String example; // 参数值
private String in; // 参数类型可选值为 path,header,query
private String description;
private boolean required; // 是否是必填参数

View File

@ -1349,9 +1349,7 @@ public class ApiDefinitionService {
}
} else { // 导出为 Swagger 格式
Swagger3Parser swagger3Parser = new Swagger3Parser();
System.out.println(apiDefinitionMapper.selectByExampleWithBLOBs(example));
apiExportResult = swagger3Parser.swagger3Export(apiDefinitionMapper.selectByExampleWithBLOBs(example));
}
return apiExportResult;