From b3fd5bc60899200a66ddaacbe9ac24f0e08159f2 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 18 May 2022 14:53:24 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8DSchema=20=E5=87=BD=E6=95=B0=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1013195 --user=赵勇 【接口测试】github#13547,接口测试,请求体在json-schema中选择整型,参数值以获取变量的方式定义时,查看实际请求的参数字符类型仍为字符串类型,需修改 https://www.tapd.cn/55049933/s/1159828 --- .../commons/json/JSONSchemaRunTest.java | 59 +++++++++++++++++-- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java index 8c8afe682c..31a1b591a9 100644 --- a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java +++ b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaRunTest.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject; import com.google.gson.*; import io.metersphere.jmeter.utils.ScriptEngineUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.util.NumberUtils; import java.util.HashMap; import java.util.LinkedList; @@ -70,6 +71,53 @@ public class JSONSchemaRunTest { return "true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value); } + private static void valueOf(String evlValue, String propertyName, JSONObject concept) { + if (StringUtils.startsWith(evlValue, "@")) { + String str = ScriptEngineUtils.calculate(evlValue); + switch (evlValue) { + case "@integer": + concept.put(propertyName, NumberUtils.parseNumber(str, Long.class)); + break; + case "@boolean": + concept.put(propertyName, Boolean.parseBoolean(str)); + break; + case "@float": + concept.put(propertyName, Float.parseFloat(str)); + break; + default: + concept.put(propertyName, str); + break; + } + } else { + String value = ScriptEngineUtils.buildFunctionCallString(evlValue); + concept.put(propertyName, value); + } + } + + private static void arrayValueOf(String evlValue, List array) { + if (StringUtils.startsWith(evlValue, "@")) { + String str = ScriptEngineUtils.calculate(evlValue); + switch (evlValue) { + case "@integer": + array.add(NumberUtils.parseNumber(str, Long.class)); + break; + case "@boolean": + array.add(Boolean.parseBoolean(str)); + break; + case "@float": + array.add(Float.parseFloat(str)); + break; + default: + array.add(str); + break; + } + } else { + String value = ScriptEngineUtils.buildFunctionCallString(evlValue); + array.add(value); + } + } + + private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object, Map map) { if (!object.has(BasicConstant.TYPE)) { @@ -93,8 +141,7 @@ public class JSONSchemaRunTest { } } } catch (Exception e) { - String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString()); - concept.put(propertyName, value); + valueOf(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString(), propertyName, concept); } } else if (propertyObjType.equals(BasicConstant.BOOLEAN)) { // 先设置空值 @@ -152,17 +199,17 @@ public class JSONSchemaRunTest { } else { array.add(value.longValue()); } + } else { + arrayValueOf(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString(), array); } } catch (Exception e) { - String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString()); - array.add(value); + arrayValueOf(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString(), array); } } else if (itemsObject.has(BasicConstant.TYPE) && (itemsObject.has(BasicConstant.ENUM) || itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.STRING))) { array.add(getValue(itemsObject)); } else if (itemsObject.has(BasicConstant.TYPE) && itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.NUMBER)) { if (isMock(itemsObject)) { - String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString()); - array.add(value); + arrayValueOf(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString(), array); } else { array.add(0); }