From 9dced1f3561816efcf85a12bd8905611f674e368 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 6 May 2021 13:53:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DMock=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=8F=82=E6=95=B0-mock=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=B2=A1=E7=BB=99=E8=BF=94=E5=9B=9E=E5=80=BC=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复Mock服务返回参数-mock数据没给返回值的问题 --- .../api/service/MockConfigService.java | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java index 38f543f82d..6a69f65291 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -12,6 +12,7 @@ import io.metersphere.base.mapper.MockConfigMapper; import io.metersphere.base.mapper.MockExpectConfigMapper; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.JsonPathUtils; +import io.metersphere.commons.utils.ScriptEngineUtils; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.i18n.Translator; import org.apache.commons.lang3.StringUtils; @@ -332,7 +333,16 @@ public class MockConfigService { for (String key : keySet) { try { JsonSchemaReturnObj obj = bodyReturnObj.getObject(key, JsonSchemaReturnObj.class); - returnObj.put(key, obj.getMockValue()); + String values = obj.getMockValue(); + if (StringUtils.isEmpty(values)) { + values = ""; + } else { + try { + values = values.startsWith("@") ? ScriptEngineUtils.calculate(values) : values; + } catch (Exception e) { + } + } + returnObj.put(key, values); } catch (Exception e) { } } @@ -356,7 +366,16 @@ public class MockConfigService { for (int i = 0; i < kvsArr.size(); i++) { JSONObject kv = kvsArr.getJSONObject(i); if (kv.containsKey("name")) { - paramMap.put(kv.getString("name"), kv.getString("value")); + String values = kv.getString("value"); + if (StringUtils.isEmpty(values)) { + values = ""; + } else { + try { + values = values.startsWith("@") ? ScriptEngineUtils.calculate(values) : values; + } catch (Exception e) { + } + } + paramMap.put(kv.getString("name"), values); } } } @@ -370,14 +389,24 @@ public class MockConfigService { if (kv.containsKey("description") && kv.containsKey("files")) { String name = kv.getString("description"); JSONArray fileArr = kv.getJSONArray("files"); - String value = ""; + String allValue = ""; for (int j = 0; j < fileArr.size(); j++) { JSONObject fileObj = fileArr.getJSONObject(j); if (fileObj.containsKey("name")) { - value += fileObj.getString("name") + " ;"; + String values = fileObj.getString("name"); + if (StringUtils.isEmpty(values)) { + values = ""; + } else { + try { + values = values.startsWith("@") ? ScriptEngineUtils.calculate(values) : values; + } catch (Exception e) { + } + } + + allValue += values + " ;"; } } - paramMap.put(name, value); + paramMap.put(name, allValue); } } }