From 7ff11ff95075386a78eea6b1874fca0f6ca37eb2 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Tue, 23 Nov 2021 18:52:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(Mock=E6=B5=8B=E8=AF=95):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8Dmock=E6=B5=8B=E8=AF=95=E5=85=A5=E5=8F=82=E6=98=AF?= =?UTF-8?q?=E5=A4=9A=E5=B1=82json=E6=97=B6=E6=97=A0=E6=B3=95=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=9F=90=E4=B8=AA=E8=8A=82=E7=82=B9=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1008132 --user=宋天阳 【github#7835】json入参含数组时mock自定义脚本匹配成功后无返回值 https://www.tapd.cn/55049933/s/1073422 --- .../io/metersphere/api/dto/mock/MockApiUtils.java | 10 ++++++---- .../mock/Components/MockApiScriptEditor.vue | 13 ++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java b/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java index 6a796310fc..195e74224d 100644 --- a/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java +++ b/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java @@ -102,7 +102,6 @@ public class MockApiUtils { if (bodyObj.containsKey("raw")) { String xmlStr = bodyObj.getString("raw"); JSONObject matchObj = XMLUtils.XmlToJson(xmlStr); -// returnJsonArray.add(matchObj); returnJson = matchObj; } } else if (StringUtils.equalsIgnoreCase(type, "Raw")) { @@ -111,7 +110,6 @@ public class MockApiUtils { if(StringUtils.isNotEmpty(raw)){ JSONObject rawObject = new JSONObject(); rawObject.put("raw",raw); -// returnJsonArray.add(rawObject); returnJson = rawObject; } } @@ -134,7 +132,6 @@ public class MockApiUtils { bodyParamArr.put(kv.getString("name"), values); } } -// returnJsonArray.add(bodyParamArr); returnJson = bodyParamArr; } } @@ -388,11 +385,17 @@ public class MockApiUtils { JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0); for(String key : bodyParamObj.keySet()){ String value = String.valueOf(bodyParamObj.get(key)); + value = StringUtils.replace(value,"\\","\\\\"); + value = StringUtils.replace(value,"\"","\\\""); scriptStringBuffer.append("requestParams.put(\"body."+key+"\",\""+value+"\");\n"); if(StringUtils.equalsIgnoreCase(key,"raw")){ scriptStringBuffer.append("requestParams.put(\"bodyRaw\",\""+value+"\");\n"); } } + String jsonBody = bodyParamObj.toJSONString(); + jsonBody = StringUtils.replace(jsonBody,"\\","\\\\"); + jsonBody = StringUtils.replace(jsonBody,"\"","\\\""); + scriptStringBuffer.append("requestParams.put(\"body.json\",\""+jsonBody+"\");\n"); }else { scriptStringBuffer.append("requestParams.put(\"bodyRaw\",\""+requestMockParams.getBodyParams().toJSONString()+"\");\n"); } @@ -416,7 +419,6 @@ public class MockApiUtils { } return scriptStringBuffer.toString(); } - private static String runScript(String script, String scriptLanguage) throws ScriptException { JSR223Sampler jmeterScriptSampler = new JSR223Sampler(); jmeterScriptSampler.setScriptLanguage(scriptLanguage); diff --git a/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue b/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue index b277894760..5b526e7d05 100644 --- a/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue +++ b/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue @@ -54,7 +54,18 @@ export default { }, { title: this.$t('api_test.request.body')+this.$t('api_test.variable'), - value: 'var returnMsg = requestParams.get("body.${param}");\nreturn returnMsg;', + value: 'var returnMsg = requestParams.get("body.${param}");\nreturn returnMsg;\n' + + "\n"+ + '//如果对象是多层JSON,需要引入fastjson协助解析:\n' + + '// 以"{\"name\":\"user\",\"rows\":[{\"type\":1}]}" 为demo,取rows第1个的type数据:\n' + + 'import com.alibaba.fastjson.JSON;\n'+ + 'import com.alibaba.fastjson.JSONArray;\n'+ + 'import com.alibaba.fastjson.JSONObject;\n'+ + '\n'+ + 'var jsonParam = requestParams.get("body.json");\n' + + 'JSONObject jsonObject = JSONObject.parseObject(jsonParam);\n' + + 'var returnMsg = jsonObject.getJSONArray("rows").getJSONObject(0).getString("type");\n' + + 'return returnMsg;\n', }, { title: this.$t('api_test.request.body')+this.$t('api_test.variable')+" (Raw)",