fix(接口测试): 修复Schema 函数处理问题

--bug=1013195 --user=赵勇 【接口测试】github#13547,接口测试,请求体在json-schema中选择整型,参数值以获取变量的方式定义时,查看实际请求的参数字符类型仍为字符串类型,需修改 https://www.tapd.cn/55049933/s/1159828
This commit is contained in:
fit2-zhao 2022-05-18 14:53:24 +08:00 committed by f2c-ci-robot[bot]
parent c86923a6dc
commit b3fd5bc608
1 changed files with 53 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.google.gson.*; import com.google.gson.*;
import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.NumberUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
@ -70,6 +71,53 @@ public class JSONSchemaRunTest {
return "true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value); 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<Object> 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, private static void analyzeProperty(JSONObject concept, String propertyName,
JsonObject object, Map<String, String> map) { JsonObject object, Map<String, String> map) {
if (!object.has(BasicConstant.TYPE)) { if (!object.has(BasicConstant.TYPE)) {
@ -93,8 +141,7 @@ public class JSONSchemaRunTest {
} }
} }
} catch (Exception e) { } catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString()); valueOf(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString(), propertyName, concept);
concept.put(propertyName, value);
} }
} else if (propertyObjType.equals(BasicConstant.BOOLEAN)) { } else if (propertyObjType.equals(BasicConstant.BOOLEAN)) {
// 先设置空值 // 先设置空值
@ -152,17 +199,17 @@ public class JSONSchemaRunTest {
} else { } else {
array.add(value.longValue()); array.add(value.longValue());
} }
} else {
arrayValueOf(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString(), array);
} }
} catch (Exception e) { } catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString()); arrayValueOf(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString(), array);
array.add(value);
} }
} else if (itemsObject.has(BasicConstant.TYPE) && (itemsObject.has(BasicConstant.ENUM) || itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.STRING))) { } else if (itemsObject.has(BasicConstant.TYPE) && (itemsObject.has(BasicConstant.ENUM) || itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.STRING))) {
array.add(getValue(itemsObject)); array.add(getValue(itemsObject));
} else if (itemsObject.has(BasicConstant.TYPE) && itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.NUMBER)) { } else if (itemsObject.has(BasicConstant.TYPE) && itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.NUMBER)) {
if (isMock(itemsObject)) { if (isMock(itemsObject)) {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString()); arrayValueOf(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString(), array);
array.add(value);
} else { } else {
array.add(0); array.add(0);
} }