refactor(接口测试): 文档结构解析代码优化统一常量

This commit is contained in:
fit2-zhao 2022-01-25 15:38:01 +08:00 committed by fit2-zhao
parent 74628bf74a
commit c1f36caf4a
6 changed files with 303 additions and 299 deletions

View File

@ -0,0 +1,20 @@
package io.metersphere.commons.json;
public class BasicConstant {
public final static String REQUIRED = "required";
public final static String ALL_OF = "allOf";
public final static String PROPERTIES = "properties";
public final static String TYPE = "type";
public final static String MS_OBJECT = "MS-OBJECT";
public final static String ARRAY = "array";
public final static String OBJECT = "object";
public final static String DEFAULT = "default";
public final static String STRING = "string";
public final static String BOOLEAN = "boolean";
public final static String NUMBER = "number";
public final static String INTEGER = "integer";
public final static String MOCK = "mock";
public final static String ENUM = "enum";
public final static String ITEMS = "items";
public final static String ITEM = "_item";
}

View File

@ -2,7 +2,7 @@ package io.metersphere.commons.json;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.google.gson.*; import com.google.gson.*;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.EnumPropertyUtil;
import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -17,19 +17,14 @@ public class JSONSchemaGenerator {
} }
private static void analyzeSchema(String json, JSONObject rootObj) { private static void analyzeSchema(String json, JSONObject rootObj) {
try {
Gson gson = new Gson(); Gson gson = new Gson();
JsonElement element = gson.fromJson(json, JsonElement.class); JsonElement element = gson.fromJson(json, JsonElement.class);
JsonObject rootElement = element.getAsJsonObject(); JsonObject rootElement = element.getAsJsonObject();
analyzeRootSchemaElement(rootElement, rootObj); analyzeRootSchemaElement(rootElement, rootObj);
} catch (Exception e) {
LogUtil.error(e);
}
} }
private static void analyzeRootSchemaElement(JsonObject rootElement, JSONObject rootObj) { private static void analyzeRootSchemaElement(JsonObject rootElement, JSONObject rootObj) {
if (rootElement.has("type") || rootElement.has("allOf")) { if (rootElement.has(BasicConstant.TYPE) || rootElement.has(BasicConstant.ALL_OF)) {
analyzeObject(rootElement, rootObj); analyzeObject(rootElement, rootObj);
} }
if (rootElement.has("definitions")) { if (rootElement.has("definitions")) {
@ -38,41 +33,41 @@ public class JSONSchemaGenerator {
} }
private static void analyzeObject(JsonObject object, JSONObject rootObj) { private static void analyzeObject(JsonObject object, JSONObject rootObj) {
if (object.has("allOf")) { if (object != null && object.has(BasicConstant.ALL_OF)) {
JsonArray allOfArray = object.get("allOf").getAsJsonArray(); JsonArray allOfArray = object.get(BasicConstant.ALL_OF).getAsJsonArray();
for (JsonElement allOfElement : allOfArray) { for (JsonElement allOfElement : allOfArray) {
JsonObject allOfElementObj = allOfElement.getAsJsonObject(); JsonObject allOfElementObj = allOfElement.getAsJsonObject();
if (allOfElementObj.has("properties")) { if (!allOfElementObj.has(BasicConstant.PROPERTIES)) {
// Properties elements will become the attributes/references of the element continue;
JsonObject propertiesObj = allOfElementObj.get("properties").getAsJsonObject(); }
JsonObject propertiesObj = allOfElementObj.get(BasicConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) { for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey(); String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(rootObj, propertyKey, propertyObj); analyzeProperty(rootObj, propertyKey, propertyObj);
} }
} }
} } else if (object.has(BasicConstant.PROPERTIES)) {
} else if (object.has("properties")) { JsonObject propertiesObj = object.get(BasicConstant.PROPERTIES).getAsJsonObject();
JsonObject propertiesObj = object.get("properties").getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) { for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey(); String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(rootObj, propertyKey, propertyObj); analyzeProperty(rootObj, propertyKey, propertyObj);
} }
} else if (object.has("type") && object.get("type").getAsString().equals("array")) { } else if (object.has(BasicConstant.TYPE) && object.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.ARRAY)) {
analyzeProperty(rootObj, "MS-OBJECT", object); analyzeProperty(rootObj, BasicConstant.MS_OBJECT, object);
} else if (object.has("type") && !object.get("type").getAsString().equals("object")) { } else if (object.has(BasicConstant.TYPE) && !object.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.OBJECT)) {
analyzeProperty(rootObj, object.getAsString(), object); analyzeProperty(rootObj, object.getAsString(), object);
} }
} }
private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object) { private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object) {
if (object.has("type")) { if (object.has(BasicConstant.TYPE)) {
String propertyObjType = null; String propertyObjType = "";
if (object.get("type") instanceof JsonPrimitive) { if (object.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
propertyObjType = object.get("type").getAsString(); propertyObjType = object.get(BasicConstant.TYPE).getAsString();
} else if (object.get("type") instanceof JsonArray) { } else if (object.get(BasicConstant.TYPE) instanceof JsonArray) {
JsonArray typeArray = (JsonArray) object.get("type").getAsJsonArray(); JsonArray typeArray = (JsonArray) object.get(BasicConstant.TYPE).getAsJsonArray();
propertyObjType = typeArray.get(0).getAsString(); propertyObjType = typeArray.get(0).getAsString();
if (typeArray.size() > 1) { if (typeArray.size() > 1) {
if (typeArray.get(1).getAsString().equals("null")) { if (typeArray.get(1).getAsString().equals("null")) {
@ -80,15 +75,15 @@ public class JSONSchemaGenerator {
} }
} }
} }
if (object.has("default")) { if (object.has(BasicConstant.DEFAULT)) {
concept.put(propertyName, object.get("default")); concept.put(propertyName, object.get(BasicConstant.DEFAULT));
} else if (object.has("enum")) { } else if (object.has(BasicConstant.ENUM)) {
try { try {
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
Object value = object.get("mock").getAsJsonObject().get("mock"); Object value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK);
concept.put(propertyName, value); concept.put(propertyName, value);
} else { } else {
List<Object> list = analyzeEnumProperty(object); List<Object> list = EnumPropertyUtil.analyzeEnumProperty(object);
if (list.size() > 0) { if (list.size() > 0) {
int index = (int) (Math.random() * list.size()); int index = (int) (Math.random() * list.size());
concept.put(propertyName, list.get(index)); concept.put(propertyName, list.get(index));
@ -97,7 +92,7 @@ public class JSONSchemaGenerator {
} catch (Exception e) { } catch (Exception e) {
concept.put(propertyName, ""); concept.put(propertyName, "");
} }
} else if (propertyObjType.equals("string")) { } else if (propertyObjType.equals(BasicConstant.STRING)) {
// 先设置空值 // 先设置空值
concept.put(propertyName, ""); concept.put(propertyName, "");
if (object.has("format")) { if (object.has("format")) {
@ -106,56 +101,60 @@ public class JSONSchemaGenerator {
} }
} }
if (object.has("default")) { if (object.has(BasicConstant.DEFAULT)) {
concept.put(propertyName, object.get("default")); concept.put(propertyName, object.get(BasicConstant.DEFAULT));
} }
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString()) && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); && StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
concept.put(propertyName, value); concept.put(propertyName, value);
} }
} else if (propertyObjType.equals("integer")) { } else if (propertyObjType.equals(BasicConstant.INTEGER)) {
// 先设置空值 // 先设置空值
concept.put(propertyName, 0); concept.put(propertyName, 0);
if (object.has("default")) { if (object.has(BasicConstant.DEFAULT)) {
concept.put(propertyName, object.get("default")); concept.put(propertyName, object.get(BasicConstant.DEFAULT));
} }
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
try { try {
int value = object.get("mock").getAsJsonObject().get("mock").getAsInt(); int value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsInt();
concept.put(propertyName, value); concept.put(propertyName, value);
} catch (Exception e) { } catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
concept.put(propertyName, value); concept.put(propertyName, value);
} }
} }
} else if (propertyObjType.equals("number")) { } else if (propertyObjType.equals(BasicConstant.NUMBER)) {
// 先设置空值 // 先设置空值
concept.put(propertyName, 0); concept.put(propertyName, 0);
if (object.has("default")) { if (object.has(BasicConstant.DEFAULT)) {
concept.put(propertyName, object.get("default")); concept.put(propertyName, object.get(BasicConstant.DEFAULT));
} }
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
try { try {
Number value = object.get("mock").getAsJsonObject().get("mock").getAsNumber(); Number value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsNumber();
if (value.toString().indexOf(".") == -1) { if (value.toString().indexOf(".") == -1) {
concept.put(propertyName, value.longValue()); concept.put(propertyName, value.longValue());
} else { } else {
concept.put(propertyName, value.floatValue()); concept.put(propertyName, value.floatValue());
} }
} catch (Exception e) { } catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
concept.put(propertyName, value); concept.put(propertyName, value);
} }
} }
} else if (propertyObjType.equals("boolean")) { } else if (propertyObjType.equals(BasicConstant.BOOLEAN)) {
// 先设置空值 // 先设置空值
concept.put(propertyName, false); concept.put(propertyName, false);
if (object.has("default")) { if (object.has(BasicConstant.DEFAULT)) {
concept.put(propertyName, object.get("default")); concept.put(propertyName, object.get(BasicConstant.DEFAULT));
} }
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").toString()); && StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).toString());
try { try {
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
if (value.indexOf("\"") != -1) { if (value.indexOf("\"") != -1) {
@ -167,59 +166,59 @@ public class JSONSchemaGenerator {
concept.put(propertyName, value); concept.put(propertyName, value);
} }
} }
} else if (propertyObjType.equals("array")) { } else if (propertyObjType.equals(BasicConstant.ARRAY)) {
// 先设置空值 // 先设置空值
List<Object> array = new LinkedList<>(); List<Object> array = new LinkedList<>();
JsonArray jsonArray = new JsonArray(); JsonArray jsonArray = new JsonArray();
if (object.has("items")) { if (object.has(BasicConstant.ITEMS)) {
if(object.get("items").isJsonArray()){ if (object.get(BasicConstant.ITEMS).isJsonArray()) {
jsonArray = object.get("items").getAsJsonArray(); jsonArray = object.get(BasicConstant.ITEMS).getAsJsonArray();
} else { } else {
JsonObject itemsObject = object.get("items").getAsJsonObject(); JsonObject itemsObject = object.get(BasicConstant.ITEMS).getAsJsonObject();
array.add(itemsObject); array.add(itemsObject);
} }
} }
for (int i = 0; i < jsonArray.size(); i++) { for (int i = 0; i < jsonArray.size(); i++) {
JsonObject itemsObject = jsonArray.get(i).getAsJsonObject(); JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
if (object.has("items")) { if (object.has(BasicConstant.ITEMS)) {
if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) { if (jsonObject.has(BasicConstant.MOCK) && jsonObject.get(BasicConstant.MOCK).getAsJsonObject() != null
&& StringUtils.isNotEmpty(jsonObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
try { try {
if(itemsObject.has("type") && itemsObject.get("type").getAsString().equals("integer")){ if (jsonObject.has(BasicConstant.TYPE) && jsonObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.INTEGER)) {
int value = itemsObject.get("mock").getAsJsonObject().get("mock").getAsInt(); int value = jsonObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsInt();
array.add(value); array.add(value);
} else { } else {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
array.add(value); array.add(value);
} }
} catch (Exception e) { } catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
array.add(value); array.add(value);
} }
} else if (itemsObject.has("enum")) { } else if (jsonObject.has(BasicConstant.ENUM)) {
array.add(analyzeEnumProperty(itemsObject)); array.add(EnumPropertyUtil.analyzeEnumProperty(jsonObject));
} else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("string")) { } else if (jsonObject.has(BasicConstant.TYPE) && jsonObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.STRING)) {
if (itemsObject.has("default")) { if (jsonObject.has(BasicConstant.DEFAULT)) {
array.add(itemsObject.get("default")); array.add(jsonObject.get(BasicConstant.DEFAULT));
} else if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) { } else if (jsonObject.has(BasicConstant.MOCK) && jsonObject.get(BasicConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(jsonObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
array.add(value); array.add(value);
} else { } else {
array.add(""); array.add("");
} }
} else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("number")) { } else if (jsonObject.has(BasicConstant.TYPE) && jsonObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.NUMBER)) {
if (itemsObject.has("default")) { if (jsonObject.has(BasicConstant.DEFAULT)) {
array.add(itemsObject.get("default")); array.add(jsonObject.get(BasicConstant.DEFAULT));
} else if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) { } else if (jsonObject.has(BasicConstant.MOCK) && jsonObject.get(BasicConstant.MOCK).getAsJsonObject() != null
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString()); && StringUtils.isNotEmpty(jsonObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
array.add(value); array.add(value);
} else { } else {
array.add(0); array.add(0);
} }
} else if (itemsObject.has("properties")) { } else if (jsonObject.has(BasicConstant.PROPERTIES)) {
JSONObject propertyConcept = new JSONObject(true); JSONObject propertyConcept = new JSONObject(true);
JsonObject propertiesObj = itemsObject.get("properties").getAsJsonObject(); JsonObject propertiesObj = jsonObject.get(BasicConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) { for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey(); String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
@ -227,18 +226,18 @@ public class JSONSchemaGenerator {
} }
array.add(propertyConcept); array.add(propertyConcept);
} else if (itemsObject.has("type") && itemsObject.get("type") instanceof JsonPrimitive) { } else if (jsonObject.has(BasicConstant.TYPE) && jsonObject.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
JSONObject newJsonObj = new JSONObject(); JSONObject newJsonObj = new JSONObject();
analyzeProperty(newJsonObj, propertyName + "_item", itemsObject); analyzeProperty(newJsonObj, propertyName + BasicConstant.ITEM, jsonObject);
array.add(newJsonObj.get(propertyName + "_item")); array.add(newJsonObj.get(propertyName + BasicConstant.ITEM));
} }
} else if (object.has("items") && object.get("items").isJsonArray()) { } else if (object.has(BasicConstant.ITEMS) && object.get(BasicConstant.ITEMS).isJsonArray()) {
JsonArray itemsObjectArray = object.get("items").getAsJsonArray(); JsonArray itemsObjectArray = object.get(BasicConstant.ITEMS).getAsJsonArray();
array.add(itemsObjectArray); array.add(itemsObjectArray);
} }
} }
concept.put(propertyName, array); concept.put(propertyName, array);
} else if (propertyObjType.equals("object")) { } else if (propertyObjType.equals(BasicConstant.OBJECT)) {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
concept.put(propertyName, obj); concept.put(propertyName, obj);
analyzeObject(object, obj); analyzeObject(object, obj);
@ -248,26 +247,6 @@ public class JSONSchemaGenerator {
} }
} }
private static List<Object> analyzeEnumProperty(JsonObject object) {
List<Object> list = new LinkedList<>();
String jsonStr = null;
try {
JsonArray enumValues = object.get("enum").getAsJsonArray();
for (JsonElement enumValueElem : enumValues) {
String enumValue = enumValueElem.getAsString();
list.add(enumValue);
}
} catch (Exception e) {
jsonStr = object.get("enum").getAsString();
}
if (jsonStr != null && list.isEmpty()) {
String[] arrays = jsonStr.split("\n");
for (String str : arrays) {
list.add(str);
}
}
return list;
}
private static void analyzeDefinitions(JsonObject object) { private static void analyzeDefinitions(JsonObject object) {
JsonObject definitionsObj = object.get("definitions").getAsJsonObject(); JsonObject definitionsObj = object.get("definitions").getAsJsonObject();
@ -279,20 +258,15 @@ public class JSONSchemaGenerator {
} }
} }
private static String formerJson(String jsonSchema) { private static String formerJson(String jsonSchema) {
try {
JSONObject root = new JSONObject(true); JSONObject root = new JSONObject(true);
generator(jsonSchema, root); generator(jsonSchema, root);
// 格式化返回 // 格式化返回
Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create(); Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create();
if (root.get("MS-OBJECT") != null) { if (root.get(BasicConstant.MS_OBJECT) != null) {
return gson.toJson(root.get("MS-OBJECT")); return gson.toJson(root.get(BasicConstant.MS_OBJECT));
} }
return gson.toJson(root); return gson.toJson(root);
} catch (Exception e) {
return jsonSchema;
}
} }
public static String getJson(String jsonSchema) { public static String getJson(String jsonSchema) {

View File

@ -2,7 +2,6 @@ package io.metersphere.commons.json;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.google.gson.*; import com.google.gson.*;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -17,19 +16,14 @@ public class JSONSchemaRunTest {
} }
private static void analyzeSchema(String json, JSONObject rootObj) { private static void analyzeSchema(String json, JSONObject rootObj) {
try {
Gson gson = new Gson(); Gson gson = new Gson();
JsonElement element = gson.fromJson(json, JsonElement.class); JsonElement element = gson.fromJson(json, JsonElement.class);
JsonObject rootElement = element.getAsJsonObject(); JsonObject rootElement = element.getAsJsonObject();
analyzeRootSchemaElement(rootElement, rootObj); analyzeRootSchemaElement(rootElement, rootObj);
} catch (Exception e) {
LogUtil.error(e);
}
} }
private static void analyzeRootSchemaElement(JsonObject rootElement, JSONObject rootObj) { private static void analyzeRootSchemaElement(JsonObject rootElement, JSONObject rootObj) {
if (rootElement.has("type") || rootElement.has("allOf")) { if (rootElement.has(BasicConstant.TYPE) || rootElement.has(BasicConstant.ALL_OF)) {
analyzeObject(rootElement, rootObj); analyzeObject(rootElement, rootObj);
} }
if (rootElement.has("definitions")) { if (rootElement.has("definitions")) {
@ -38,12 +32,12 @@ public class JSONSchemaRunTest {
} }
private static void analyzeObject(JsonObject object, JSONObject rootObj) { private static void analyzeObject(JsonObject object, JSONObject rootObj) {
if (object.has("allOf")) { if (object.has(BasicConstant.ALL_OF)) {
JsonArray allOfArray = object.get("allOf").getAsJsonArray(); JsonArray allOfArray = object.get(BasicConstant.ALL_OF).getAsJsonArray();
for (JsonElement allOfElement : allOfArray) { for (JsonElement allOfElement : allOfArray) {
JsonObject allOfElementObj = allOfElement.getAsJsonObject(); JsonObject allOfElementObj = allOfElement.getAsJsonObject();
if (allOfElementObj.has("properties")) { if (allOfElementObj.has(BasicConstant.PROPERTIES)) {
JsonObject propertiesObj = allOfElementObj.get("properties").getAsJsonObject(); JsonObject propertiesObj = allOfElementObj.get(BasicConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) { for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey(); String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
@ -51,42 +45,34 @@ public class JSONSchemaRunTest {
} }
} }
} }
} else if (object.has("properties")) { } else if (object.has(BasicConstant.PROPERTIES)) {
JsonObject propertiesObj = object.get("properties").getAsJsonObject(); JsonObject propertiesObj = object.get(BasicConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) { for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey(); String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(rootObj, propertyKey, propertyObj); analyzeProperty(rootObj, propertyKey, propertyObj);
} }
} else if (object.has("type") && object.get("type").getAsString().equals("array")) { } else if (object.has(BasicConstant.TYPE) && object.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.ARRAY)) {
analyzeProperty(rootObj, "MS-OBJECT", object); analyzeProperty(rootObj, BasicConstant.MS_OBJECT, object);
} else if (object.has("type") && !object.get("type").getAsString().equals("object")) { } else if (object.has(BasicConstant.TYPE) && !object.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.OBJECT)) {
analyzeProperty(rootObj, object.getAsString(), object); analyzeProperty(rootObj, object.getAsString(), object);
} }
} }
private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object) { private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object) {
if (object.has("type")) { if (object.has(BasicConstant.TYPE)) {
String propertyObjType = null; String propertyObjType = null;
if (object.get("type") instanceof JsonPrimitive) { if (object.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
propertyObjType = object.get("type").getAsString(); propertyObjType = object.get(BasicConstant.TYPE).getAsString();
} else if (object.get("type") instanceof JsonArray) {
JsonArray typeArray = (JsonArray) object.get("type").getAsJsonArray();
propertyObjType = typeArray.get(0).getAsString();
if (typeArray.size() > 1) {
if (typeArray.get(1).getAsString().equals("null")) {
// 暂不处理后续使用时再加
} }
} if (propertyObjType.equals(BasicConstant.STRING) || propertyObjType.equals(BasicConstant.ENUM)) {
}
if (propertyObjType.equals("string") || propertyObjType.equals("enum")) {
concept.put(propertyName, getValue(object)); concept.put(propertyName, getValue(object));
} else if (propertyObjType.equals("integer") || propertyObjType.equals("number")) { } else if (propertyObjType.equals(BasicConstant.INTEGER) || propertyObjType.equals(BasicConstant.NUMBER)) {
// 先设置空值
concept.put(propertyName, 0);
try { try {
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { concept.put(propertyName, 0);
Number value = object.get("mock").getAsJsonObject().get("mock").getAsNumber(); if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
Number value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsNumber();
if (value.toString().indexOf(".") == -1) { if (value.toString().indexOf(".") == -1) {
concept.put(propertyName, value.longValue()); concept.put(propertyName, value.longValue());
} else { } else {
@ -94,15 +80,17 @@ public class JSONSchemaRunTest {
} }
} }
} catch (Exception e) { } catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
concept.put(propertyName, value); concept.put(propertyName, value);
} }
} else if (propertyObjType.equals("boolean")) { } else if (propertyObjType.equals(BasicConstant.BOOLEAN)) {
// 先设置空值 // 先设置空值
concept.put(propertyName, false); concept.put(propertyName, false);
try { try {
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").toString()); && StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).toString());
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
if (value.indexOf("\"") != -1) { if (value.indexOf("\"") != -1) {
value = value.replaceAll("\"", ""); value = value.replaceAll("\"", "");
@ -113,10 +101,8 @@ public class JSONSchemaRunTest {
} catch (Exception e) { } catch (Exception e) {
concept.put(propertyName, false); concept.put(propertyName, false);
} }
} else if (propertyObjType.equals("array")) { } else if (propertyObjType.equals(BasicConstant.ARRAY)) {
// 先设置空值
List<Object> array = new LinkedList<>(); List<Object> array = new LinkedList<>();
JsonArray jsonArray = new JsonArray(); JsonArray jsonArray = new JsonArray();
if (object.has("items")) { if (object.has("items")) {
if (object.get("items").isJsonArray()) { if (object.get("items").isJsonArray()) {
@ -130,26 +116,28 @@ public class JSONSchemaRunTest {
for (int i = 0; i < jsonArray.size(); i++) { for (int i = 0; i < jsonArray.size(); i++) {
JsonObject itemsObject = jsonArray.get(i).getAsJsonObject(); JsonObject itemsObject = jsonArray.get(i).getAsJsonObject();
if (object.has("items")) { if (object.has("items")) {
if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) { if (itemsObject.has(BasicConstant.MOCK) && itemsObject.get(BasicConstant.MOCK).getAsJsonObject() != null
&& StringUtils.isNotEmpty(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
try { try {
int value = itemsObject.get("mock").getAsJsonObject().get("mock").getAsInt(); int value = itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsInt();
array.add(value); array.add(value);
} catch (Exception e) { } catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString()); String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
array.add(value); array.add(value);
} }
} else if (itemsObject.has("type") && (itemsObject.has("enum") || itemsObject.get("type").getAsString().equals("string"))) { } else if (itemsObject.has(BasicConstant.TYPE) && (itemsObject.has(BasicConstant.ENUM) || itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.STRING))) {
array.add(getValue(itemsObject)); array.add(getValue(itemsObject));
} else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("number")) { } else if (itemsObject.has(BasicConstant.TYPE) && itemsObject.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.NUMBER)) {
if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) { if (itemsObject.has(BasicConstant.MOCK) && itemsObject.get(BasicConstant.MOCK).getAsJsonObject() != null
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString()); && StringUtils.isNotEmpty(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
array.add(value); array.add(value);
} else { } else {
array.add(0); array.add(0);
} }
} else if (itemsObject.has("properties")) { } else if (itemsObject.has(BasicConstant.PROPERTIES)) {
JSONObject propertyConcept = new JSONObject(true); JSONObject propertyConcept = new JSONObject(true);
JsonObject propertiesObj = itemsObject.get("properties").getAsJsonObject(); JsonObject propertiesObj = itemsObject.get(BasicConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) { for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey(); String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
@ -157,7 +145,7 @@ public class JSONSchemaRunTest {
} }
array.add(propertyConcept); array.add(propertyConcept);
} else if (itemsObject.has("type") && itemsObject.get("type") instanceof JsonPrimitive) { } else if (itemsObject.has(BasicConstant.TYPE) && itemsObject.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
JSONObject newJsonObj = new JSONObject(); JSONObject newJsonObj = new JSONObject();
analyzeProperty(newJsonObj, propertyName + "_item", itemsObject); analyzeProperty(newJsonObj, propertyName + "_item", itemsObject);
array.add(newJsonObj.get(propertyName + "_item")); array.add(newJsonObj.get(propertyName + "_item"));
@ -168,7 +156,7 @@ public class JSONSchemaRunTest {
} }
} }
concept.put(propertyName, array); concept.put(propertyName, array);
} else if (propertyObjType.equals("object")) { } else if (propertyObjType.equals(BasicConstant.OBJECT)) {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
concept.put(propertyName, obj); concept.put(propertyName, obj);
analyzeObject(object, obj); analyzeObject(object, obj);
@ -180,18 +168,20 @@ public class JSONSchemaRunTest {
private static Object getValue(JsonObject object) { private static Object getValue(JsonObject object) {
try { try {
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString()) && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); && StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
return value; return value;
} }
} catch (Exception e) { } catch (Exception e) {
return object.get("mock").getAsJsonObject().get("mock"); return object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK);
} }
return ""; return "";
} }
private static void analyzeDefinitions(JsonObject object) { private static void analyzeDefinitions(JsonObject object) {
JsonObject definitionsObj = object.get("definitions").getAsJsonObject(); JsonObject definitionsObj = object.get("definitions").getAsJsonObject();
if (definitionsObj != null) {
for (Entry<String, JsonElement> entry : definitionsObj.entrySet()) { for (Entry<String, JsonElement> entry : definitionsObj.entrySet()) {
String definitionKey = entry.getKey(); String definitionKey = entry.getKey();
JsonObject definitionObj = definitionsObj.get(definitionKey).getAsJsonObject(); JsonObject definitionObj = definitionsObj.get(definitionKey).getAsJsonObject();
@ -199,21 +189,18 @@ public class JSONSchemaRunTest {
analyzeRootSchemaElement(definitionObj, obj); analyzeRootSchemaElement(definitionObj, obj);
} }
} }
}
private static String formerJson(String jsonSchema) { private static String formerJson(String jsonSchema) {
try {
JSONObject root = new JSONObject(true); JSONObject root = new JSONObject(true);
generator(jsonSchema, root); generator(jsonSchema, root);
// 格式化返回 // 格式化返回
Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create(); Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create();
if (root.get("MS-OBJECT") != null) { if (root.get(BasicConstant.MS_OBJECT) != null) {
return gson.toJson(root.get("MS-OBJECT")); return gson.toJson(root.get(BasicConstant.MS_OBJECT));
} }
return gson.toJson(root); return gson.toJson(root);
} catch (Exception e) {
return jsonSchema;
}
} }
public static String getJson(String jsonSchema) { public static String getJson(String jsonSchema) {

View File

@ -2,6 +2,7 @@ package io.metersphere.commons.json;
import com.google.gson.*; import com.google.gson.*;
import io.metersphere.api.dto.definition.request.assertions.document.DocumentElement; import io.metersphere.api.dto.definition.request.assertions.document.DocumentElement;
import io.metersphere.commons.utils.EnumPropertyUtil;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
@ -15,25 +16,25 @@ import java.util.Map.Entry;
public class JSONSchemaToDocumentUtils { public class JSONSchemaToDocumentUtils {
private static void analyzeRootSchemaElement(JsonObject rootElement, List<DocumentElement> roots) { private static void analyzeRootSchemaElement(JsonObject rootElement, List<DocumentElement> roots) {
if (rootElement.has("type") || rootElement.has("allOf")) { if (rootElement.has(BasicConstant.TYPE) || rootElement.has(BasicConstant.ALL_OF)) {
analyzeObject(rootElement, roots); analyzeObject(rootElement, roots);
} }
} }
private static void analyzeObject(JsonObject object, List<DocumentElement> roots) { private static void analyzeObject(JsonObject object, List<DocumentElement> roots) {
List<String> requiredList = new ArrayList<>(); List<String> requiredList = new ArrayList<>();
if (object.get("required") != null) { if (object.get(BasicConstant.REQUIRED) != null) {
JsonArray jsonElements = object.get("required").getAsJsonArray(); JsonArray jsonElements = object.get(BasicConstant.REQUIRED).getAsJsonArray();
for (JsonElement jsonElement : jsonElements) { for (JsonElement jsonElement : jsonElements) {
requiredList.add(jsonElement.getAsString()); requiredList.add(jsonElement.getAsString());
} }
} }
if (object.has("allOf")) { if (object.has(BasicConstant.ALL_OF)) {
JsonArray allOfArray = object.get("allOf").getAsJsonArray(); JsonArray allOfArray = object.get(BasicConstant.ALL_OF).getAsJsonArray();
for (JsonElement allOfElement : allOfArray) { for (JsonElement allOfElement : allOfArray) {
JsonObject allOfElementObj = allOfElement.getAsJsonObject(); JsonObject allOfElementObj = allOfElement.getAsJsonObject();
if (allOfElementObj.has("properties")) { if (allOfElementObj.has(BasicConstant.PROPERTIES)) {
JsonObject propertiesObj = allOfElementObj.get("properties").getAsJsonObject(); JsonObject propertiesObj = allOfElementObj.get(BasicConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) { for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey(); String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
@ -41,48 +42,51 @@ public class JSONSchemaToDocumentUtils {
} }
} }
} }
} else if (object.has("properties")) { } else if (object.has(BasicConstant.PROPERTIES)) {
JsonObject propertiesObj = object.get("properties").getAsJsonObject(); JsonObject propertiesObj = object.get(BasicConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) { for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey(); String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
if (propertyObj.get("required") != null) { if (propertyObj.get(BasicConstant.REQUIRED) != null) {
JsonArray jsonElements = propertyObj.get("required").getAsJsonArray(); JsonArray jsonElements = propertyObj.get(BasicConstant.REQUIRED).getAsJsonArray();
for (JsonElement jsonElement : jsonElements) { for (JsonElement jsonElement : jsonElements) {
requiredList.add(jsonElement.getAsString()); requiredList.add(jsonElement.getAsString());
} }
} }
analyzeProperty(roots, propertyKey, propertyObj, requiredList); analyzeProperty(roots, propertyKey, propertyObj, requiredList);
} }
} else if (object.has("type") && object.get("type").getAsString().equals("array")) { } else if (object.has(BasicConstant.TYPE)
analyzeProperty(roots, "MS-OBJECT", object, requiredList); && object.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.ARRAY)) {
} else if (object.has("type") && !object.get("type").getAsString().equals("object")) { analyzeProperty(roots, BasicConstant.MS_OBJECT, object, requiredList);
} else if (object.has(BasicConstant.TYPE)
&& !object.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.OBJECT)) {
analyzeProperty(roots, object.getAsString(), object, requiredList); analyzeProperty(roots, object.getAsString(), object, requiredList);
} }
} }
private static void analyzeProperty(List<DocumentElement> concept, private static void analyzeProperty(List<DocumentElement> concept,
String propertyName, JsonObject object, List<String> requiredList) { String propertyName, JsonObject object, List<String> requiredList) {
if (object.has("type")) { if (object.has(BasicConstant.TYPE)) {
String propertyObjType = null; String propertyObjType = null;
if (object.get("type") instanceof JsonPrimitive) { if (object.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
propertyObjType = object.get("type").getAsString(); propertyObjType = object.get(BasicConstant.TYPE).getAsString();
} else if (object.get("type") instanceof JsonArray) { } else if (object.get(BasicConstant.TYPE) instanceof JsonArray) {
JsonArray typeArray = (JsonArray) object.get("type").getAsJsonArray(); JsonArray typeArray = (JsonArray) object.get(BasicConstant.TYPE).getAsJsonArray();
propertyObjType = typeArray.get(0).getAsString(); propertyObjType = typeArray.get(0).getAsString();
} }
Object value = null; Object value = null;
boolean required = requiredList.contains(propertyName); boolean required = requiredList.contains(propertyName);
if (object.has("default")) { if (object.has(BasicConstant.DEFAULT)) {
value = object.get("default") != null ? object.get("default").getAsString() : ""; value = object.get(BasicConstant.DEFAULT) != null ? object.get(BasicConstant.DEFAULT).getAsString() : "";
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null)); concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (object.has("enum")) { } else if (object.has(BasicConstant.ENUM)) {
try { try {
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
value = object.get("mock").getAsJsonObject().get("mock"); && StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK);
} else { } else {
List<Object> list = analyzeEnumProperty(object); List<Object> list = EnumPropertyUtil.analyzeEnumProperty(object);
if (list.size() > 0) { if (CollectionUtils.isNotEmpty(list)) {
int index = (int) (Math.random() * list.size()); int index = (int) (Math.random() * list.size());
value = list.get(index).toString(); value = list.get(index).toString();
} }
@ -94,56 +98,60 @@ public class JSONSchemaToDocumentUtils {
LogUtil.error(e); LogUtil.error(e);
} }
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null)); concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (propertyObjType.equals("string")) { } else if (propertyObjType.equals(BasicConstant.STRING)) {
// 先设置空值 // 先设置空值
if (object.has("default")) { if (object.has(BasicConstant.DEFAULT)) {
value = object.get("default") != null ? object.get("default").getAsString() : ""; value = object.get(BasicConstant.DEFAULT) != null ? object.get(BasicConstant.DEFAULT).getAsString() : "";
} }
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString()) && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); && StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
} }
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null)); concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (propertyObjType.equals("integer")) { } else if (propertyObjType.equals(BasicConstant.INTEGER)) {
if (object.has("default")) { if (object.has(BasicConstant.DEFAULT)) {
value = object.get("default"); value = object.get(BasicConstant.DEFAULT);
} }
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
try { try {
value = object.get("mock").getAsJsonObject().get("mock").getAsInt(); value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsInt();
} catch (Exception e) { } catch (Exception e) {
value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
} }
} }
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null)); concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (propertyObjType.equals("number")) { } else if (propertyObjType.equals(BasicConstant.NUMBER)) {
if (object.has("default")) { if (object.has(BasicConstant.DEFAULT)) {
value = object.get("default"); value = object.get(BasicConstant.DEFAULT);
} }
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
try { try {
value = object.get("mock").getAsJsonObject().get("mock").getAsNumber(); value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsNumber();
} catch (Exception e) { } catch (Exception e) {
value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString()); value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
} }
} }
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null)); concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (propertyObjType.equals("boolean")) { } else if (propertyObjType.equals(BasicConstant.BOOLEAN)) {
if (object.has("default")) { if (object.has(BasicConstant.DEFAULT)) {
value = object.get("default"); value = object.get(BasicConstant.DEFAULT);
} }
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) { if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
try { try {
value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").toString()); value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).toString());
} catch (Exception e) { } catch (Exception e) {
} }
} }
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null)); concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (propertyObjType.equals("array")) { } else if (propertyObjType.equals(BasicConstant.ARRAY)) {
List<DocumentElement> elements = new LinkedList<>(); List<DocumentElement> elements = new LinkedList<>();
concept.add(new DocumentElement(propertyName, propertyObjType, "", requiredList.contains(propertyName), true, elements)); concept.add(new DocumentElement(propertyName, propertyObjType, "", requiredList.contains(propertyName), true, elements));
JsonArray jsonArray = object.get("items").getAsJsonArray(); JsonArray jsonArray = object.get("items").getAsJsonArray();
analyzeArray(propertyName, jsonArray, elements, requiredList); analyzeArray(propertyName, jsonArray, elements, requiredList);
} else if (propertyObjType.equals("object")) { } else if (propertyObjType.equals(BasicConstant.OBJECT)) {
List<DocumentElement> list = new LinkedList<>(); List<DocumentElement> list = new LinkedList<>();
concept.add(new DocumentElement(propertyName, propertyObjType, "", list)); concept.add(new DocumentElement(propertyName, propertyObjType, "", list));
analyzeObject(object, list); analyzeObject(object, list);
@ -161,8 +169,8 @@ public class JSONSchemaToDocumentUtils {
analyzeArray("", itemsObject, elements, requiredList); analyzeArray("", itemsObject, elements, requiredList);
} else if (obj.isJsonObject()) { } else if (obj.isJsonObject()) {
List<String> requiredItems = new ArrayList<>(); List<String> requiredItems = new ArrayList<>();
if (obj.getAsJsonObject().get("required") != null) { if (obj.getAsJsonObject().get(BasicConstant.REQUIRED) != null) {
JsonArray jsonElements = obj.getAsJsonObject().get("required").getAsJsonArray(); JsonArray jsonElements = obj.getAsJsonObject().get(BasicConstant.REQUIRED).getAsJsonArray();
for (JsonElement jsonElement : jsonElements) { for (JsonElement jsonElement : jsonElements) {
requiredItems.add(jsonElement.getAsString()); requiredItems.add(jsonElement.getAsString());
} }
@ -175,41 +183,23 @@ public class JSONSchemaToDocumentUtils {
} }
} }
private static List<Object> analyzeEnumProperty(JsonObject object) {
List<Object> list = new LinkedList<>();
String jsonStr = null;
try {
JsonArray enumValues = object.get("enum").getAsJsonArray();
for (JsonElement enumValueElem : enumValues) {
String enumValue = enumValueElem.getAsString();
list.add(enumValue);
}
} catch (Exception e) {
jsonStr = object.get("enum").getAsString();
}
if (jsonStr != null && list.isEmpty()) {
String[] arrays = jsonStr.split("\n");
for (String str : arrays) {
list.add(str);
}
}
return list;
}
public static List<DocumentElement> getDocument(String jsonSchema) { public static List<DocumentElement> getDocument(String jsonSchema) {
Gson gson = new Gson(); Gson gson = new Gson();
JsonElement element = gson.fromJson(jsonSchema, JsonElement.class); JsonElement element = gson.fromJson(jsonSchema, JsonElement.class);
JsonObject rootElement = element.getAsJsonObject(); JsonObject rootElement = element.getAsJsonObject();
List<DocumentElement> roots = new LinkedList<>(); List<DocumentElement> roots = new LinkedList<>();
analyzeRootSchemaElement(rootElement, roots); analyzeRootSchemaElement(rootElement, roots);
if (rootElement.get("type") != null) { if (rootElement.get(BasicConstant.TYPE) != null) {
if (rootElement.get("type").toString().equals("object") || rootElement.get("type").toString().equals("\"object\"")) { if (rootElement.get(BasicConstant.TYPE).toString().equals(BasicConstant.OBJECT)
|| rootElement.get(BasicConstant.TYPE).toString().equals("\"object\"")) {
return new LinkedList<DocumentElement>() {{ return new LinkedList<DocumentElement>() {{
this.add(new DocumentElement().newRoot("root", roots)); this.add(new DocumentElement().newRoot("root", roots));
}}; }};
} else { } else {
return new LinkedList<DocumentElement>() {{ return new LinkedList<DocumentElement>() {{
this.add(new DocumentElement().newRoot("array", roots)); this.add(new DocumentElement().newRoot(BasicConstant.ARRAY, roots));
}}; }};
} }
} }

View File

@ -26,21 +26,21 @@ public class JSONToDocumentUtils {
Object value = array.get(i); Object value = array.get(i);
if (value instanceof JSONObject) { if (value instanceof JSONObject) {
List<DocumentElement> childrenElements = new LinkedList<>(); List<DocumentElement> childrenElements = new LinkedList<>();
children.add(new DocumentElement("", "object", "", childrenElements)); children.add(new DocumentElement("", BasicConstant.OBJECT, "", childrenElements));
jsonDataFormatting((JSONObject) value, childrenElements); jsonDataFormatting((JSONObject) value, childrenElements);
} else if (value instanceof JSONArray) { } else if (value instanceof JSONArray) {
List<DocumentElement> childrenElements = new LinkedList<>(); List<DocumentElement> childrenElements = new LinkedList<>();
DocumentElement documentElement = new DocumentElement("", "array", "", childrenElements); DocumentElement documentElement = new DocumentElement("", BasicConstant.ARRAY, "", childrenElements);
documentElement.setArrayVerification(true); documentElement.setArrayVerification(true);
children.add(documentElement); children.add(documentElement);
jsonDataFormatting((JSONArray) value, childrenElements); jsonDataFormatting((JSONArray) value, childrenElements);
} else { } else {
String type = "string"; String type = BasicConstant.STRING;
if (value != null) { if (value != null) {
if (isWholeNumber(value.toString())) { if (isWholeNumber(value.toString())) {
type = "integer"; type = BasicConstant.INTEGER;
} else if (isNumber(value.toString())) { } else if (isNumber(value.toString())) {
type = "number"; type = BasicConstant.NUMBER;
} }
} }
children.add(new DocumentElement("", type, value, null)); children.add(new DocumentElement("", type, value, null));
@ -53,23 +53,23 @@ public class JSONToDocumentUtils {
Object value = object.get(key); Object value = object.get(key);
if (value instanceof JSONObject) { if (value instanceof JSONObject) {
List<DocumentElement> childrenElements = new LinkedList<>(); List<DocumentElement> childrenElements = new LinkedList<>();
children.add(new DocumentElement(key, "object", "", childrenElements)); children.add(new DocumentElement(key, BasicConstant.OBJECT, "", childrenElements));
jsonDataFormatting((JSONObject) value, childrenElements); jsonDataFormatting((JSONObject) value, childrenElements);
} else if (value instanceof JSONArray) { } else if (value instanceof JSONArray) {
List<DocumentElement> childrenElements = new LinkedList<>(); List<DocumentElement> childrenElements = new LinkedList<>();
DocumentElement documentElement = new DocumentElement(key, "array", "", childrenElements); DocumentElement documentElement = new DocumentElement(key, BasicConstant.ARRAY, "", childrenElements);
documentElement.setArrayVerification(true); documentElement.setArrayVerification(true);
children.add(documentElement); children.add(documentElement);
jsonDataFormatting((JSONArray) value, childrenElements); jsonDataFormatting((JSONArray) value, childrenElements);
} else { } else {
String type = "string"; String type = BasicConstant.STRING;
if (value != null) { if (value != null) {
if (isWholeNumber(value.toString())) { if (isWholeNumber(value.toString())) {
type = "integer"; type = BasicConstant.INTEGER;
} else if (isNumber(value.toString())) { } else if (isNumber(value.toString())) {
type = "number"; type = BasicConstant.NUMBER;
} else if (StringUtils.equalsIgnoreCase(DocumentUtils.getType(value), "boolean")) { } else if (StringUtils.equalsIgnoreCase(DocumentUtils.getType(value), BasicConstant.BOOLEAN)) {
type = "boolean"; type = BasicConstant.BOOLEAN;
} }
} }
children.add(new DocumentElement(key, type, value, null)); children.add(new DocumentElement(key, type, value, null));
@ -83,7 +83,7 @@ public class JSONToDocumentUtils {
Object typeObject = new JSONTokener(json).nextValue(); Object typeObject = new JSONTokener(json).nextValue();
if (typeObject instanceof net.sf.json.JSONArray) { if (typeObject instanceof net.sf.json.JSONArray) {
if (StringUtils.equals(type, "JSON")) { if (StringUtils.equals(type, "JSON")) {
roots.add(new DocumentElement().newRoot("array", children)); roots.add(new DocumentElement().newRoot(BasicConstant.ARRAY, children));
JSONArray array = JSON.parseArray(json); JSONArray array = JSON.parseArray(json);
jsonDataFormatting(array, children); jsonDataFormatting(array, children);
} else { } else {
@ -92,7 +92,7 @@ public class JSONToDocumentUtils {
} }
} else { } else {
if (StringUtils.equals(type, "JSON")) { if (StringUtils.equals(type, "JSON")) {
roots.add(new DocumentElement().newRoot("object", children)); roots.add(new DocumentElement().newRoot(BasicConstant.OBJECT, children));
JSONObject object = JSON.parseObject(json); JSONObject object = JSON.parseObject(json);
jsonDataFormatting(object, children); jsonDataFormatting(object, children);
} else { } else {
@ -113,7 +113,7 @@ public class JSONToDocumentUtils {
return getJsonDocument(jsonPrettyPrintString, type); return getJsonDocument(jsonPrettyPrintString, type);
} else { } else {
return new LinkedList<DocumentElement>() {{ return new LinkedList<DocumentElement>() {{
this.add(new DocumentElement().newRoot("object", null)); this.add(new DocumentElement().newRoot(BasicConstant.OBJECT, null));
}}; }};
} }
} catch (Exception e) { } catch (Exception e) {
@ -140,12 +140,12 @@ public class JSONToDocumentUtils {
//递归遍历当前节点所有的子节点 //递归遍历当前节点所有的子节点
List<Element> listElement = node.elements(); List<Element> listElement = node.elements();
if (listElement.isEmpty()) { if (listElement.isEmpty()) {
String type = "string"; String type = BasicConstant.STRING;
if (StringUtils.isNotEmpty(node.getTextTrim())) { if (StringUtils.isNotEmpty(node.getTextTrim())) {
if (isWholeNumber(node.getText())) { if (isWholeNumber(node.getText())) {
type = "integer"; type = BasicConstant.INTEGER;
} else if (isNumber(node.getText())) { } else if (isNumber(node.getText())) {
type = "number"; type = BasicConstant.NUMBER;
} }
} }
children.add(new DocumentElement(node.getName(), type, node.getTextTrim(), null)); children.add(new DocumentElement(node.getName(), type, node.getTextTrim(), null));
@ -154,7 +154,7 @@ public class JSONToDocumentUtils {
List<Element> elementNodes = element.elements(); List<Element> elementNodes = element.elements();
if (elementNodes.size() > 0) { if (elementNodes.size() > 0) {
List<DocumentElement> elements = new LinkedList<>(); List<DocumentElement> elements = new LinkedList<>();
children.add(new DocumentElement(element.getName(), "object", element.getTextTrim(), elements)); children.add(new DocumentElement(element.getName(), BasicConstant.OBJECT, element.getTextTrim(), elements));
getNodes(element, elements);//递归 getNodes(element, elements);//递归
} else { } else {
getNodes(element, children);//递归 getNodes(element, children);//递归
@ -176,7 +176,7 @@ public class JSONToDocumentUtils {
if (roots.size() > 1) { if (roots.size() > 1) {
Element node = document.getRootElement(); Element node = document.getRootElement();
List<DocumentElement> newRoots = new LinkedList<>(); List<DocumentElement> newRoots = new LinkedList<>();
newRoots.add(new DocumentElement("root", node.getName(), "object", node.getTextTrim(), roots)); newRoots.add(new DocumentElement("root", node.getName(), BasicConstant.OBJECT, node.getTextTrim(), roots));
return newRoots; return newRoots;
} else if (roots.size() == 1) { } else if (roots.size() == 1) {
roots.get(0).setId("root"); roots.get(0).setId("root");

View File

@ -0,0 +1,33 @@
package io.metersphere.commons.utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.metersphere.commons.json.BasicConstant;
import java.util.LinkedList;
import java.util.List;
public class EnumPropertyUtil {
public static List<Object> analyzeEnumProperty(JsonObject object) {
List<Object> list = new LinkedList<>();
String jsonStr = null;
try {
JsonArray enumValues = object.get(BasicConstant.ENUM).getAsJsonArray();
for (JsonElement enumValueElem : enumValues) {
String enumValue = enumValueElem.getAsString();
list.add(enumValue);
}
} catch (Exception e) {
jsonStr = object.get(BasicConstant.ENUM).getAsString();
}
if (jsonStr != null && list.isEmpty()) {
String[] arrays = jsonStr.split("\n");
for (String str : arrays) {
list.add(str);
}
}
return list;
}
}