From 230a3b3c2c3cc0bb70ed3904dbc14fe75392b533 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 9 Jan 2023 17:13:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8DjsonSchema=E9=A2=84=E8=A7=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E6=9C=AA=E6=A0=BC=E5=BC=8F=E5=8C=96=E7=9A=84=E7=BC=BA?= =?UTF-8?q?=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1021690 --user=王孝刚 【接口测试】github #21168开启json-schema,后预览json数据显示不全,且数据未格式化 https://www.tapd.cn/55049933/s/1324583 --- .../io/metersphere/api/dto/scenario/Body.java | 6 ++--- .../exec/generator/JSONSchemaGenerator.java | 7 +++++- .../api/exec/generator/JSONSchemaRunTest.java | 7 +++++- .../metersphere/commons/utils/JSONUtil.java | 23 ++++++++++++++----- .../commons/json-schema/JsonSchemaEditor.vue | 2 +- 5 files changed, 33 insertions(+), 12 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 35aaf9bd51..46256521ef 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 @@ -99,7 +99,7 @@ public class Body { 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(JSONSchemaRunTest.getJson(JSON.toJSONString(this.getJsonSchema()))); + this.raw = StringEscapeUtils.unescapeJava(JSONSchemaRunTest.getJson(JSONUtil.toJSONString(this.getJsonSchema()))); } else { try { if (StringUtils.isNotEmpty(this.getRaw())) { @@ -109,13 +109,13 @@ public class Body { if (!this.getRaw().contains("$ref")) { jsonMockParse(list); } - this.raw = JSONUtil.parser(JSONUtil.toJSONString(list)); + this.raw = JSONUtil.parserArray(JSONUtil.toJSONString(list)); } else { Map map = JSON.parseObject(this.getRaw(), Map.class); if (!this.getRaw().contains("$ref")) { jsonMockParse(map); } - this.raw = JSONUtil.parser(JSONUtil.toJSONString(map)); + this.raw = JSONUtil.parserObject(JSONUtil.toJSONString(map)); } } } catch (Exception e) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java index d1feffe545..35723e34ab 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java @@ -272,7 +272,12 @@ public class JSONSchemaGenerator { if (StringUtils.isEmpty(jsonSchema)) { return null; } - return formerJson(jsonSchema); + String value = formerJson(jsonSchema); + if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) { + return JSONUtil.parserArray(value); + } else { + return JSONUtil.parserObject(value); + } } catch (Exception ex) { return jsonSchema; } diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java index 6770dfa4fc..1d2b827ea3 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java @@ -291,7 +291,12 @@ public class JSONSchemaRunTest { json = json.replace(str, map.get(str)); } } - return JSONUtil.parser(json); + String value = StringUtils.chomp(json.trim()); + if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) { + return JSONUtil.parserArray(value); + } else { + return JSONUtil.parserObject(value); + } } catch (Exception ex) { return jsonSchema; } diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java index 28f6656622..103b1ba1ae 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java @@ -32,6 +32,13 @@ public class JSONUtil { private static final ObjectMapper objectMapper = new ObjectMapper(); private static final TypeFactory typeFactory = objectMapper.getTypeFactory(); + private static final Gson gson = new GsonBuilder() + .setPrettyPrinting() + .disableHtmlEscaping() + .serializeNulls() + .create(); + ; + static { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 自动检测所有类的全部属性 @@ -39,6 +46,7 @@ public class JSONUtil { // 如果一个对象中没有任何的属性,那么在序列化的时候就会报错 objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + } public static T parseObject(String content, Class valueType) { @@ -241,19 +249,22 @@ public class JSONUtil { return objectMapper.createObjectNode(); } - public static String parser(String content) { + public static String parserObject(String content) { try { - Gson gson = new GsonBuilder() - .setPrettyPrinting() - .disableHtmlEscaping() - .serializeNulls() - .create(); return gson.toJson(JsonParser.parseString(content).getAsJsonObject()); } catch (Exception e) { return content; } } + public static String parserArray(String content) { + try { + return gson.toJson(JsonParser.parseString(content).getAsJsonArray()); + } catch (Exception e) { + return content; + } + } + public static LinkedList readValue(String content) { try { return objectMapper.readValue(content, new TypeReference>() { diff --git a/api-test/frontend/src/business/commons/json-schema/JsonSchemaEditor.vue b/api-test/frontend/src/business/commons/json-schema/JsonSchemaEditor.vue index b6f666ef55..33adc27d5f 100644 --- a/api-test/frontend/src/business/commons/json-schema/JsonSchemaEditor.vue +++ b/api-test/frontend/src/business/commons/json-schema/JsonSchemaEditor.vue @@ -43,7 +43,7 @@
-
{{ this.preview }}
+
{{ this.preview }}