diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index 4489642a11..4248f69e69 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -381,7 +381,7 @@ public class ElementUtil { } } else { if (element.has(ElementConstants.HASH_TREE)) { - JSONArray elementJSONArray = element.getJSONArray(ElementConstants.HASH_TREE); + JSONArray elementJSONArray = element.optJSONArray(ElementConstants.HASH_TREE); relationships(elementJSONArray, referenceRelationships); } } @@ -399,7 +399,7 @@ public class ElementUtil { element.put(ElementConstants.CLAZZ_NAME, clazzMap.get(element.optString(PropertyConstant.TYPE))); } if (element.has(ElementConstants.HASH_TREE)) { - JSONArray elementJSONArray = element.getJSONArray(ElementConstants.HASH_TREE); + JSONArray elementJSONArray = element.optJSONArray(ElementConstants.HASH_TREE); dataFormatting(elementJSONArray); } } @@ -411,7 +411,7 @@ public class ElementUtil { } formatSampler(element); if (element != null && element.has(ElementConstants.HASH_TREE)) { - JSONArray elementJSONArray = element.getJSONArray(ElementConstants.HASH_TREE); + JSONArray elementJSONArray = element.optJSONArray(ElementConstants.HASH_TREE); dataFormatting(elementJSONArray); } } @@ -477,7 +477,7 @@ public class ElementUtil { } } if (element.has(ElementConstants.HASH_TREE)) { - JSONArray elementJSONArray = element.getJSONArray(ElementConstants.HASH_TREE); + JSONArray elementJSONArray = element.optJSONArray(ElementConstants.HASH_TREE); if (isScenarioEnv) { dataSetDomain(elementJSONArray, config); } else { diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index 366ef8147e..46705751bd 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -190,7 +190,7 @@ public class MsScenario extends MsTestElement { if (scenario != null && StringUtils.isNotEmpty(scenario.getScenarioDefinition())) { JSONObject element = JSONUtil.parseObject(scenario.getScenarioDefinition()); // 历史数据处理 - ElementUtil.dataFormatting(element.getJSONArray(ElementConstants.HASH_TREE)); + ElementUtil.dataFormatting(element.optJSONArray(ElementConstants.HASH_TREE)); this.setName(scenario.getName()); this.setProjectId(scenario.getProjectId()); LinkedList sourceHashTree = mapper.readValue(element.optString(ElementConstants.HASH_TREE), new TypeReference>() { diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiEnvironmentRunningParamService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiEnvironmentRunningParamService.java index cf7c28bcb7..d6f6df68b3 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiEnvironmentRunningParamService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiEnvironmentRunningParamService.java @@ -43,7 +43,7 @@ public class ApiEnvironmentRunningParamService { if (configObj.has("commonConfig")) { JSONObject commonConfig = configObj.optJSONObject("commonConfig"); if (commonConfig.has("variables")) { - JSONArray variables = commonConfig.getJSONArray("variables"); + JSONArray variables = commonConfig.optJSONArray("variables"); List variableList = new LinkedList<>(); for (Map.Entry entry : varMap.entrySet()) { String key = entry.getKey(); diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/MsAbstractParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/MsAbstractParser.java index 2e041a37fa..50e5358ec3 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/MsAbstractParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/MsAbstractParser.java @@ -1,9 +1,9 @@ package io.metersphere.api.parse; -import io.metersphere.api.parse.api.ms.NodeTree; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; +import io.metersphere.api.parse.api.ms.NodeTree; import io.metersphere.commons.utils.LogUtil; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -19,19 +19,21 @@ public abstract class MsAbstractParser extends ApiImportAbstractParser { protected List parseMsHTTPSamplerProxy(JSONObject testObject, String tag, boolean isSetUrl) { JSONObject requests = testObject.optJSONObject(tag); List msHTTPSamplerProxies = new ArrayList<>(); - requests.keySet().forEach(requestName -> { - JSONObject requestObject = requests.optJSONObject(requestName); - String path = requestObject.optString("url"); - String method = requestObject.optString("method"); - MsHTTPSamplerProxy request = buildRequest(requestName, path, method); - parseBody(requestObject, request.getBody()); - parseHeader(requestObject, request.getHeaders()); - parsePath(request); - if (isSetUrl) { - request.setUrl(path); - } - msHTTPSamplerProxies.add(request); - }); + if (requests != null) { + requests.keySet().forEach(requestName -> { + JSONObject requestObject = requests.optJSONObject(requestName); + String path = requestObject.optString("url"); + String method = requestObject.optString("method"); + MsHTTPSamplerProxy request = buildRequest(requestName, path, method); + parseBody(requestObject, request.getBody()); + parseHeader(requestObject, request.getHeaders()); + parsePath(request); + if (isSetUrl) { + request.setUrl(path); + } + msHTTPSamplerProxies.add(request); + }); + } return msHTTPSamplerProxies; } @@ -64,7 +66,7 @@ public abstract class MsAbstractParser extends ApiImportAbstractParser { } private void parseHeader(JSONObject requestObject, List msHeaders) { - JSONArray headers = requestObject.getJSONArray("headers"); + JSONArray headers = requestObject.optJSONArray("headers"); if (headers != null) { for (int i = 0; i < headers.length(); i++) { JSONObject header = headers.optJSONObject(i); @@ -77,7 +79,7 @@ public abstract class MsAbstractParser extends ApiImportAbstractParser { if (requestObject.has("body")) { Object body = requestObject.get("body"); if (body instanceof JSONArray) { - JSONArray bodies = requestObject.getJSONArray("body"); + JSONArray bodies = requestObject.optJSONArray("body"); if (bodies != null) { StringBuilder bodyStr = new StringBuilder(); for (int i = 0; i < bodies.length(); i++) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/MsDefinitionParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/MsDefinitionParser.java index 326a196b1a..67bfb743fa 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/MsDefinitionParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/MsDefinitionParser.java @@ -50,18 +50,20 @@ public class MsDefinitionParser extends MsAbstractParser { protected List parsePluginFormat(JSONObject testObject, ApiTestImportRequest importRequest, Boolean isCreateModule) { List results = new ArrayList<>(); - testObject.keySet().forEach(tag -> { + if (testObject != null) { + testObject.keySet().forEach(tag -> { - List msHTTPSamplerProxies = parseMsHTTPSamplerProxy(testObject, tag, false); - for (MsHTTPSamplerProxy msHTTPSamplerProxy : msHTTPSamplerProxies) { - ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(msHTTPSamplerProxy.getId(), msHTTPSamplerProxy.getName(), msHTTPSamplerProxy.getPath(), msHTTPSamplerProxy.getMethod(), importRequest); - apiDefinition.setProjectId(this.projectId); - apiDefinition.setModulePath(tag); - apiDefinition.setRequest(JSON.toJSONString(msHTTPSamplerProxy)); - apiDefinition.setName(apiDefinition.getPath() + " [" + apiDefinition.getMethod() + "]"); - results.add(apiDefinition); - } - }); + List msHTTPSamplerProxies = parseMsHTTPSamplerProxy(testObject, tag, false); + for (MsHTTPSamplerProxy msHTTPSamplerProxy : msHTTPSamplerProxies) { + ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(msHTTPSamplerProxy.getId(), msHTTPSamplerProxy.getName(), msHTTPSamplerProxy.getPath(), msHTTPSamplerProxy.getMethod(), importRequest); + apiDefinition.setProjectId(this.projectId); + apiDefinition.setModulePath(tag); + apiDefinition.setRequest(JSON.toJSONString(msHTTPSamplerProxy)); + apiDefinition.setName(apiDefinition.getPath() + " [" + apiDefinition.getMethod() + "]"); + results.add(apiDefinition); + } + }); + } return results; } diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java index 64ac06ec77..4d65dff1df 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java @@ -695,7 +695,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { }}; Set typeKeys = typeMap.keySet(); for (String type : typeKeys) { - JSONArray params = request.getJSONArray(type); // 获得请求参数列表 + JSONArray params = request.optJSONArray(type); // 获得请求参数列表 if (params != null) { for (int i = 0; i < params.length(); ++i) { JSONObject param = params.optJSONObject(i); // 对于每个参数: @@ -942,7 +942,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { JSONObject statusCodeInfo = new JSONObject(); // build 请求头 JSONObject headers = new JSONObject(); - JSONArray headValueList = response.getJSONArray("headers"); + JSONArray headValueList = response.optJSONArray("headers"); if (headValueList != null) { for (Object item : headValueList) { if (item instanceof JSONObject && ((JSONObject) item).optString("name") != null) { @@ -960,7 +960,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { statusCodeInfo.put("content", buildContent(response)); statusCodeInfo.put("description", ""); // 返回code - JSONArray statusCode = response.getJSONArray("statusCode"); + JSONArray statusCode = response.optJSONArray("statusCode"); responseBody.put(statusCode.toString(), statusCodeInfo); return responseBody; } @@ -997,19 +997,19 @@ public class Swagger3Parser extends SwaggerAbstractParser { JSONObject jsonObject = JSONUtil.parseObject(jsonSchema); JSONArray required = new JSONArray(); if (jsonObject != null) { - required = jsonObject.getJSONArray(PropertyConstant.REQUIRED); + required = jsonObject.optJSONArray(PropertyConstant.REQUIRED); } if (required == null) { JSONObject items = jsonObject.optJSONObject(PropertyConstant.ITEMS); if (items != null) { - required = items.getJSONArray(PropertyConstant.REQUIRED); + required = items.optJSONArray(PropertyConstant.REQUIRED); } } bodyInfo = buildJsonSchema(jsonObject, required); } } else { try { // 若请求体是一个 object - bodyInfo = buildRequestBodyJsonInfo(body.getJSONArray("raw")); + bodyInfo = buildRequestBodyJsonInfo(body.optJSONArray("raw")); } catch (Exception e) { bodyInfo = buildRequestBodyJsonInfo(body.optJSONObject("raw")); } @@ -1032,7 +1032,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { JSONObject xmlToJson = XMLUtil.XmlToJson(xmlText); bodyInfo = buildRequestBodyJsonInfo(xmlToJson); } else if (bodyType != null && (bodyType.equalsIgnoreCase("WWW_FORM") || bodyType.equalsIgnoreCase("Form Data") || bodyType.equalsIgnoreCase("BINARY"))) { // key-value 类格式 - JSONObject formData = getformDataProperties(body.getJSONArray("kvs")); + JSONObject formData = getformDataProperties(body.optJSONArray("kvs")); bodyInfo = buildFormDataSchema(formData); } } diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/ApiScenarioImportUtil.java b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/ApiScenarioImportUtil.java index 88c219e2a6..8159a053db 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/ApiScenarioImportUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/ApiScenarioImportUtil.java @@ -296,8 +296,8 @@ public class ApiScenarioImportUtil { object.put("index", i + 1); object.put("resourceId", UUID.randomUUID().toString()); hashTree.put(i, object); - if (object.has(ElementConstants.HASH_TREE) && object.getJSONArray(ElementConstants.HASH_TREE) != null) { - formatHashTree(object.getJSONArray(ElementConstants.HASH_TREE)); + if (object.has(ElementConstants.HASH_TREE) && object.optJSONArray(ElementConstants.HASH_TREE) != null) { + formatHashTree(object.optJSONArray(ElementConstants.HASH_TREE)); } } } diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiTestDefinitionDiffUtilImpl.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiTestDefinitionDiffUtilImpl.java index 944931a1c9..f7cf415a33 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiTestDefinitionDiffUtilImpl.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/ApiTestDefinitionDiffUtilImpl.java @@ -5,6 +5,7 @@ import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.request.sampler.MsJDBCSampler; import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler; import io.metersphere.api.dto.scenario.Body; +import io.metersphere.commons.constants.ElementConstants; import io.metersphere.log.utils.ApiDefinitionDiffUtil; import io.metersphere.log.utils.ReflexObjectUtil; import io.metersphere.log.utils.diff.json.JacksonDiff; @@ -94,21 +95,21 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil { Map diffMap = new LinkedHashMap<>(); diffMap.put(TYPE, bloBsIsNew.getString(TYPE)); JsonDiff jsonDiff = new JacksonDiff(); - if (bloBsIsNew.getString(TYPE).equals("TCPSampler")) { - MsTCPSampler tcpSamplerNew = JSONUtil.parseObject(bloBsIsNew.toString(), MsTCPSampler.class); - MsTCPSampler tcpSamplerOld = JSONUtil.parseObject(bloBsIsOld.toString(), MsTCPSampler.class); + if (bloBsIsNew.getString(TYPE).equals(ElementConstants.TCP_SAMPLER)) { + MsTCPSampler tcpSamplerNew = JSON.parseObject(bloBsIsNew.toString(), MsTCPSampler.class); + MsTCPSampler tcpSamplerOld = JSON.parseObject(bloBsIsOld.toString(), MsTCPSampler.class); diffTcp(tcpSamplerNew, tcpSamplerOld, jsonDiff, diffMap); - } else if (bloBsIsNew.getString(TYPE).equals("HTTPSamplerProxy")) { - MsHTTPSamplerProxy httpSamplerProxyNew = JSONUtil.parseObject(bloBsIsNew.toString(), MsHTTPSamplerProxy.class); - MsHTTPSamplerProxy httpSamplerProxyOld = JSONUtil.parseObject(bloBsIsOld.toString(), MsHTTPSamplerProxy.class); + } else if (bloBsIsNew.getString(TYPE).equals(ElementConstants.HTTP_SAMPLER)) { + MsHTTPSamplerProxy httpSamplerProxyNew = JSON.parseObject(bloBsIsNew.toString(), MsHTTPSamplerProxy.class); + MsHTTPSamplerProxy httpSamplerProxyOld = JSON.parseObject(bloBsIsOld.toString(), MsHTTPSamplerProxy.class); diffHttp(httpSamplerProxyNew, httpSamplerProxyOld, jsonDiff, diffMap); - } else if (bloBsIsNew.getString(TYPE).equals("JDBCSampler")) { - MsJDBCSampler jdbcSamplerNew = JSONUtil.parseObject(bloBsIsNew.toString(), MsJDBCSampler.class); - MsJDBCSampler jdbcSamplerOld = JSONUtil.parseObject(bloBsIsOld.toString(), MsJDBCSampler.class); + } else if (bloBsIsNew.getString(TYPE).equals(ElementConstants.JDBC_SAMPLER)) { + MsJDBCSampler jdbcSamplerNew = JSON.parseObject(bloBsIsNew.toString(), MsJDBCSampler.class); + MsJDBCSampler jdbcSamplerOld = JSON.parseObject(bloBsIsOld.toString(), MsJDBCSampler.class); diffJdbc(jdbcSamplerNew, jdbcSamplerOld, jsonDiff, diffMap); } else { - MsDubboSampler dubboSamplerNew = JSONUtil.parseObject(bloBsIsNew.toString(), MsDubboSampler.class); - MsDubboSampler dubboSamplerOld = JSONUtil.parseObject(bloBsIsOld.toString(), MsDubboSampler.class); + MsDubboSampler dubboSamplerNew = JSON.parseObject(bloBsIsNew.toString(), MsDubboSampler.class); + MsDubboSampler dubboSamplerOld = JSON.parseObject(bloBsIsOld.toString(), MsDubboSampler.class); diffDubbo(dubboSamplerNew, dubboSamplerOld, jsonDiff, diffMap); } if (diffMap.size() > 1) { diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java index 3047961a4c..a134202f16 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java @@ -55,7 +55,7 @@ public class GenerateHashTreeUtil { ElementUtil.dataFormatting(element); // 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取 if (element != null && element.has(ElementConstants.HASH_TREE)) { - LinkedList elements = mapper.readValue(element.getJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference>() { + LinkedList elements = mapper.readValue(element.optJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference>() { }); scenario.setHashTree(elements); } @@ -76,7 +76,7 @@ public class GenerateHashTreeUtil { try { if (element != null && element.has(ElementConstants.HASH_TREE)) { ElementUtil.dataFormatting(element); - return objectMapper.readValue(element.getJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference>() { + return objectMapper.readValue(element.optJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference>() { }); } } catch (JsonProcessingException e) { diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java index fa31b2fc8c..cef1dea509 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java @@ -52,7 +52,7 @@ public class HashTreeUtil { JSONObject commonConfig = configJson.optJSONObject(COMMON_CONFIG); if (commonConfig.has(VARIABLES)) { Map envHeadMap = new HashMap<>(); - JSONArray variablesArr = commonConfig.getJSONArray(VARIABLES); + JSONArray variablesArr = commonConfig.optJSONArray(VARIABLES); for (int i = 0; i < variablesArr.length(); i++) { JSONObject object = variablesArr.optJSONObject(i); if (object.has(NAME) && object.has(VALUE)) { diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java index 8ef643f669..2c2f8629f0 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java @@ -47,7 +47,7 @@ public class XMLUtil { jsonToXmlStr(jo, buffer, nowTab.append("\t")); buffer.append(tab).append("\n"); } else if (en.getValue() instanceof JSONArray) { - JSONArray array = jObj.getJSONArray(en.getKey()); + JSONArray array = jObj.optJSONArray(en.getKey()); for (int i = 0; i < array.length(); i++) { buffer.append(tab).append("<").append(en.getKey()).append(">\n"); if (StringUtils.isNotBlank(array.optString(i))) { diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java index 56ec9f3b34..34e0f5a675 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java @@ -116,7 +116,7 @@ public class MockApiUtils { } else if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM")) { if (bodyObj.has("kvs")) { JSONObject bodyParamArr = new JSONObject(); - JSONArray kvsArr = bodyObj.getJSONArray("kvs"); + JSONArray kvsArr = bodyObj.optJSONArray("kvs"); for (int i = 0; i < kvsArr.length(); i++) { JSONObject kv = kvsArr.optJSONObject(i); if (kv.has("name")) { @@ -200,7 +200,7 @@ public class MockApiUtils { } else if (StringUtils.equalsAny(type, "Form Data", "WWW_FORM")) { Map paramMap = new LinkedHashMap<>(); if (bodyObj.has("kvs")) { - JSONArray kvsArr = bodyObj.getJSONArray("kvs"); + JSONArray kvsArr = bodyObj.optJSONArray("kvs"); for (int i = 0; i < kvsArr.length(); i++) { JSONObject kv = kvsArr.optJSONObject(i); if (kv.has("name")) { @@ -223,7 +223,7 @@ public class MockApiUtils { responseDTO.setReturnData(returnStr); } if (respObj.has("statusCode")) { - JSONArray statusCodeArray = respObj.getJSONArray("statusCode"); + JSONArray statusCodeArray = respObj.optJSONArray("statusCode"); int code = 200; if (statusCodeArray != null) { for (int i = 0; i < statusCodeArray.length(); i++) { @@ -241,7 +241,7 @@ public class MockApiUtils { responseDTO.setReturnCode(code); } if (respObj.has("headers")) { - JSONArray jsonArray = respObj.getJSONArray("headers"); + JSONArray jsonArray = respObj.optJSONArray("headers"); Map headMap = new HashMap<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject headObj = jsonArray.optJSONObject(i); diff --git a/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java b/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java index fb9cce4002..86add2d270 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java @@ -335,7 +335,7 @@ public class MockConfigService { JSONObject expectParamsObj = mockExpectRequestObj.optJSONObject("params"); if (expectParamsObj.has("headers")) { //检测headers - JSONArray headerArr = expectParamsObj.getJSONArray("headers"); + JSONArray headerArr = expectParamsObj.optJSONArray("headers"); for (int i = 0; i < headerArr.length(); i++) { JSONObject jsonObject = headerArr.optJSONObject(i); if (jsonObject.has("name") && jsonObject.has("value")) { @@ -359,7 +359,7 @@ public class MockConfigService { paramsFilterType = expectBodyObject.optString("paramsFilterType"); } if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM") && expectBodyObject.has("kvs")) { - JSONArray kvsArr = expectBodyObject.getJSONArray("kvs"); + JSONArray kvsArr = expectBodyObject.optJSONArray("kvs"); List mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(kvsArr); if (CollectionUtils.isNotEmpty(mockConfigRequestParams)) { if (!MockApiUtils.checkParamsCompliance(jsonArray, mockConfigRequestParams, StringUtils.equals(paramsFilterType, "And"))) { @@ -386,7 +386,7 @@ public class MockConfigService { } if (expectParamsObj.has("arguments")) { - JSONArray argumentsArray = expectParamsObj.getJSONArray("arguments"); + JSONArray argumentsArray = expectParamsObj.optJSONArray("arguments"); List mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(argumentsArray); if (!MockApiUtils.checkParamsCompliance(requestMockParams.getQueryParamsObj(), mockConfigRequestParams, StringUtils.equals(paramsFilterType, "And"))) { return false; @@ -398,7 +398,7 @@ public class MockConfigService { restFilterType = expectParamsObj.optString("restFilterType"); } if (expectParamsObj.has("rest")) { - JSONArray restArray = expectParamsObj.getJSONArray("rest"); + JSONArray restArray = expectParamsObj.optJSONArray("rest"); List mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(restArray); if (!MockApiUtils.checkParamsCompliance(requestMockParams.getRestParamsObj(), mockConfigRequestParams, StringUtils.equals(restFilterType, "And"))) { return false; @@ -424,7 +424,7 @@ public class MockConfigService { mockExpectJson = mockExpectJsonItem; } } else { - JSONArray jsonArray = mockExpectRequestObj.getJSONArray("variables"); + JSONArray jsonArray = mockExpectRequestObj.optJSONArray("variables"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject object = jsonArray.optJSONObject(i); String name = ""; @@ -470,14 +470,14 @@ public class MockConfigService { if (requestObj.has("params")) { JSONObject paramsObj = requestObj.optJSONObject("params"); if (paramsObj.has("headers")) { - JSONArray headArray = paramsObj.getJSONArray("headers"); + JSONArray headArray = paramsObj.optJSONArray("headers"); boolean isHeadMatch = MockApiUtils.matchRequestHeader(headArray, requestHeaderMap); if (!isHeadMatch) { return null; } //判断rest为空 if (paramsObj.has("rest")) { - JSONArray restArray = paramsObj.getJSONArray("rest"); + JSONArray restArray = paramsObj.optJSONArray("rest"); for (int i = 0; i < restArray.length(); i++) { JSONObject restObj = restArray.optJSONObject(i); if (restObj.has("name") && restObj.has("value")) { @@ -487,7 +487,7 @@ public class MockConfigService { } //判断arguments为空 if (paramsObj.has("arguments")) { - JSONArray argumentsArray = paramsObj.getJSONArray("arguments"); + JSONArray argumentsArray = paramsObj.optJSONArray("arguments"); for (int i = 0; i < argumentsArray.length(); i++) { JSONObject argumentsObj = argumentsArray.optJSONObject(i); if (argumentsObj.has("name") && argumentsObj.has("value")) { @@ -513,7 +513,7 @@ public class MockConfigService { } } else if (StringUtils.equalsAnyIgnoreCase(type, "KeyValue", "Form Data", "WWW_FORM")) { if (bodyObj.has("kvs")) { - JSONArray kvsArray = bodyObj.getJSONArray("kvs"); + JSONArray kvsArray = bodyObj.optJSONArray("kvs"); for (int i = 0; i < kvsArray.length(); i++) { JSONObject kvsObj = kvsArray.optJSONObject(i); if (kvsObj.has("name") && kvsObj.has("value")) { @@ -547,7 +547,7 @@ public class MockConfigService { } } else { JSONObject mockExpectJson = new JSONObject(); - JSONArray jsonArray = requestObj.getJSONArray("variables"); + JSONArray jsonArray = requestObj.optJSONArray("variables"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject object = jsonArray.optJSONObject(i); String name = ""; @@ -583,7 +583,7 @@ public class MockConfigService { if (responseObj.has("responseResult")) { JSONObject responseJsonObj = responseObj.optJSONObject("responseResult"); if (responseJsonObj.has("headers")) { - JSONArray jsonArray = responseJsonObj.getJSONArray("headers"); + JSONArray jsonArray = responseJsonObj.optJSONArray("headers"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject object = jsonArray.optJSONObject(i); if (object.has("name") && object.has("value")) { @@ -618,7 +618,7 @@ public class MockConfigService { } } } else { - JSONArray jsonArray = responseObj.getJSONArray("httpHeads"); + JSONArray jsonArray = responseObj.optJSONArray("httpHeads"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject object = jsonArray.optJSONObject(i); String name = null; @@ -746,7 +746,7 @@ public class MockConfigService { //url参数赋值 if (requestObj.has("arguments")) { try { - JSONArray headArr = requestObj.getJSONArray("arguments"); + JSONArray headArr = requestObj.optJSONArray("arguments"); for (int index = 0; index < headArr.length(); index++) { JSONObject headObj = headArr.optJSONObject(index); @@ -759,7 +759,7 @@ public class MockConfigService { } if (requestObj.has("rest")) { try { - JSONArray headArr = requestObj.getJSONArray("rest"); + JSONArray headArr = requestObj.optJSONArray("rest"); for (int index = 0; index < headArr.length(); index++) { JSONObject headObj = headArr.optJSONObject(index); if (headObj.has("name") && !restParamList.contains(headObj.has("name"))) { @@ -778,7 +778,7 @@ public class MockConfigService { if (StringUtils.equalsAny(type, "Form Data", "WWW_FORM")) { if (bodyObj.has("kvs")) { - JSONArray kvsArr = bodyObj.getJSONArray("kvs"); + JSONArray kvsArr = bodyObj.optJSONArray("kvs"); for (int i = 0; i < kvsArr.length(); i++) { JSONObject kv = kvsArr.optJSONObject(i); if (kv.has("name") && !formDataList.contains(kv.has("name"))) { diff --git a/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java b/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java index fef22d9243..1d56c5fada 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java @@ -100,8 +100,8 @@ public class MsHashTreeService { } } } - if (object != null && object.getJSONArray(HASH_TREE) != null) { - setHashTree(object.getJSONArray(HASH_TREE)); + if (object != null && object.optJSONArray(HASH_TREE) != null) { + setHashTree(object.optJSONArray(HASH_TREE)); } } } @@ -122,7 +122,7 @@ public class MsHashTreeService { if (StringUtils.equalsIgnoreCase(element.optString(REFERENCED), REF)) { JSONObject refElement = JSONUtil.parseObject(apiTestCase.getRequest()); ElementUtil.dataFormatting(refElement); - JSONArray array = refElement.getJSONArray(HASH_TREE); + JSONArray array = refElement.optJSONArray(HASH_TREE); ElementUtil.copyBean(element, refElement); element.put(HEADERS, refElement.opt(HEADERS)); element.put(REST, refElement.opt(REST)); @@ -133,9 +133,9 @@ public class MsHashTreeService { element.put(ARGUMENTS, refElement.opt(ARGUMENTS)); element.put(PROJECT_ID, apiTestCase.getProjectId()); if (array != null) { - JSONArray sourceHashTree = element.getJSONArray(HASH_TREE); + JSONArray sourceHashTree = element.optJSONArray(HASH_TREE); Map> groupMap = ElementUtil.group(sourceHashTree); - Map> targetGroupMap = ElementUtil.group(refElement.getJSONArray(HASH_TREE)); + Map> targetGroupMap = ElementUtil.group(refElement.optJSONArray(HASH_TREE)); List pre = ElementUtil.mergeHashTree(groupMap.get("PRE"), targetGroupMap.get("PRE")); List post = ElementUtil.mergeHashTree(groupMap.get("POST"), targetGroupMap.get("POST")); @@ -223,7 +223,7 @@ public class MsHashTreeService { hashTree.put(i, element); } if (element.has(HASH_TREE)) { - JSONArray elementJSONArray = element.getJSONArray(HASH_TREE); + JSONArray elementJSONArray = element.optJSONArray(HASH_TREE); dataFormatting(elementJSONArray); } } @@ -236,7 +236,7 @@ public class MsHashTreeService { element = this.setRequest(element); } if (element != null && element.has(HASH_TREE)) { - JSONArray elementJSONArray = element.getJSONArray(HASH_TREE); + JSONArray elementJSONArray = element.optJSONArray(HASH_TREE); dataFormatting(elementJSONArray); } } diff --git a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioReferenceIdService.java b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioReferenceIdService.java index 58239e0822..9a5eeb5d79 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioReferenceIdService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioReferenceIdService.java @@ -111,7 +111,7 @@ public class ApiScenarioReferenceIdService { if (!jsonObject.has(MsHashTreeService.HASH_TREE)) { return returnList; } - JSONArray hashTree = jsonObject.getJSONArray(MsHashTreeService.HASH_TREE); + JSONArray hashTree = jsonObject.optJSONArray(MsHashTreeService.HASH_TREE); for (int index = 0; index < hashTree.length(); index++) { JSONObject item = hashTree.optJSONObject(index); if (item == null) { @@ -140,7 +140,7 @@ public class ApiScenarioReferenceIdService { returnList.add(saveItem); } if (item.has(MsHashTreeService.HASH_TREE)) { - returnList.addAll(this.deepElementRelation(scenario.getId(), item.getJSONArray(MsHashTreeService.HASH_TREE))); + returnList.addAll(this.deepElementRelation(scenario.getId(), item.optJSONArray(MsHashTreeService.HASH_TREE))); } } } @@ -191,7 +191,7 @@ public class ApiScenarioReferenceIdService { deepRelations.add(saveItem); } if (item.has(MsHashTreeService.HASH_TREE)) { - deepRelations.addAll(this.deepElementRelation(scenarioId, item.getJSONArray(MsHashTreeService.HASH_TREE))); + deepRelations.addAll(this.deepElementRelation(scenarioId, item.optJSONArray(MsHashTreeService.HASH_TREE))); } } } diff --git a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioReportStructureService.java b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioReportStructureService.java index b4cebcf392..0f7e0385df 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioReportStructureService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioReportStructureService.java @@ -185,7 +185,7 @@ public class ApiScenarioReportStructureService { StepTreeDTO dto = new StepTreeDTO(name, resourceId, element.optString(TYPE), resourceId, 1); dto.setAllIndex(null); if (element.has(HASH_TREE) && !REQUESTS.contains(dto.getType())) { - JSONArray elementJSONArray = element.getJSONArray(HASH_TREE); + JSONArray elementJSONArray = element.optJSONArray(HASH_TREE); dataFormatting(elementJSONArray, dto, id, reportType); } return dto; @@ -221,7 +221,7 @@ public class ApiScenarioReportStructureService { } dto.getChildren().add(children); if (element.has(HASH_TREE) && !REQUESTS.contains(children.getType())) { - JSONArray elementJSONArray = element.getJSONArray(HASH_TREE); + JSONArray elementJSONArray = element.optJSONArray(HASH_TREE); dataFormatting(elementJSONArray, children, id, reportType); } } diff --git a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java index 480fcc559d..1a7c64dde7 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioService.java @@ -406,7 +406,7 @@ public class ApiScenarioService { return; } JSONObject element = JSONUtil.parseObject(scenario.getScenarioDefinition()); - JSONArray hashTree = element.getJSONArray(ElementConstants.HASH_TREE); + JSONArray hashTree = element.optJSONArray(ElementConstants.HASH_TREE); ApiScenarioImportUtil.formatHashTree(hashTree); setReferenced(hashTree, scenario.getVersionId(), scenario.getProjectId(), apiTestCaseMapper, apiDefinitionMapper, true); scenario.setScenarioDefinition(element.toString()); @@ -731,8 +731,8 @@ public class ApiScenarioService { for (int i = 0; i < valueArray.length(); i++) { try { JSONObject obj = (JSONObject) valueArray.get(i); - JSONObject targetValue = jsonMerge(obj, (JSONObject) target.getJSONArray(key).get(i)); - target.getJSONArray(key).put(i, targetValue); + JSONObject targetValue = jsonMerge(obj, (JSONObject) target.optJSONArray(key).get(i)); + target.optJSONArray(key).put(i, targetValue); } catch (Exception e) { LogUtil.error(e); } @@ -783,7 +783,7 @@ public class ApiScenarioService { ParameterConfig config = new ParameterConfig(); apiScenarioEnvService.setEnvConfig(environmentMap, config); if (config.getConfig() != null && !config.getConfig().isEmpty()) { - ElementUtil.dataSetDomain(element.getJSONArray(ElementConstants.HASH_TREE), config); + ElementUtil.dataSetDomain(element.optJSONArray(ElementConstants.HASH_TREE), config); } return element.toString(); } catch (Exception e) { @@ -1490,7 +1490,7 @@ public class ApiScenarioService { } JSONObject element = JSONUtil.parseObject(scenario.getScenarioDefinition()); if (element != null) { - JSONArray hashTree = element.getJSONArray(ElementConstants.HASH_TREE); + JSONArray hashTree = element.optJSONArray(ElementConstants.HASH_TREE); ApiScenarioImportUtil.formatHashTree(hashTree); setHashTree(hashTree); scenario.setScenarioDefinition(element.toString()); @@ -1511,19 +1511,19 @@ public class ApiScenarioService { String refType = object.optString("refType"); if (StringUtils.isNotEmpty(refType)) { if (refType.equals("CASE")) { - if (object.getJSONArray(ElementConstants.HASH_TREE) == null || object.getJSONArray(ElementConstants.HASH_TREE).length() == 0) { + if (object.optJSONArray(ElementConstants.HASH_TREE) == null || object.optJSONArray(ElementConstants.HASH_TREE).length() == 0) { ApiTestCaseInfo model = extApiTestCaseMapper.selectApiCaseInfoByPrimaryKey(object.optString("id")); if (model != null) { JSONObject element = JSONUtil.parseObject(model.getRequest()); - object.put(ElementConstants.HASH_TREE, element.getJSONArray(ElementConstants.HASH_TREE)); + object.put(ElementConstants.HASH_TREE, element.optJSONArray(ElementConstants.HASH_TREE)); } } } } } if (StringUtils.isNotEmpty(object.optString("refType"))) { - if (object.getJSONArray(ElementConstants.HASH_TREE) != null) { - setHashTree(object.getJSONArray(ElementConstants.HASH_TREE)); + if (object.optJSONArray(ElementConstants.HASH_TREE) != null) { + setHashTree(object.optJSONArray(ElementConstants.HASH_TREE)); } } @@ -2099,12 +2099,12 @@ public class ApiScenarioService { object.put("environmentMap", new HashMap<>()); } if (StringUtils.isNotEmpty(object.optString("refType")) && object.optString("refType").equals("CASE")) { - if (object.has(ElementConstants.HASH_TREE) && object.getJSONArray(ElementConstants.HASH_TREE) != null) { - setReferenced(object.getJSONArray(ElementConstants.HASH_TREE), versionId, projectId, apiTestCaseMapper, apiDefinitionMapper, true); + if (object.has(ElementConstants.HASH_TREE) && object.optJSONArray(ElementConstants.HASH_TREE) != null) { + setReferenced(object.optJSONArray(ElementConstants.HASH_TREE), versionId, projectId, apiTestCaseMapper, apiDefinitionMapper, true); } } else { - if (object.has(ElementConstants.HASH_TREE) && object.getJSONArray(ElementConstants.HASH_TREE) != null) { - setReferenced(object.getJSONArray(ElementConstants.HASH_TREE), versionId, projectId, apiTestCaseMapper, apiDefinitionMapper, false); + if (object.has(ElementConstants.HASH_TREE) && object.optJSONArray(ElementConstants.HASH_TREE) != null) { + setReferenced(object.optJSONArray(ElementConstants.HASH_TREE), versionId, projectId, apiTestCaseMapper, apiDefinitionMapper, false); } } diff --git a/api-test/frontend/src/business/automation/scenario/DebugRun.vue b/api-test/frontend/src/business/automation/scenario/DebugRun.vue index 81924c5784..2e47c388ef 100644 --- a/api-test/frontend/src/business/automation/scenario/DebugRun.vue +++ b/api-test/frontend/src/business/automation/scenario/DebugRun.vue @@ -100,12 +100,9 @@ export default { this.$emit('runRefresh', {}); let url = '/api/automation/run/debug'; - if (this.runData.type === 'UiScenario') { - url = '/ui/automation/run/debug'; - } saveScenario(url, reqObj, this.runData.hashTree, this, (response) => { if (response.data !== "SUCCESS") { - this.$error(response.message ? response.message : this.$t('commons.run_fail')); + this.$error(response.data ? response.data : this.$t('commons.run_fail')); this.$emit('errorRefresh'); } }); diff --git a/api-test/frontend/src/business/definition/components/list/ApiList.vue b/api-test/frontend/src/business/definition/components/list/ApiList.vue index d7f50c88aa..d0f1da0bdf 100644 --- a/api-test/frontend/src/business/definition/components/list/ApiList.vue +++ b/api-test/frontend/src/business/definition/components/list/ApiList.vue @@ -273,7 +273,7 @@ import { initCondition } from "metersphere-frontend/src/utils/tableUtils"; import HeaderLabelOperate from "metersphere-frontend/src/components/head/HeaderLabelOperate"; -import {Body} from "@/business/definition/model/ApiTestModel"; +import {Body, KeyValue} from "@/business/definition/model/ApiTestModel"; import {getGraphByCondition} from "@/api/graph"; import ListItemDeleteConfirm from "metersphere-frontend/src/components/ListItemDeleteConfirm"; import MsSearch from "metersphere-frontend/src/components/search/MsSearch"; @@ -718,6 +718,11 @@ export default { } if (!item.request.headers) { item.request.headers = []; + } else if (item.request.headers.length === 1) { + let values = item.request.headers.filter(tab => tab.name !== ''); + if (values.length > 0) { + item.request.headers.push(new KeyValue({enable: true})) + } } if (!item.request.body.kvs) { item.request.body.kvs = []; @@ -727,8 +732,20 @@ export default { i.files = [] } }) + if (!item.request.rest) { item.request.rest = []; + } else if (item.request.rest.length === 1) { + let values = item.request.rest.filter(tab => tab.name !== ''); + if (values.length > 0) { + item.request.rest.push(new KeyValue({enable: true})) + } + } + if (item.request.query && item.request.query.length === 1) { + let values = item.request.query.filter(tab => tab.name !== ''); + if (values.length > 0) { + item.request.query.push(new KeyValue({enable: true})) + } } if (!item.request.arguments) { item.request.arguments = [{ diff --git a/api-test/frontend/src/business/definition/components/request/http/ApiHttpRequestForm.vue b/api-test/frontend/src/business/definition/components/request/http/ApiHttpRequestForm.vue index 3ee7502dd6..e9c6642dd5 100644 --- a/api-test/frontend/src/business/definition/components/request/http/ApiHttpRequestForm.vue +++ b/api-test/frontend/src/business/definition/components/request/http/ApiHttpRequestForm.vue @@ -34,7 +34,7 @@ {{ $t('api_test.definition.request.query_param') }} -
+
{{ request.arguments.length - 1 }}
@@ -61,7 +61,7 @@ placement="top-start" slot="label"> {{ $t('api_test.definition.request.rest_param') }} -
+
{{ request.rest.length - 1 }}