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 }}