fix: swagger导出报错

This commit is contained in:
chenjianxing 2021-04-26 10:12:16 +08:00 committed by jianxing
parent da620b78b0
commit f4db89f34b
1 changed files with 28 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.ApiModuleDTO;
import io.metersphere.api.dto.definition.SwaggerApiExportResult; import io.metersphere.api.dto.definition.SwaggerApiExportResult;
import io.metersphere.api.dto.definition.parse.swagger.*; import io.metersphere.api.dto.definition.parse.swagger.*;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
@ -450,7 +451,10 @@ public class Swagger3Parser extends SwaggerAbstractParser {
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 可能为空
moduleName = apiModuleService.getNode(apiDefinition.getModuleId()).getName(); ApiModuleDTO node = apiModuleService.getNode(apiDefinition.getModuleId());
if (node != null) {
moduleName = node.getName();
}
} }
swaggerApiInfo.setTags(Arrays.asList(moduleName)); swaggerApiInfo.setTags(Arrays.asList(moduleName));
// 设置请求体 // 设置请求体
@ -614,8 +618,11 @@ public class Swagger3Parser extends SwaggerAbstractParser {
} }
} }
statusCodeInfo.put("headers", headers); statusCodeInfo.put("headers", headers);
JSONArray statusCode = response.getJSONArray("statusCode");
// build 请求体 // build 请求体
if(((JSONObject) response.getJSONArray("statusCode").get(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));
@ -644,21 +651,28 @@ public class Swagger3Parser extends SwaggerAbstractParser {
put("WWW_FORM", org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE); put("WWW_FORM", org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE);
}}; }};
JSONObject bodyInfo = new JSONObject(); JSONObject bodyInfo = new JSONObject();
if(respOrReq.getJSONObject("body") != null) { // 将请求体转换成相应的格式导出 JSONObject body = respOrReq.getJSONObject("body");
String bodyType = respOrReq.getJSONObject("body").getString("type");
if(bodyType == null) {
}else if(bodyType.equals("JSON")) { try {
bodyInfo = buildRequestBodyJsonInfo(respOrReq.getJSONObject("body").getJSONObject("raw")); if(body != null) { // 将请求体转换成相应的格式导出
} else if(bodyType.equals("XML")) { String bodyType = body.getString("type");
String xmlText = respOrReq.getJSONObject("body").getString("raw"); if(bodyType == null) {
JSONObject xmlToJson = XMLUtils.XmlToJson(xmlText);
bodyInfo = buildRequestBodyJsonInfo(xmlToJson); }else if(bodyType.equals("JSON")) {
} else if(bodyType.equals("WWW_FORM") || bodyType.equals("Form Data") || bodyType.equals("BINARY")) { // key-value 类格式 bodyInfo = buildRequestBodyJsonInfo(body.getJSONObject("raw"));
JSONObject formData = getformDataProperties(respOrReq.getJSONObject("body").getJSONArray("kvs")); } else if(bodyType.equals("XML")) {
bodyInfo = buildformDataSchema(formData); String xmlText = body.getString("raw");
JSONObject xmlToJson = XMLUtils.XmlToJson(xmlText);
bodyInfo = buildRequestBodyJsonInfo(xmlToJson);
} else if(bodyType.equals("WWW_FORM") || bodyType.equals("Form Data") || bodyType.equals("BINARY")) { // key-value 类格式
JSONObject formData = getformDataProperties(body.getJSONArray("kvs"));
bodyInfo = buildformDataSchema(formData);
}
} }
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
} }
String type = respOrReq.getJSONObject("body").getString("type"); String type = respOrReq.getJSONObject("body").getString("type");
JSONObject content = new JSONObject(); JSONObject content = new JSONObject();
JSONObject schema = bodyInfo; // 请求体部分 JSONObject schema = bodyInfo; // 请求体部分