From af896c93859ce777fe14590b0a16eed46fa39957 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 22 Jul 2021 18:25:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E4=BF=AE=E5=A4=8Djson-schema=E5=8B=BE=E9=80=89?= =?UTF-8?q?=E5=BF=85=E5=A1=AB=E5=90=8E=E5=88=87=E5=9B=9Ejson=E6=8A=A5?= =?UTF-8?q?=E9=94=99=20#1005335=20--bug=3D1005335=20--user=3D=E8=B5=B5?= =?UTF-8?q?=E5=8B=87=20=E3=80=90github#4790=E3=80=91json-...=20https://www?= =?UTF-8?q?.tapd.cn/55049933/s/1028239?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/json/JSONSchemaGenerator.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java index fe04a92d64..89c84529ef 100644 --- a/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java +++ b/backend/src/main/java/io/metersphere/commons/json/JSONSchemaGenerator.java @@ -312,6 +312,21 @@ public class JSONSchemaGenerator { private static final SyntaxValidator VALIDATOR = new SyntaxValidator(ValidationConfiguration.byDefault()); + private static String formerJson(String jsonSchema) { + try { + JSONObject root = new JSONObject(); + generator(jsonSchema, root); + // 格式化返回 + Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create(); + if (root.get("MS-OBJECT") != null) { + return gson.toJson(root.get("MS-OBJECT")); + } + return gson.toJson(root); + } catch (Exception e) { + return jsonSchema; + } + } + public static String getJson(String jsonSchema) { if (StringUtils.isEmpty(jsonSchema)) { return null; @@ -320,20 +335,17 @@ public class JSONSchemaGenerator { JsonNode jsonNode = JsonLoader.fromString(jsonSchema); ProcessingReport report = VALIDATOR.validateSchema(jsonNode); if (report.isSuccess()) { - JSONObject root = new JSONObject(); - generator(jsonSchema, root); - // 格式化返回 - Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create(); - if (root.get("MS-OBJECT") != null) { - return gson.toJson(root.get("MS-OBJECT")); - } - return gson.toJson(root); + return formerJson(jsonSchema); } else { return report.getExceptionThreshold().toString(); } } catch (Exception ex) { ex.printStackTrace(); - return ex.getMessage(); + try { + return formerJson(jsonSchema); + } catch (Exception e) { + return jsonSchema; + } } } }