From e2d029a925595f1893edf8d35bfbd27f860ef208 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Mon, 7 Mar 2022 13:12:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=97=B6=E5=85=81=E8=AE=B8=E8=AE=BE=E7=BD=AE=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 设置环境变量参数时允许设置为空 --- .../ApiEnvironmentRunningParamService.java | 52 +++++++++---------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java b/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java index d0afa5d36e..692d873f69 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java @@ -26,7 +26,7 @@ public class ApiEnvironmentRunningParamService { @Resource ApiTestEnvironmentMapper testEnvironmentMapper; - public void addParam(String environmentId, Map varMap) { + public void addParam(String environmentId, Map varMap) { if (MapUtils.isEmpty(varMap)) { return; } @@ -42,7 +42,7 @@ public class ApiEnvironmentRunningParamService { if (commonConfig.containsKey("variables")) { JSONArray variables = commonConfig.getJSONArray("variables"); - for (Map.Entry entry: varMap.entrySet()){ + for (Map.Entry entry : varMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); @@ -51,9 +51,9 @@ public class ApiEnvironmentRunningParamService { JSONObject jsonObj = variables.getJSONObject(i); if (jsonObj.containsKey("name") && StringUtils.equals(jsonObj.getString("name"), key)) { contains = true; - if(jsonObj.containsKey("value") && StringUtils.equals(jsonObj.getString("value"), value)){ + if (jsonObj.containsKey("value") && StringUtils.equals(jsonObj.getString("value"), value)) { break; - }else { + } else { envNeedUpdate = true; jsonObj.put("value", value); } @@ -77,7 +77,7 @@ public class ApiEnvironmentRunningParamService { } } else { JSONArray variables = new JSONArray(); - for (Map.Entry entry: varMap.entrySet()){ + for (Map.Entry entry : varMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); @@ -97,7 +97,7 @@ public class ApiEnvironmentRunningParamService { } else { JSONObject commonConfig = new JSONObject(); JSONArray variables = new JSONArray(); - for (Map.Entry entry: varMap.entrySet()) { + for (Map.Entry entry : varMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); JSONObject itemObj = new JSONObject(); @@ -112,7 +112,7 @@ public class ApiEnvironmentRunningParamService { commonConfig.put("variables", variables); configObj.put("commonConfig", commonConfig); } - if(envNeedUpdate){ + if (envNeedUpdate) { environment.setConfig(configObj.toJSONString()); testEnvironmentMapper.updateByPrimaryKeyWithBLOBs(environment); } @@ -124,36 +124,34 @@ public class ApiEnvironmentRunningParamService { public void parseEvn(String envStr) { String[] envStringArr = envStr.split("\n"); - Map> envVarsMap = new HashMap<>(); + Map> envVarsMap = new HashMap<>(); for (String env : envStringArr) { if (StringUtils.contains(env, "=")) { String[] envItem = env.split("="); - if (envItem.length > 1) { - String jmeterVarKey = envItem[0]; - if (this.checkValidity(jmeterVarKey, "MS.ENV.")) { - String[] envAndKeyArr = jmeterVarKey.substring("MS.ENV.".length()).split("\\."); - String envId = envAndKeyArr[0]; - String[] keyArr = ArrayUtils.remove(envAndKeyArr, 0); - String key = StringUtils.join(keyArr, "."); - String value = StringUtils.substring(env, jmeterVarKey.length() + 1); - if (StringUtils.isNoneEmpty(envId, key, value)) { - if(envVarsMap.containsKey(envId)){ - envVarsMap.get(envId).put(key,value); - }else { - Map varMap = new HashMap<>(); - varMap.put(key,value); - envVarsMap.put(envId,varMap); - } + String jmeterVarKey = envItem[0]; + if (this.checkValidity(jmeterVarKey, "MS.ENV.")) { + String[] envAndKeyArr = jmeterVarKey.substring("MS.ENV.".length()).split("\\."); + String envId = envAndKeyArr[0]; + String[] keyArr = ArrayUtils.remove(envAndKeyArr, 0); + String key = StringUtils.join(keyArr, "."); + String value = StringUtils.substring(env, jmeterVarKey.length() + 1); + if (StringUtils.isNoneEmpty(envId, key)) { + if (envVarsMap.containsKey(envId)) { + envVarsMap.get(envId).put(key, value); + } else { + Map varMap = new HashMap<>(); + varMap.put(key, value); + envVarsMap.put(envId, varMap); } } } } } - if(MapUtils.isNotEmpty(envVarsMap)){ - for (Map.Entry> entry: envVarsMap.entrySet()){ + if (MapUtils.isNotEmpty(envVarsMap)) { + for (Map.Entry> entry : envVarsMap.entrySet()) { String envId = entry.getKey(); - Map vars = entry.getValue(); + Map vars = entry.getValue(); this.addParam(envId, vars); } }