fix(接口自动化): 修复json-schema勾选必填后切回json报错 #1005335

--bug=1005335 --user=赵勇 【github#4790】json-... https://www.tapd.cn/55049933/s/1028239
This commit is contained in:
fit2-zhao 2021-07-22 18:25:33 +08:00 committed by fit2-zhao
parent 1175dd747d
commit af896c9385
1 changed files with 21 additions and 9 deletions

View File

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