fix(接口测试): 修复8000行JSON作为参数的接口使用文档无法加载的缺陷

修复8000行JSON作为参数的接口使用文档无法加载的缺陷
This commit is contained in:
song-tianyang 2022-04-02 23:18:41 +08:00 committed by CountryBuilder
parent b2b2e50b57
commit a0439bf844
4 changed files with 53 additions and 65 deletions

View File

@ -31,7 +31,7 @@ public class ApiDocumentInfoDTO {
private String requestBodyFormData; private String requestBodyFormData;
private String requestBodyStrutureData; private String requestBodyStrutureData;
private Object requestPreviewData; private Object requestPreviewData;
private Object jsonSchemaBody; private String jsonSchemaBody;
private String responseHead; private String responseHead;
private String responseBody; private String responseBody;

View File

@ -3,6 +3,7 @@ package io.metersphere.api.service;
import com.alibaba.fastjson.JSON; 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 com.alibaba.fastjson.parser.Feature;
import io.metersphere.api.dto.automation.APIScenarioReportResult; import io.metersphere.api.dto.automation.APIScenarioReportResult;
import io.metersphere.api.dto.share.*; import io.metersphere.api.dto.share.*;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
@ -110,7 +111,7 @@ public class ShareInfoService {
return true; return true;
} }
public ApiDocumentInfoDTO conversionModelToDTO(ApiDefinitionWithBLOBs apiModel, Map<String,User> userIdMap) { public ApiDocumentInfoDTO conversionModelToDTO(ApiDefinitionWithBLOBs apiModel, Map<String, User> userIdMap) {
ApiDocumentInfoDTO apiInfoDTO = new ApiDocumentInfoDTO(); ApiDocumentInfoDTO apiInfoDTO = new ApiDocumentInfoDTO();
JSONArray previewJsonArray = new JSONArray(); JSONArray previewJsonArray = new JSONArray();
if (apiModel != null) { if (apiModel != null) {
@ -120,19 +121,19 @@ public class ShareInfoService {
apiInfoDTO.setUri(apiModel.getPath()); apiInfoDTO.setUri(apiModel.getPath());
apiInfoDTO.setStatus(apiModel.getStatus()); apiInfoDTO.setStatus(apiModel.getStatus());
if(StringUtils.isNotEmpty(apiModel.getTags())){ if (StringUtils.isNotEmpty(apiModel.getTags())) {
JSONArray tagsArr = JSONArray.parseArray(apiModel.getTags()); JSONArray tagsArr = JSONArray.parseArray(apiModel.getTags());
List<String> tagList = new ArrayList<>(); List<String> tagList = new ArrayList<>();
for(int i = 0;i < tagsArr.size();i ++){ for (int i = 0; i < tagsArr.size(); i++) {
tagList.add(tagsArr.getString(i)); tagList.add(tagsArr.getString(i));
} }
if(!tagList.isEmpty()){ if (!tagList.isEmpty()) {
apiInfoDTO.setTags(StringUtils.join(tagList,",")); apiInfoDTO.setTags(StringUtils.join(tagList, ","));
} }
} }
apiInfoDTO.setResponsibler(userIdMap.get(apiModel.getUserId()) == null? apiModel.getUserId() : userIdMap.get(apiModel.getUserId()).getName()); apiInfoDTO.setResponsibler(userIdMap.get(apiModel.getUserId()) == null ? apiModel.getUserId() : userIdMap.get(apiModel.getUserId()).getName());
apiInfoDTO.setCreateUser(userIdMap.get(apiModel.getCreateUser()) == null? apiModel.getCreateUser() : userIdMap.get(apiModel.getCreateUser()).getName()); apiInfoDTO.setCreateUser(userIdMap.get(apiModel.getCreateUser()) == null ? apiModel.getCreateUser() : userIdMap.get(apiModel.getCreateUser()).getName());
apiInfoDTO.setDesc(apiModel.getDescription()); apiInfoDTO.setDesc(apiModel.getDescription());
ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
apiInfoDTO.setModules(apiModuleService.getModuleNameById(apiModel.getModuleId())); apiInfoDTO.setModules(apiModuleService.getModuleNameById(apiModel.getModuleId()));
@ -165,6 +166,7 @@ public class ShareInfoService {
} }
} }
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage());
} }
} }
//rest参数设置 //rest参数设置
@ -180,6 +182,7 @@ public class ShareInfoService {
} }
} }
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage());
} }
} }
apiInfoDTO.setUrlParams(urlParamArr.toJSONString()); apiInfoDTO.setUrlParams(urlParamArr.toJSONString());
@ -207,20 +210,12 @@ public class ShareInfoService {
isJsonSchema = true; isJsonSchema = true;
} }
} }
if (isJsonSchema) { if (bodyObj.containsKey("raw")) {
apiInfoDTO.setRequestBodyParamType("JSON-SCHEMA"); String raw = bodyObj.getString("raw");
apiInfoDTO.setJsonSchemaBody(bodyObj); apiInfoDTO.setJsonSchemaBody(raw);
if (bodyObj.containsKey("jsonSchema")) { apiInfoDTO.setRequestBodyStrutureData(raw);
JSONObject jsonSchemaObj = bodyObj.getJSONObject("jsonSchema"); //转化jsonObje 或者 jsonArray
apiInfoDTO.setRequestPreviewData(JSON.parse(JSONSchemaGenerator.getJson(jsonSchemaObj.toJSONString()))); this.setPreviewData(previewJsonArray, raw);
}
} else {
if (bodyObj.containsKey("raw")) {
String raw = bodyObj.getString("raw");
apiInfoDTO.setRequestBodyStrutureData(raw);
//转化jsonObje 或者 jsonArray
this.setPreviewData(previewJsonArray, raw);
}
} }
} else if (StringUtils.equalsAny(type, "XML", "Raw")) { } else if (StringUtils.equalsAny(type, "XML", "Raw")) {
if (bodyObj.containsKey("raw")) { if (bodyObj.containsKey("raw")) {
@ -281,9 +276,8 @@ public class ShareInfoService {
} }
} }
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage());
} }
} }
} }
} }
@ -303,7 +297,7 @@ public class ShareInfoService {
} }
apiInfoDTO.setResponseHead(responseHeadDataArr.toJSONString()); apiInfoDTO.setResponseHead(responseHeadDataArr.toJSONString());
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage());
} }
} }
// 赋值响应体 // 赋值响应体
@ -330,10 +324,8 @@ public class ShareInfoService {
} }
} }
if (isJsonSchema) { if (isJsonSchema) {
// apiInfoDTO.setRequestBodyParamType("JSON-SCHEMA");
apiInfoDTO.setResponseBodyParamType("JSON-SCHEMA"); apiInfoDTO.setResponseBodyParamType("JSON-SCHEMA");
apiInfoDTO.setJsonSchemaResponseBody(bodyObj); apiInfoDTO.setJsonSchemaResponseBody(bodyObj);
// apiInfoDTO.setJsonSchemaBody(bodyObj);
} else { } else {
if (bodyObj.containsKey("raw")) { if (bodyObj.containsKey("raw")) {
String raw = bodyObj.getString("raw"); String raw = bodyObj.getString("raw");
@ -342,10 +334,6 @@ public class ShareInfoService {
this.setPreviewData(previewJsonArray, raw); this.setPreviewData(previewJsonArray, raw);
} }
} }
// if (bodyObj.containsKey("raw")) {
// String raw = bodyObj.getString("raw");
// apiInfoDTO.setResponseBodyStrutureData(raw);
// }
} else if (StringUtils.equalsAny(type, "Form Data", "WWW_FORM")) { } else if (StringUtils.equalsAny(type, "Form Data", "WWW_FORM")) {
if (bodyObj.containsKey("kvs")) { if (bodyObj.containsKey("kvs")) {
JSONArray bodyParamArr = new JSONArray(); JSONArray bodyParamArr = new JSONArray();
@ -386,7 +374,7 @@ public class ShareInfoService {
} }
} }
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage());
} }
} }
@ -403,7 +391,7 @@ public class ShareInfoService {
} }
apiInfoDTO.setResponseCode(responseStatusDataArr.toJSONString()); apiInfoDTO.setResponseCode(responseStatusDataArr.toJSONString());
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage());
} }
} }
} }
@ -418,21 +406,24 @@ public class ShareInfoService {
private JSONObject genJSONObject(String request) { private JSONObject genJSONObject(String request) {
JSONObject returnObj = null; JSONObject returnObj = null;
try { try {
returnObj = JSONObject.parseObject(request); returnObj = JSONObject.parseObject(request, Feature.DisableCircularReferenceDetect);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage());
} }
return returnObj; return returnObj;
} }
private void setPreviewData(JSONArray previewArray, String data) { private void setPreviewData(JSONArray previewArray, String data) {
try { try {
JSONObject previewObj = JSONObject.parseObject(data); JSONObject previewObj = JSONObject.parseObject(data, Feature.DisableCircularReferenceDetect);
previewArray.add(previewObj); previewArray.add(previewObj);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage());
} }
try { try {
previewArray = JSONArray.parseArray(data); previewArray = JSONArray.parseArray(data);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e.getMessage());
} }
} }
@ -554,44 +545,42 @@ public class ShareInfoService {
@Transactional(propagation = Propagation.NOT_SUPPORTED) @Transactional(propagation = Propagation.NOT_SUPPORTED)
public void validateExpired(ShareInfo shareInfo) { public void validateExpired(ShareInfo shareInfo) {
// 有效期根据类型从ProjectApplication中获取 // 有效期根据类型从ProjectApplication中获取
if(shareInfo == null ){ if (shareInfo == null) {
MSException.throwException(Translator.get("connection_expired")); MSException.throwException(Translator.get("connection_expired"));
} }
String type = ""; String type = "";
String projectId=""; String projectId = "";
if(shareInfo.getShareType().equals("PERFORMANCE_REPORT")){ if (shareInfo.getShareType().equals("PERFORMANCE_REPORT")) {
type = ProjectApplicationType.PERFORMANCE_SHARE_REPORT_TIME.toString(); type = ProjectApplicationType.PERFORMANCE_SHARE_REPORT_TIME.toString();
LoadTestReportWithBLOBs loadTestReportWithBLOBs = loadTestReportMapper.selectByPrimaryKey(shareInfo.getCustomData()); LoadTestReportWithBLOBs loadTestReportWithBLOBs = loadTestReportMapper.selectByPrimaryKey(shareInfo.getCustomData());
if(loadTestReportWithBLOBs!=null){ if (loadTestReportWithBLOBs != null) {
projectId = loadTestReportWithBLOBs.getProjectId(); projectId = loadTestReportWithBLOBs.getProjectId();
} }
} }
if(shareInfo.getShareType().equals("PLAN_DB_REPORT")){ if (shareInfo.getShareType().equals("PLAN_DB_REPORT")) {
type = ProjectApplicationType.TRACK_SHARE_REPORT_TIME.toString(); type = ProjectApplicationType.TRACK_SHARE_REPORT_TIME.toString();
TestPlanWithBLOBs testPlan = getTestPlan(shareInfo); TestPlanWithBLOBs testPlan = getTestPlan(shareInfo);
if (testPlan != null){ if (testPlan != null) {
projectId = testPlan.getProjectId(); projectId = testPlan.getProjectId();
}; }
} }
if(shareInfo.getShareType().equals("API_REPORT")){ if (shareInfo.getShareType().equals("API_REPORT")) {
type = ProjectApplicationType.API_SHARE_REPORT_TIME.toString(); type = ProjectApplicationType.API_SHARE_REPORT_TIME.toString();
APIScenarioReportResult reportResult = extApiScenarioReportMapper.get(shareInfo.getCustomData()); APIScenarioReportResult reportResult = extApiScenarioReportMapper.get(shareInfo.getCustomData());
if (reportResult != null){ if (reportResult != null) {
projectId = reportResult.getProjectId(); projectId = reportResult.getProjectId();
}; }
} }
if(StringUtils.isBlank(type)|| Strings.isBlank(projectId)){ if (StringUtils.isBlank(type) || Strings.isBlank(projectId)) {
millisCheck(System.currentTimeMillis() - shareInfo.getUpdateTime() ,1000 * 60 * 60 * 24,shareInfo.getId()); millisCheck(System.currentTimeMillis() - shareInfo.getUpdateTime(), 1000 * 60 * 60 * 24, shareInfo.getId());
}else{ } else {
ProjectApplication projectApplication = projectApplicationService.getProjectApplication(projectId,type); ProjectApplication projectApplication = projectApplicationService.getProjectApplication(projectId, type);
if(projectApplication.getTypeValue()==null){ if (projectApplication.getTypeValue() == null) {
millisCheck(System.currentTimeMillis() - shareInfo.getUpdateTime() ,1000 * 60 * 60 * 24,shareInfo.getId()); millisCheck(System.currentTimeMillis() - shareInfo.getUpdateTime(), 1000 * 60 * 60 * 24, shareInfo.getId());
}else { } else {
String expr= projectApplication.getTypeValue(); String expr = projectApplication.getTypeValue();
long timeMills = getTimeMills(shareInfo.getUpdateTime(),expr); long timeMills = getTimeMills(shareInfo.getUpdateTime(), expr);
millisCheck(System.currentTimeMillis(),timeMills,shareInfo.getId()); millisCheck(System.currentTimeMillis(), timeMills, shareInfo.getId());
} }
} }
} }
@ -604,7 +593,7 @@ public class ShareInfoService {
TestPlanReportContentWithBLOBs testPlanReportContent = testPlanReportContents.get(0); TestPlanReportContentWithBLOBs testPlanReportContent = testPlanReportContents.get(0);
if (testPlanReportContent != null) { if (testPlanReportContent != null) {
TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(testPlanReportContent.getTestPlanReportId()); TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(testPlanReportContent.getTestPlanReportId());
if(testPlanReport!=null){ if (testPlanReport != null) {
return testPlanMapper.selectByPrimaryKey(testPlanReport.getTestPlanId()); return testPlanMapper.selectByPrimaryKey(testPlanReport.getTestPlanId());
} }
@ -613,8 +602,8 @@ public class ShareInfoService {
return null; return null;
} }
private void millisCheck(long compareMillis, long millis,String shareInfoId) { private void millisCheck(long compareMillis, long millis, String shareInfoId) {
if (compareMillis>millis) { if (compareMillis > millis) {
shareInfoMapper.deleteByPrimaryKey(shareInfoId); shareInfoMapper.deleteByPrimaryKey(shareInfoId);
MSException.throwException(Translator.get("connection_expired")); MSException.throwException(Translator.get("connection_expired"));
} }

View File

@ -332,6 +332,9 @@ export default {
for (let dataIndex = 0; dataIndex < returnDatas.length; dataIndex++) { for (let dataIndex = 0; dataIndex < returnDatas.length; dataIndex++) {
let index = indexArr[dataIndex]; let index = indexArr[dataIndex];
let data = returnDatas[dataIndex]; let data = returnDatas[dataIndex];
if (data.jsonSchemaBody) {
data.jsonSchemaBody = {raw:data.jsonSchemaBody};
}
this.$set(this.apiInfoArray, index, data); this.$set(this.apiInfoArray, index, data);
} }
this.updateShowArray(itemIndex, afterNodeIndex, beforeNodeIndex); this.updateShowArray(itemIndex, afterNodeIndex, beforeNodeIndex);

View File

@ -27,14 +27,10 @@
min-width="120px" min-width="120px"
show-overflow-tooltip/> show-overflow-tooltip/>
</el-table> </el-table>
<div v-else-if="apiInfo.requestBodyParamType == 'JSON-SCHEMA'" style="margin-left: 10px"> <div v-else-if="apiInfo.requestBodyParamType === 'JSON-SCHEMA' || apiInfo.requestBodyParamType === 'JSON'" style="margin-left: 10px">
<ms-json-code-edit :show-preview="false" :json-schema-disable="true" :body="apiInfo.jsonSchemaBody" <ms-json-code-edit :show-preview="false" :json-schema-disable="true" :body="apiInfo.jsonSchemaBody"
ref="jsonCodeEdit"/> ref="jsonCodeEdit"/>
</div> </div>
<div v-else-if="formatRowDataToJsonSchema(apiInfo,'request') " style="margin-left: 10px">
<ms-json-code-edit :show-preview="false" :json-schema-disable="true" :body="apiInfo.requestJsonSchema"
ref="jsonCodeEdit"/>
</div>
<div v-else class="showDataDiv"> <div v-else class="showDataDiv">
<br/> <br/>
<p style="margin: 0px 20px;" <p style="margin: 0px 20px;"