From 138cd4b3aec5e0bda6286d685c590c4161098062 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 12 Oct 2022 14:55:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20ge?= =?UTF-8?q?tJSONObject=E8=8E=B7=E5=8F=96=E4=B8=8D=E5=8C=85=E5=90=AB?= =?UTF-8?q?=E7=9A=84=E5=B1=9E=E6=80=A7=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/definition/request/ElementUtil.java | 8 ++-- .../request/sampler/MsHTTPSamplerProxy.java | 18 +++----- .../environment/EnvironmentConfig.java | 4 +- .../api/exec/engine/AbstractEngine.java | 4 +- .../ApiEnvironmentRunningParamService.java | 4 +- .../api/parse/HarScenarioAbstractParser.java | 4 +- .../api/parse/MsAbstractParser.java | 8 ++-- .../parse/PostmanAbstractParserParser.java | 4 +- .../api/parse/scenario/MsScenarioParser.java | 2 +- .../api/tcp/server/TCPService.java | 4 +- .../commons/utils/HashTreeUtil.java | 4 +- .../commons/utils/JsonStructUtils.java | 2 +- .../io/metersphere/commons/utils/XMLUtil.java | 4 +- .../commons/utils/mock/MockApiUtils.java | 24 +++++----- .../utils/mock/MockScriptEngineUtils.java | 4 +- .../service/MockConfigService.java | 44 +++++++++---------- .../service/MsHashTreeService.java | 2 +- .../ApiScenarioReferenceIdService.java | 4 +- .../ApiScenarioReportStructureService.java | 2 +- .../service/scenario/ApiScenarioService.java | 4 +- .../scenario/variable/VariableList.vue | 7 ++- 21 files changed, 81 insertions(+), 80 deletions(-) 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 7b4dd81169..084bb80b04 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 @@ -379,7 +379,7 @@ public class ElementUtil { */ public static void relationships(JSONArray hashTree, List referenceRelationships) { for (int i = 0; i < hashTree.length(); i++) { - JSONObject element = hashTree.getJSONObject(i); + JSONObject element = hashTree.optJSONObject(i); if (element != null && StringUtils.equals(element.get(PropertyConstant.TYPE).toString(), ElementConstants.SCENARIO) && StringUtils.equals(element.get("referenced").toString(), "REF")) { if (!referenceRelationships.contains(element.get("id").toString())) { referenceRelationships.add(element.get("id").toString()); @@ -395,7 +395,7 @@ public class ElementUtil { public static void dataFormatting(JSONArray hashTree) { for (int i = 0; i < hashTree.length(); i++) { - JSONObject element = hashTree.getJSONObject(i); + JSONObject element = hashTree.optJSONObject(i); if (element == null) { continue; } @@ -426,7 +426,7 @@ public class ElementUtil { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); for (int i = 0; i < hashTree.length(); i++) { - JSONObject element = hashTree.getJSONObject(i); + JSONObject element = hashTree.optJSONObject(i); boolean isScenarioEnv = false; ParameterConfig config = new ParameterConfig(); if (element != null && element.get(PropertyConstant.TYPE).toString().equals(ElementConstants.SCENARIO)) { @@ -746,7 +746,7 @@ public class ElementUtil { Map> groupMap = new LinkedHashMap<>(); if (elements != null) { for (int i = 0; i < elements.length(); i++) { - JSONObject item = elements.getJSONObject(i); + JSONObject item = elements.optJSONObject(i); if (ElementConstants.ASSERTIONS.equals(item.optString(PropertyConstant.TYPE))) { if (groupMap.containsKey(ASSERTIONS)) { groupMap.get(ASSERTIONS).add(item); diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 34e544f262..bc03dce488 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -3,27 +3,23 @@ package io.metersphere.api.dto.definition.request.sampler; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import io.metersphere.api.dto.mock.MockApiHeaders; -import io.metersphere.api.parse.api.JMeterScriptUtil; import io.metersphere.api.dto.definition.request.ElementUtil; import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.definition.request.assertions.MsAssertions; import io.metersphere.api.dto.definition.request.auth.MsAuthManager; import io.metersphere.api.dto.definition.request.dns.MsDNSCacheManager; +import io.metersphere.api.dto.mock.MockApiHeaders; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.HttpConfig; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.environment.CommonConfig; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; -import io.metersphere.service.definition.ApiTestCaseService; +import io.metersphere.api.parse.api.JMeterScriptUtil; import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.MsTestElementConstants; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.CommonBeanFactory; -import io.metersphere.commons.utils.FileUtils; -import io.metersphere.commons.utils.JSON; -import io.metersphere.commons.utils.LogUtil; +import io.metersphere.commons.utils.*; import io.metersphere.environment.service.CommandService; import io.metersphere.environment.ssl.KeyStoreConfig; import io.metersphere.environment.ssl.KeyStoreFile; @@ -31,8 +27,7 @@ import io.metersphere.environment.ssl.MsKeyStore; import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; -import io.metersphere.commons.utils.HashTreeUtil; -import io.metersphere.commons.utils.JSONUtil; +import io.metersphere.service.definition.ApiTestCaseService; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; @@ -47,6 +42,7 @@ import org.apache.jmeter.protocol.http.util.HTTPConstants; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; import org.apache.jorphan.collections.HashTree; +import org.codehaus.jackson.JsonNode; import org.json.JSONObject; import java.net.URL; @@ -312,9 +308,9 @@ public class MsHTTPSamplerProxy extends MsTestElement { String useEvnId = environmentConfig.getApiEnvironmentid(); if (this.authManager == null && environmentConfig.getAuthManager() != null && environmentConfig.getAuthManager().has("authManager")) { try { - JSONObject authObject = environmentConfig.getAuthManager().getJSONObject("authManager"); + JsonNode authObject = environmentConfig.getAuthManager().get("authManager"); if (authObject != null) { - if (authObject.has("verification") && !StringUtils.equalsIgnoreCase(authObject.optString("verification"), "No Auth")) { + if (authObject.has("verification") && !StringUtils.equalsIgnoreCase(authObject.get("verification").asText(), "No Auth")) { this.authManager = JSON.parseObject(authObject.toString(), MsAuthManager.class); } } diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/environment/EnvironmentConfig.java b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/environment/EnvironmentConfig.java index 5e01e45f74..87e7223592 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/environment/EnvironmentConfig.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/environment/EnvironmentConfig.java @@ -9,7 +9,7 @@ import io.metersphere.api.dto.scenario.HttpConfig; import io.metersphere.api.dto.scenario.TCPConfig; import io.metersphere.environment.ssl.KeyStoreConfig; import lombok.Data; -import org.json.JSONObject; +import org.codehaus.jackson.node.ObjectNode; import java.util.ArrayList; import java.util.List; @@ -31,7 +31,7 @@ public class EnvironmentConfig { private MsJSR223Processor postStepProcessor; //全局前后置脚本都配置 private GlobalScriptConfig globalScriptConfig; - private JSONObject authManager; + private ObjectNode authManager; private List assertions; private boolean useErrorCode; private boolean higherThanSuccess; diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/engine/AbstractEngine.java b/api-test/backend/src/main/java/io/metersphere/api/exec/engine/AbstractEngine.java index ae8d98e3d0..b9c9881ad4 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/engine/AbstractEngine.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/engine/AbstractEngine.java @@ -158,8 +158,8 @@ public abstract class AbstractEngine implements Engine { if (jsonArray.get(i) instanceof List) { JSONArray o = jsonArray.getJSONArray(i); for (int j = 0; j < o.length(); j++) { - if (StringUtils.equals(o.getJSONObject(j).optString("key"), "TargetLevel")) { - s += o.getJSONObject(j).optInt("value"); + if (StringUtils.equals(o.optJSONObject(j).optString("key"), "TargetLevel")) { + s += o.optJSONObject(j).optInt("value"); break; } } 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 59317da1fc..cf7c28bcb7 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 @@ -41,7 +41,7 @@ public class ApiEnvironmentRunningParamService { try { JSONObject configObj = JSONUtil.parseObject(environment.getConfig()); if (configObj.has("commonConfig")) { - JSONObject commonConfig = configObj.getJSONObject("commonConfig"); + JSONObject commonConfig = configObj.optJSONObject("commonConfig"); if (commonConfig.has("variables")) { JSONArray variables = commonConfig.getJSONArray("variables"); List variableList = new LinkedList<>(); @@ -51,7 +51,7 @@ public class ApiEnvironmentRunningParamService { boolean contains = false; for (int i = 0; i < variables.length(); i++) { - JSONObject jsonObj = variables.getJSONObject(i); + JSONObject jsonObj = variables.optJSONObject(i); if (jsonObj.has("name") && StringUtils.equals(jsonObj.optString("name"), key)) { contains = true; if (jsonObj.has("value") && StringUtils.equals(jsonObj.optString("value"), value)) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/HarScenarioAbstractParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/HarScenarioAbstractParser.java index 1525f8601e..7b1e2df957 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/HarScenarioAbstractParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/HarScenarioAbstractParser.java @@ -221,9 +221,9 @@ public abstract class HarScenarioAbstractParser extends ApiImportAbstractPars private void parseRawBody(Body body, JSONObject postmanBody, String bodyMode) { body.setRaw(postmanBody.optString(bodyMode)); body.setType(MsRequestBodyType.RAW.value()); - JSONObject options = postmanBody.getJSONObject("options"); + JSONObject options = postmanBody.optJSONObject("options"); if (options != null) { - JSONObject raw = options.getJSONObject(PostmanRequestBodyMode.RAW.value()); + JSONObject raw = options.optJSONObject(PostmanRequestBodyMode.RAW.value()); if (raw != null) { String bodyType = ""; switch (raw.optString("language")) { 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 1326a8b483..2e041a37fa 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 @@ -17,10 +17,10 @@ import java.util.*; public abstract class MsAbstractParser extends ApiImportAbstractParser { protected List parseMsHTTPSamplerProxy(JSONObject testObject, String tag, boolean isSetUrl) { - JSONObject requests = testObject.getJSONObject(tag); + JSONObject requests = testObject.optJSONObject(tag); List msHTTPSamplerProxies = new ArrayList<>(); requests.keySet().forEach(requestName -> { - JSONObject requestObject = requests.getJSONObject(requestName); + JSONObject requestObject = requests.optJSONObject(requestName); String path = requestObject.optString("url"); String method = requestObject.optString("method"); MsHTTPSamplerProxy request = buildRequest(requestName, path, method); @@ -67,7 +67,7 @@ public abstract class MsAbstractParser extends ApiImportAbstractParser { JSONArray headers = requestObject.getJSONArray("headers"); if (headers != null) { for (int i = 0; i < headers.length(); i++) { - JSONObject header = headers.getJSONObject(i); + JSONObject header = headers.optJSONObject(i); msHeaders.add(new KeyValue(header.optString("name"), header.optString("value"))); } } @@ -88,7 +88,7 @@ public abstract class MsAbstractParser extends ApiImportAbstractParser { msBody.setRaw(bodyStr.toString()); } } else if (body instanceof JSONObject) { - JSONObject bodyObj = requestObject.getJSONObject("body"); + JSONObject bodyObj = requestObject.optJSONObject("body"); if (bodyObj != null) { ArrayList kvs = new ArrayList<>(); bodyObj.keySet().forEach(key -> { diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java index c67be88073..ff6f837086 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java @@ -159,9 +159,9 @@ public abstract class PostmanAbstractParserParser extends ApiImportAbstractPa private void parseRawBody(Body body, JSONObject postmanBody, String bodyMode) { body.setRaw(parseVariable(postmanBody.optString(bodyMode))); body.setType(MsRequestBodyType.RAW.value()); - JSONObject options = postmanBody.getJSONObject("options"); + JSONObject options = postmanBody.optJSONObject("options"); if (options != null) { - JSONObject raw = options.getJSONObject(PostmanRequestBodyMode.RAW.value()); + JSONObject raw = options.optJSONObject(PostmanRequestBodyMode.RAW.value()); if (raw != null) { String bodyType = ""; switch (raw.optString("language")) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/MsScenarioParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/MsScenarioParser.java index cb8502dd64..8a871e7f7a 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/MsScenarioParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/MsScenarioParser.java @@ -63,7 +63,7 @@ public class MsScenarioParser extends MsAbstractParser { if (StringUtils.isNotBlank(scenarioDefinitionStr)) { JSONObject scenarioDefinition = JSONUtil.parseObject(scenarioDefinitionStr); if (scenarioDefinition != null) { - JSONObject environmentMap = scenarioDefinition.getJSONObject("environmentMap"); + JSONObject environmentMap = scenarioDefinition.optJSONObject("environmentMap"); if (environmentMap != null) { scenarioDefinition.put("environmentMap", new HashMap<>()); } diff --git a/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java b/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java index dbca64c14a..8238d6735c 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java @@ -58,14 +58,14 @@ public class TCPService { LogUtil.error(e); } if (responseObj.has("responseResult")) { - JSONObject respResultObj = responseObj.getJSONObject("responseResult"); + JSONObject respResultObj = responseObj.optJSONObject("responseResult"); if (respResultObj.has("body")) { MockApiUtils mockApiUtils = new MockApiUtils(); boolean useScript = false; if (respResultObj.has("usePostScript")) { useScript = respResultObj.getBoolean("usePostScript"); } - returnMsg = mockApiUtils.getResultByResponseResult(matchdMockExpectDTO.getProjectId(), respResultObj.getJSONObject("body"), "", null, null, useScript); + returnMsg = mockApiUtils.getResultByResponseResult(matchdMockExpectDTO.getProjectId(), respResultObj.optJSONObject("body"), "", null, null, useScript); } try { if (respResultObj.has("delayed")) { 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 d7ecb2116b..d40e114fbe 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 @@ -49,12 +49,12 @@ public class HashTreeUtil { try { JSONObject configJson = JSONUtil.parseObject(environment.getConfig()); if (configJson.has(COMMON_CONFIG)) { - JSONObject commonConfig = configJson.getJSONObject(COMMON_CONFIG); + JSONObject commonConfig = configJson.optJSONObject(COMMON_CONFIG); if (commonConfig.has(VARIABLES)) { Map envHeadMap = new HashMap<>(); JSONArray variablesArr = commonConfig.getJSONArray(VARIABLES); for (int i = 0; i < variablesArr.length(); i++) { - JSONObject object = variablesArr.getJSONObject(i); + JSONObject object = variablesArr.optJSONObject(i); if (object.has(NAME) && object.has(VALUE)) { boolean isEnable = true; if (object.has(ENABLE)) { diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/JsonStructUtils.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/JsonStructUtils.java index ee2b671453..8ede6d66a8 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/JsonStructUtils.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/JsonStructUtils.java @@ -122,7 +122,7 @@ public class JsonStructUtils { try { Set matchKeys = matchObj.keySet(); for (int sourceIndex = 0; sourceIndex < sourceArray.length(); sourceIndex++) { - JSONObject sourceObj = sourceArray.getJSONObject(sourceIndex); + JSONObject sourceObj = sourceArray.optJSONObject(sourceIndex); for (String key : matchKeys) { if (sourceObj.has(key)) { Object sourceObjItem = sourceObj.get(key); 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 68ef35ba9d..8ef643f669 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 @@ -43,7 +43,7 @@ public class XMLUtil { if (en == null || en.getValue() == null) continue; if (en.getValue() instanceof JSONObject) { buffer.append(tab).append("<").append(en.getKey()).append(">\n"); - JSONObject jo = jObj.getJSONObject(en.getKey()); + JSONObject jo = jObj.optJSONObject(en.getKey()); jsonToXmlStr(jo, buffer, nowTab.append("\t")); buffer.append(tab).append("\n"); } else if (en.getValue() instanceof JSONArray) { @@ -51,7 +51,7 @@ public class XMLUtil { for (int i = 0; i < array.length(); i++) { buffer.append(tab).append("<").append(en.getKey()).append(">\n"); if (StringUtils.isNotBlank(array.optString(i))) { - JSONObject jsonobject = array.getJSONObject(i); + JSONObject jsonobject = array.optJSONObject(i); jsonToXmlStr(jsonobject, buffer, nowTab.append("\t")); buffer.append(tab).append("\n"); } 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 1dad6cea58..335d13e44d 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 @@ -46,7 +46,7 @@ public class MockApiUtils { public static boolean matchRequestHeader(JSONArray mockExpectHeaderArray, Map requestHeaderMap) { Map mockExpectHeaders = new HashMap<>(); for (int i = 0; i < mockExpectHeaderArray.length(); i++) { - JSONObject obj = mockExpectHeaderArray.getJSONObject(i); + JSONObject obj = mockExpectHeaderArray.optJSONObject(i); if (obj.has("name") && obj.has("value")) { mockExpectHeaders.put(obj.optString("name"), obj.optString("value")); } @@ -87,7 +87,7 @@ public class MockApiUtils { String jsonString = ""; if (isJsonSchema) { if (bodyObj.has("jsonSchema")) { - String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").toString(); + String bodyRetunStr = bodyObj.optJSONObject("jsonSchema").toString(); jsonString = JSONSchemaGenerator.getJson(bodyRetunStr); } } else { @@ -122,7 +122,7 @@ public class MockApiUtils { JSONObject bodyParamArr = new JSONObject(); JSONArray kvsArr = bodyObj.getJSONArray("kvs"); for (int i = 0; i < kvsArr.length(); i++) { - JSONObject kv = kvsArr.getJSONObject(i); + JSONObject kv = kvsArr.optJSONObject(i); if (kv.has("name")) { String values = kv.optString("value"); if (StringUtils.isEmpty(values)) { @@ -150,7 +150,7 @@ public class MockApiUtils { public static List getParamsByJSONArray(JSONArray array) { List requestParamsList = new ArrayList<>(); for (int i = 0; i < array.length(); i++) { - JSONObject obj = array.getJSONObject(i); + JSONObject obj = array.optJSONObject(i); if (obj.has("name") && obj.has("value")) { String condition = null; if (obj.has("rangeType")) { @@ -174,7 +174,7 @@ public class MockApiUtils { if (respObj != null) { if (respObj.has("body")) { String returnStr = ""; - JSONObject bodyObj = respObj.getJSONObject("body"); + JSONObject bodyObj = respObj.optJSONObject("body"); if (bodyObj.has(PropertyConstant.TYPE)) { String type = bodyObj.optString(PropertyConstant.TYPE); if (StringUtils.equals(type, "JSON")) { @@ -187,7 +187,7 @@ public class MockApiUtils { } } if (isJsonSchema && bodyObj.has("jsonSchema")) { - String json = JSONSchemaGenerator.getJson(bodyObj.getJSONObject("jsonSchema").toString()); + String json = JSONSchemaGenerator.getJson(bodyObj.optJSONObject("jsonSchema").toString()); if (StringUtils.isNotEmpty(json)) { returnStr = json; } @@ -206,7 +206,7 @@ public class MockApiUtils { if (bodyObj.has("kvs")) { JSONArray kvsArr = bodyObj.getJSONArray("kvs"); for (int i = 0; i < kvsArr.length(); i++) { - JSONObject kv = kvsArr.getJSONObject(i); + JSONObject kv = kvsArr.optJSONObject(i); if (kv.has("name")) { String values = kv.optString("value"); if (StringUtils.isEmpty(values)) { @@ -231,7 +231,7 @@ public class MockApiUtils { int code = 200; if (statusCodeArray != null) { for (int i = 0; i < statusCodeArray.length(); i++) { - JSONObject object = statusCodeArray.getJSONObject(i); + JSONObject object = statusCodeArray.optJSONObject(i); if (object.has("name") && StringUtils.isNotEmpty(object.optString("name"))) { try { code = Integer.parseInt(object.optString("name")); @@ -248,7 +248,7 @@ public class MockApiUtils { JSONArray jsonArray = respObj.getJSONArray("headers"); Map headMap = new HashMap<>(); for (int i = 0; i < jsonArray.length(); i++) { - JSONObject headObj = jsonArray.getJSONObject(i); + JSONObject headObj = jsonArray.optJSONObject(i); if (headObj.has("name") && headObj.has("value") && headObj.has("enable")) { boolean enable = headObj.getBoolean("enable"); if (enable) { @@ -274,7 +274,7 @@ public class MockApiUtils { if (useScript) { if (bodyObj.has("scriptObject")) { try { - JSONObject scriptObj = bodyObj.getJSONObject("scriptObject"); + JSONObject scriptObj = bodyObj.optJSONObject("scriptObject"); scriptLanguage = scriptObj.optString("scriptLanguage"); script = scriptObj.optString("script"); } catch (Exception e) { @@ -310,7 +310,7 @@ public class MockApiUtils { } } if (isJsonSchema && bodyObj.has("jsonSchema")) { - String json = JSONSchemaGenerator.getJson(bodyObj.getJSONObject("jsonSchema").toString()); + String json = JSONSchemaGenerator.getJson(bodyObj.optJSONObject("jsonSchema").toString()); if (StringUtils.isNotEmpty(json)) { returnStr = json; } @@ -645,7 +645,7 @@ public class MockApiUtils { public static boolean checkParamsCompliance(JSONArray jsonArray, List mockConfigRequestParamList, boolean isAllMatch) { if (jsonArray != null) { for (int i = 0; i < jsonArray.length(); i++) { - JSONObject obj = jsonArray.getJSONObject(i); + JSONObject obj = jsonArray.optJSONObject(i); boolean isMatch = checkParamsCompliance(obj, mockConfigRequestParamList, isAllMatch); if (isMatch) { return true; diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockScriptEngineUtils.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockScriptEngineUtils.java index 35cc75e70a..1913665b03 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockScriptEngineUtils.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockScriptEngineUtils.java @@ -122,7 +122,7 @@ public class MockScriptEngineUtils { if (requestMockParams.getBodyParams() != null) { if (requestMockParams.getBodyParams().length() == 1) { //参数是jsonObject - JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0); + JSONObject bodyParamObj = requestMockParams.getBodyParams().optJSONObject(0); for (String key : bodyParamObj.keySet()) { String value = String.valueOf(bodyParamObj.get(key)); value = StringUtils.replace(value, "\\", "\\\\"); @@ -177,7 +177,7 @@ public class MockScriptEngineUtils { if (requestMockParams.getBodyParams() != null) { if (requestMockParams.getBodyParams().length() == 1) { //参数是jsonObject - JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0); + JSONObject bodyParamObj = requestMockParams.getBodyParams().optJSONObject(0); for (String key : bodyParamObj.keySet()) { String value = String.valueOf(bodyParamObj.get(key)); value = StringUtils.replace(value, "\\", "\\\\"); 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 f31bc80ea1..fb9cce4002 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 @@ -332,12 +332,12 @@ public class MockConfigService { } private boolean isRequestMockExpectMatchingByParams(Map requestHeaderMap, JSONObject mockExpectRequestObj, RequestMockParams requestMockParams) { - JSONObject expectParamsObj = mockExpectRequestObj.getJSONObject("params"); + JSONObject expectParamsObj = mockExpectRequestObj.optJSONObject("params"); if (expectParamsObj.has("headers")) { //检测headers JSONArray headerArr = expectParamsObj.getJSONArray("headers"); for (int i = 0; i < headerArr.length(); i++) { - JSONObject jsonObject = headerArr.getJSONObject(i); + JSONObject jsonObject = headerArr.optJSONObject(i); if (jsonObject.has("name") && jsonObject.has("value")) { String headerName = jsonObject.optString("name"); String headerValue = jsonObject.optString("value"); @@ -351,7 +351,7 @@ public class MockConfigService { } if (expectParamsObj.has("body")) { - JSONObject expectBodyObject = expectParamsObj.getJSONObject("body"); + JSONObject expectBodyObject = expectParamsObj.optJSONObject("body"); JSONArray jsonArray = requestMockParams.getBodyParams(); String type = expectBodyObject.optString(PropertyConstant.TYPE); String paramsFilterType = "And"; @@ -416,7 +416,7 @@ public class MockConfigService { if (StringUtils.equalsIgnoreCase("Array", jsonValidator.getType().name())) { JSONArray mockExpectArr = JSONUtil.parseArray(jsonParams); for (int expectIndex = 0; expectIndex < mockExpectArr.length(); expectIndex++) { - JSONObject itemObj = mockExpectArr.getJSONObject(expectIndex); + JSONObject itemObj = mockExpectArr.optJSONObject(expectIndex); mockExpectJson = itemObj; } } else if (StringUtils.equalsIgnoreCase("Object", jsonValidator.getType().name())) { @@ -426,7 +426,7 @@ public class MockConfigService { } else { JSONArray jsonArray = mockExpectRequestObj.getJSONArray("variables"); for (int i = 0; i < jsonArray.length(); i++) { - JSONObject object = jsonArray.getJSONObject(i); + JSONObject object = jsonArray.optJSONObject(i); String name = ""; String value = ""; if (object.has("name")) { @@ -453,7 +453,7 @@ public class MockConfigService { } if (requestMockParams.getBodyParams() != null) { for (int i = 0; i < requestMockParams.getBodyParams().length(); i++) { - JSONObject reqJsonObj = requestMockParams.getBodyParams().getJSONObject(i); + JSONObject reqJsonObj = requestMockParams.getBodyParams().optJSONObject(i); matchBody = JsonStructUtils.checkJsonObjCompliance(reqJsonObj, mockExpectJson); if (matchBody) { break; @@ -468,7 +468,7 @@ public class MockConfigService { private MockExpectConfigResponse getEmptyRequestMockExpectByParams(Map requestHeaderMap, MockExpectConfigResponse model) { JSONObject requestObj = JSONUtil.parseObject(JSON.toJSONString(model.getRequest())); if (requestObj.has("params")) { - JSONObject paramsObj = requestObj.getJSONObject("params"); + JSONObject paramsObj = requestObj.optJSONObject("params"); if (paramsObj.has("headers")) { JSONArray headArray = paramsObj.getJSONArray("headers"); boolean isHeadMatch = MockApiUtils.matchRequestHeader(headArray, requestHeaderMap); @@ -479,7 +479,7 @@ public class MockConfigService { if (paramsObj.has("rest")) { JSONArray restArray = paramsObj.getJSONArray("rest"); for (int i = 0; i < restArray.length(); i++) { - JSONObject restObj = restArray.getJSONObject(i); + JSONObject restObj = restArray.optJSONObject(i); if (restObj.has("name") && restObj.has("value")) { return null; } @@ -489,7 +489,7 @@ public class MockConfigService { if (paramsObj.has("arguments")) { JSONArray argumentsArray = paramsObj.getJSONArray("arguments"); for (int i = 0; i < argumentsArray.length(); i++) { - JSONObject argumentsObj = argumentsArray.getJSONObject(i); + JSONObject argumentsObj = argumentsArray.optJSONObject(i); if (argumentsObj.has("name") && argumentsObj.has("value")) { return null; } @@ -497,7 +497,7 @@ public class MockConfigService { } //判断请求体为空 if (paramsObj.has("body")) { - JSONObject bodyObj = paramsObj.getJSONObject("body"); + JSONObject bodyObj = paramsObj.optJSONObject("body"); if (bodyObj.has(PropertyConstant.TYPE)) { String type = bodyObj.optString(PropertyConstant.TYPE); if (StringUtils.equalsIgnoreCase(type, "json")) { @@ -515,7 +515,7 @@ public class MockConfigService { if (bodyObj.has("kvs")) { JSONArray kvsArray = bodyObj.getJSONArray("kvs"); for (int i = 0; i < kvsArray.length(); i++) { - JSONObject kvsObj = kvsArray.getJSONObject(i); + JSONObject kvsObj = kvsArray.optJSONObject(i); if (kvsObj.has("name") && kvsObj.has("value")) { return null; } @@ -549,7 +549,7 @@ public class MockConfigService { JSONObject mockExpectJson = new JSONObject(); JSONArray jsonArray = requestObj.getJSONArray("variables"); for (int i = 0; i < jsonArray.length(); i++) { - JSONObject object = jsonArray.getJSONObject(i); + JSONObject object = jsonArray.optJSONObject(i); String name = ""; String value = ""; if (object.has("name")) { @@ -581,11 +581,11 @@ public class MockConfigService { response.setStatus(Integer.parseInt(httpCode)); long sleepTime = 0; if (responseObj.has("responseResult")) { - JSONObject responseJsonObj = responseObj.getJSONObject("responseResult"); + JSONObject responseJsonObj = responseObj.optJSONObject("responseResult"); if (responseJsonObj.has("headers")) { JSONArray jsonArray = responseJsonObj.getJSONArray("headers"); for (int i = 0; i < jsonArray.length(); i++) { - JSONObject object = jsonArray.getJSONObject(i); + JSONObject object = jsonArray.optJSONObject(i); if (object.has("name") && object.has("value")) { String name = String.valueOf(object.get("name")).trim(); String value = String.valueOf(object.get("value")).trim(); @@ -601,7 +601,7 @@ public class MockConfigService { if (responseJsonObj.has("usePostScript")) { useScript = responseJsonObj.getBoolean("usePostScript"); } - returnStr = mockApiUtils.getResultByResponseResult(projectId, responseJsonObj.getJSONObject("body"), url, headerMap, requestMockParams, useScript); + returnStr = mockApiUtils.getResultByResponseResult(projectId, responseJsonObj.optJSONObject("body"), url, headerMap, requestMockParams, useScript); } if (responseJsonObj.has("httpCode")) { int httpCodeNum = 500; @@ -620,7 +620,7 @@ public class MockConfigService { } else { JSONArray jsonArray = responseObj.getJSONArray("httpHeads"); for (int i = 0; i < jsonArray.length(); i++) { - JSONObject object = jsonArray.getJSONObject(i); + JSONObject object = jsonArray.optJSONObject(i); String name = null; String value = ""; if (object.has("name")) { @@ -749,7 +749,7 @@ public class MockConfigService { JSONArray headArr = requestObj.getJSONArray("arguments"); for (int index = 0; index < headArr.length(); index++) { - JSONObject headObj = headArr.getJSONObject(index); + JSONObject headObj = headArr.optJSONObject(index); if (headObj.has("name") && !queryParamList.contains(headObj.has("name"))) { queryParamList.add(String.valueOf(headObj.get("name"))); } @@ -761,7 +761,7 @@ public class MockConfigService { try { JSONArray headArr = requestObj.getJSONArray("rest"); for (int index = 0; index < headArr.length(); index++) { - JSONObject headObj = headArr.getJSONObject(index); + JSONObject headObj = headArr.optJSONObject(index); if (headObj.has("name") && !restParamList.contains(headObj.has("name"))) { restParamList.add(String.valueOf(headObj.get("name"))); } @@ -772,7 +772,7 @@ public class MockConfigService { //请求体参数类型 if (requestObj.has("body")) { try { - JSONObject bodyObj = requestObj.getJSONObject("body"); + JSONObject bodyObj = requestObj.optJSONObject("body"); if (bodyObj.has(PropertyConstant.TYPE)) { String type = bodyObj.optString(PropertyConstant.TYPE); @@ -780,7 +780,7 @@ public class MockConfigService { if (bodyObj.has("kvs")) { JSONArray kvsArr = bodyObj.getJSONArray("kvs"); for (int i = 0; i < kvsArr.length(); i++) { - JSONObject kv = kvsArr.getJSONObject(i); + JSONObject kv = kvsArr.optJSONObject(i); if (kv.has("name") && !formDataList.contains(kv.has("name"))) { formDataList.add(String.valueOf(kv.get("name"))); } @@ -1242,9 +1242,9 @@ public class MockConfigService { try { JSONObject responseObj = JSONUtil.parseObject(model.getResponse()); if (responseObj.has("responseResult")) { - JSONObject responseResultObject = responseObj.getJSONObject("responseResult"); + JSONObject responseResultObject = responseObj.optJSONObject("responseResult"); if (responseResultObject.has("body")) { - responseResultObject.getJSONObject("body").put("apiRspRaw", responseDTO.getReturnData()); + responseResultObject.optJSONObject("body").put("apiRspRaw", responseDTO.getReturnData()); model.setResponse(responseObj.toString()); mockExpectConfigMapper.updateByPrimaryKeySelective(model); 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 ae961dd156..fef22d9243 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 @@ -214,7 +214,7 @@ public class MsHashTreeService { public void dataFormatting(JSONArray hashTree) { for (int i = 0; i < hashTree.length(); i++) { - JSONObject element = hashTree.getJSONObject(i); + JSONObject element = hashTree.optJSONObject(i); if (element != null && StringUtils.equalsIgnoreCase(element.optString(TYPE), SCENARIO)) { element = this.setRefScenario(element); hashTree.put(i, element); 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 9f8763ef29..58239e0822 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 @@ -113,7 +113,7 @@ public class ApiScenarioReferenceIdService { } JSONArray hashTree = jsonObject.getJSONArray(MsHashTreeService.HASH_TREE); for (int index = 0; index < hashTree.length(); index++) { - JSONObject item = hashTree.getJSONObject(index); + JSONObject item = hashTree.optJSONObject(index); if (item == null) { continue; } @@ -166,7 +166,7 @@ public class ApiScenarioReferenceIdService { List deepRelations = new LinkedList<>(); if (hashTree != null) { for (int index = 0; index < hashTree.length(); index++) { - JSONObject item = hashTree.getJSONObject(index); + JSONObject item = hashTree.optJSONObject(index); if (item.has(MsHashTreeService.ID) && item.has(MsHashTreeService.REFERENCED)) { String method = null; String url = null; 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 708852cc31..1f090cd44c 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 @@ -197,7 +197,7 @@ public class ApiScenarioReportStructureService { public static void dataFormatting(JSONArray hashTree, StepTreeDTO dto, String id, String reportType) { for (int i = 0; i < hashTree.length(); i++) { - JSONObject element = hashTree.getJSONObject(i); + JSONObject element = hashTree.optJSONObject(i); if (element != null && element.getBoolean(ENABLE)) { String resourceId = combinationResourceId(element, reportType, id); StepTreeDTO children = new StepTreeDTO(element.optString(NAME), resourceId, element.optString(TYPE), resourceId, element.optInt("index")); 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 48b812cdc7..d5fb031e2c 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 @@ -735,7 +735,7 @@ public class ApiScenarioService { } else { if (value instanceof JSONObject) { JSONObject valueJson = (JSONObject) value; - JSONObject targetValue = jsonMerge(valueJson, target.getJSONObject(key)); + JSONObject targetValue = jsonMerge(valueJson, target.optJSONObject(key)); target.put(key, targetValue); } else if (value instanceof JSONArray) { JSONArray valueArray = (JSONArray) value; @@ -2104,7 +2104,7 @@ public class ApiScenarioService { } JSONObject environmentMap = null; if (object.has("environmentMap")) { - environmentMap = object.getJSONObject("environmentMap"); + environmentMap = object.optJSONObject("environmentMap"); } if (environmentMap != null) { object.put("environmentMap", new HashMap<>()); diff --git a/api-test/frontend/src/business/automation/scenario/variable/VariableList.vue b/api-test/frontend/src/business/automation/scenario/variable/VariableList.vue index 8d183e4e91..1c9a7ea128 100644 --- a/api-test/frontend/src/business/automation/scenario/variable/VariableList.vue +++ b/api-test/frontend/src/business/automation/scenario/variable/VariableList.vue @@ -5,7 +5,7 @@
- +
@@ -255,6 +255,11 @@ export default { }; }, methods: { + reloadTable() { + if (this.$refs.variableTable) { + this.$refs.variableTable.reloadTable(); + } + }, importVariable() { this.$refs.variableImport.open(); },