From cf08833361c346647c2c052d05380e39957b6e6f Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 16 Aug 2024 16:06:09 +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=8DJSON=20Schema=20Integer=20=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1044960 --user=赵勇 [接口测试]github#32536请求体内使用Mock的@integer函数并指定类型为integer,请求参数仍然是字符串类型 https://www.tapd.cn/55049933/s/1564131 Signed-off-by: fit2-zhao --- .../api/exec/generator/JSONSchemaBuilder.java | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaBuilder.java b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaBuilder.java index 95a896a029..5da3dbed5f 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaBuilder.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaBuilder.java @@ -18,8 +18,21 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.function.Function; +import java.util.regex.Pattern; public class JSONSchemaBuilder { + private static final Pattern INTEGER_PATTERN = Pattern.compile("^@integer|@natural"); + private static final Pattern BOOLEAN_PATTERN = Pattern.compile("^@boolean"); + private static final Pattern FLOAT_PATTERN = Pattern.compile("^@float"); + private static final Map> handlers = new HashMap<>(); + + static { + handlers.put("@integer", str -> NumberUtils.parseNumber(str, Long.class)); + handlers.put("@boolean", Boolean::parseBoolean); + handlers.put("@float", Float::parseFloat); + } + private static void analyzeSchema(String json, JSONObject rootObj, Map map) { Gson gson = new Gson(); JsonElement element = gson.fromJson(json, JsonElement.class); @@ -63,20 +76,18 @@ public class JSONSchemaBuilder { private static boolean valueOf(String evlValue, String propertyName, JSONObject concept) { if (StringUtils.startsWith(evlValue, "@")) { String str = ScriptEngineUtils.calculate(evlValue); - switch (evlValue) { - case "@integer": - case "@natural": - 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; + if (INTEGER_PATTERN.matcher(evlValue).find()) { + // Parse number as Long + concept.put(propertyName, NumberUtils.parseNumber(str, Long.class)); + } else if (BOOLEAN_PATTERN.matcher(evlValue).find()) { + // Parse boolean + concept.put(propertyName, Boolean.parseBoolean(str)); + } else if (FLOAT_PATTERN.matcher(evlValue).find()) { + // Parse float + concept.put(propertyName, Float.parseFloat(str)); + } else { + // Default case + concept.put(propertyName, str); } return false; } else { @@ -88,25 +99,14 @@ public class JSONSchemaBuilder { private static void arrayValueOf(String evlValue, JSONArray array) { if (StringUtils.startsWith(evlValue, "@")) { String str = ScriptEngineUtils.calculate(evlValue); - switch (evlValue) { - case "@integer": - array.put(NumberUtils.parseNumber(str, Long.class)); - break; - case "@boolean": - array.put(Boolean.parseBoolean(str)); - break; - case "@float": - array.put(Float.parseFloat(str)); - break; - default: - array.put(str); - break; - } + Function handler = handlers.getOrDefault(evlValue, s -> s); + array.put(handler.apply(str)); } else { array.put(ScriptEngineUtils.buildFunctionCallString(evlValue)); } } + private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object, Map map) { if (!object.has(PropertyConstant.TYPE)) { return; @@ -115,14 +115,14 @@ public class JSONSchemaBuilder { if (object.get(PropertyConstant.TYPE) instanceof JsonPrimitive) { propertyObjType = object.get(PropertyConstant.TYPE).getAsString(); } - if (propertyObjType.equals(PropertyConstant.STRING) || propertyObjType.equals(PropertyConstant.ENUM)) { + if (StringUtils.equals(propertyObjType, PropertyConstant.STRING) || StringUtils.equals(propertyObjType, PropertyConstant.ENUM)) { concept.put(propertyName, object.has(PropertyConstant.MOCK) ? FormatterUtil.getMockValue(object) : StringUtils.EMPTY); - } else if (propertyObjType.equals(PropertyConstant.INTEGER) || propertyObjType.equals(PropertyConstant.NUMBER)) { + } else if (StringUtils.equals(propertyObjType, PropertyConstant.INTEGER) || StringUtils.equals(propertyObjType, PropertyConstant.NUMBER)) { try { concept.put(propertyName, 0); if (FormatterUtil.isMockValue(object)) { Number value = FormatterUtil.getElementValue(object).getAsNumber(); - if (value.toString().indexOf(".") == -1) { + if (!value.toString().contains(".")) { concept.put(propertyName, value.longValue()); } else { concept.put(propertyName, new BigDecimal(object.getAsString())); @@ -135,14 +135,14 @@ public class JSONSchemaBuilder { processValue(concept, map, propertyName, value); } } - } else if (propertyObjType.equals(PropertyConstant.BOOLEAN)) { + } else if (StringUtils.equals(propertyObjType, PropertyConstant.BOOLEAN)) { // 先设置空值 concept.put(propertyName, false); try { if (FormatterUtil.isMockValue(object)) { String value = FormatterUtil.getMockValue(object); if (StringUtils.isNotEmpty(value)) { - if (value.indexOf("\"") != -1) { + if (value.contains("\"")) { value = value.replaceAll("\"", StringUtils.EMPTY); } if (isBoolean(value)) { @@ -155,9 +155,9 @@ public class JSONSchemaBuilder { } catch (Exception e) { concept.put(propertyName, false); } - } else if (propertyObjType.equals(PropertyConstant.ARRAY)) { + } else if (StringUtils.equals(propertyObjType, PropertyConstant.ARRAY)) { analyzeArray(concept, propertyName, object, map); - } else if (propertyObjType.equals(PropertyConstant.OBJECT)) { + } else if (StringUtils.equals(propertyObjType, PropertyConstant.OBJECT)) { JSONObject obj = new JSONObject(); concept.put(propertyName, obj); analyzeObject(object, obj, map); @@ -238,7 +238,7 @@ public class JSONSchemaBuilder { } else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.NUMBER)) { JsonElement valueObj = FormatterUtil.getElementValue(itemsObject); String value = valueObj.getAsString(); - if (StringUtils.isNotEmpty(valueObj.getAsString()) && valueObj.getAsString().indexOf(".") != -1) { + if (StringUtils.isNotEmpty(valueObj.getAsString()) && valueObj.getAsString().contains(".")) { array.put(new BigDecimal(value)); } else { array.put(Integer.valueOf(value));