From 458e68d61a1b4c9d585b891f1683d68898f4353e Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 13 Mar 2023 19:06:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96json=E4=B8=AD=E5=8C=85=E5=90=ABmock?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/api/dto/scenario/Body.java | 38 ++++++++++--------- .../api/exec/generator/JSONSchemaBuilder.java | 3 +- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java index a9b9407301..6e42159a68 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java @@ -22,6 +22,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; @Data @@ -90,37 +92,39 @@ public class Body { private void parseMock() { if (StringUtils.isNotEmpty(this.getRaw())) { String value = StringUtils.chomp(this.getRaw().trim()); - if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) { - List list = JSON.parseArray(this.getRaw()); - if (!this.getRaw().contains(ElementConstants.REF)) { - jsonMockParse(list); + try { + if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) { + List list = JSON.parseArray(this.getRaw()); + if (!this.getRaw().contains(ElementConstants.REF)) { + jsonMockParse(list); + } + this.raw = JSONUtil.parserArray(JSONUtil.toJSONString(list)); + } else { + Map map = JSON.parseObject(this.getRaw(), Map.class); + if (!this.getRaw().contains(ElementConstants.REF)) { + jsonMockParse(map); + } + this.raw = JSONUtil.parserObject(JSONUtil.toJSONString(map)); } - this.raw = JSONUtil.toJSONString(list); - } else { - Map map = JSON.parseObject(this.getRaw(), Map.class); - if (!this.getRaw().contains(ElementConstants.REF)) { - jsonMockParse(map); + } catch (Exception e) { + String pattern = "@[a-zA-Z]*"; + Pattern regex = Pattern.compile(pattern); + Matcher matcher = regex.matcher(this.raw); + while (matcher.find()) { + this.raw = this.raw.replace(matcher.group(), "${__Mock(" + matcher.group() + ")}"); } - this.raw = JSONUtil.toJSONString(map); } } } private void analyticalData() { try { - boolean isArray = false; if (StringUtils.isNotBlank(this.type) && StringUtils.equals(this.type, JSON_STR)) { if (StringUtils.isNotEmpty(this.format) && this.getJsonSchema() != null && JSON_SCHEMA.equals(this.format)) { this.raw = StringEscapeUtils.unescapeJava(JSONSchemaBuilder.generator(JSONUtil.toJSONString(this.getJsonSchema()))); } else { parseMock(); } - // 格式化处理 - if (isArray) { - this.raw = JSONUtil.parserArray(this.raw); - } else { - this.raw = JSONUtil.parserObject(this.raw); - } } } catch (Exception e) { LoggerUtil.error("json mock value is abnormal", e); 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 c28669863b..7006f1eabc 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 @@ -61,6 +61,7 @@ public class JSONSchemaBuilder { String str = ScriptEngineUtils.calculate(evlValue); switch (evlValue) { case "@integer": + case "@natural": concept.put(propertyName, NumberUtils.parseNumber(str, Long.class)); break; case "@boolean": @@ -169,7 +170,7 @@ public class JSONSchemaBuilder { } public static void processArrayValue(Map map, String value) { - String key = StringUtils.join("\"",value, "\""); + String key = StringUtils.join("\"", value, "\""); String targetValue = StringUtils.join(value); map.put(key, targetValue); }