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

View File

@ -2,6 +2,7 @@ package io.metersphere.commons.json;
import com.google.gson.*;
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.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.collections4.CollectionUtils;
@ -15,25 +16,25 @@ import java.util.Map.Entry;
public class JSONSchemaToDocumentUtils {
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);
}
}
private static void analyzeObject(JsonObject object, List<DocumentElement> roots) {
List<String> requiredList = new ArrayList<>();
if (object.get("required") != null) {
JsonArray jsonElements = object.get("required").getAsJsonArray();
if (object.get(BasicConstant.REQUIRED) != null) {
JsonArray jsonElements = object.get(BasicConstant.REQUIRED).getAsJsonArray();
for (JsonElement jsonElement : jsonElements) {
requiredList.add(jsonElement.getAsString());
}
}
if (object.has("allOf")) {
JsonArray allOfArray = object.get("allOf").getAsJsonArray();
if (object.has(BasicConstant.ALL_OF)) {
JsonArray allOfArray = object.get(BasicConstant.ALL_OF).getAsJsonArray();
for (JsonElement allOfElement : allOfArray) {
JsonObject allOfElementObj = allOfElement.getAsJsonObject();
if (allOfElementObj.has("properties")) {
JsonObject propertiesObj = allOfElementObj.get("properties").getAsJsonObject();
if (allOfElementObj.has(BasicConstant.PROPERTIES)) {
JsonObject propertiesObj = allOfElementObj.get(BasicConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
@ -41,48 +42,51 @@ public class JSONSchemaToDocumentUtils {
}
}
}
} else if (object.has("properties")) {
JsonObject propertiesObj = object.get("properties").getAsJsonObject();
} else if (object.has(BasicConstant.PROPERTIES)) {
JsonObject propertiesObj = object.get(BasicConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
if (propertyObj.get("required") != null) {
JsonArray jsonElements = propertyObj.get("required").getAsJsonArray();
if (propertyObj.get(BasicConstant.REQUIRED) != null) {
JsonArray jsonElements = propertyObj.get(BasicConstant.REQUIRED).getAsJsonArray();
for (JsonElement jsonElement : jsonElements) {
requiredList.add(jsonElement.getAsString());
}
}
analyzeProperty(roots, propertyKey, propertyObj, requiredList);
}
} else if (object.has("type") && object.get("type").getAsString().equals("array")) {
analyzeProperty(roots, "MS-OBJECT", object, requiredList);
} else if (object.has("type") && !object.get("type").getAsString().equals("object")) {
} else if (object.has(BasicConstant.TYPE)
&& object.get(BasicConstant.TYPE).getAsString().equals(BasicConstant.ARRAY)) {
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);
}
}
private static void analyzeProperty(List<DocumentElement> concept,
String propertyName, JsonObject object, List<String> requiredList) {
if (object.has("type")) {
if (object.has(BasicConstant.TYPE)) {
String propertyObjType = null;
if (object.get("type") instanceof JsonPrimitive) {
propertyObjType = object.get("type").getAsString();
} else if (object.get("type") instanceof JsonArray) {
JsonArray typeArray = (JsonArray) object.get("type").getAsJsonArray();
if (object.get(BasicConstant.TYPE) instanceof JsonPrimitive) {
propertyObjType = object.get(BasicConstant.TYPE).getAsString();
} else if (object.get(BasicConstant.TYPE) instanceof JsonArray) {
JsonArray typeArray = (JsonArray) object.get(BasicConstant.TYPE).getAsJsonArray();
propertyObjType = typeArray.get(0).getAsString();
}
Object value = null;
boolean required = requiredList.contains(propertyName);
if (object.has("default")) {
value = object.get("default") != null ? object.get("default").getAsString() : "";
if (object.has(BasicConstant.DEFAULT)) {
value = object.get(BasicConstant.DEFAULT) != null ? object.get(BasicConstant.DEFAULT).getAsString() : "";
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (object.has("enum")) {
} else if (object.has(BasicConstant.ENUM)) {
try {
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) {
value = object.get("mock").getAsJsonObject().get("mock");
if (object.has(BasicConstant.MOCK) && object.get(BasicConstant.MOCK).getAsJsonObject() != null
&& StringUtils.isNotEmpty(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString())) {
value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK);
} else {
List<Object> list = analyzeEnumProperty(object);
if (list.size() > 0) {
List<Object> list = EnumPropertyUtil.analyzeEnumProperty(object);
if (CollectionUtils.isNotEmpty(list)) {
int index = (int) (Math.random() * list.size());
value = list.get(index).toString();
}
@ -94,56 +98,60 @@ public class JSONSchemaToDocumentUtils {
LogUtil.error(e);
}
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (propertyObjType.equals("string")) {
} else if (propertyObjType.equals(BasicConstant.STRING)) {
// 先设置空值
if (object.has("default")) {
value = object.get("default") != null ? object.get("default").getAsString() : "";
if (object.has(BasicConstant.DEFAULT)) {
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())) {
value = ScriptEngineUtils.buildFunctionCallString(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())) {
value = ScriptEngineUtils.buildFunctionCallString(object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsString());
}
concept.add(new DocumentElement(propertyName, propertyObjType, value, required, null));
} else if (propertyObjType.equals("integer")) {
if (object.has("default")) {
value = object.get("default");
} else if (propertyObjType.equals(BasicConstant.INTEGER)) {
if (object.has(BasicConstant.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 {
value = object.get("mock").getAsJsonObject().get("mock").getAsInt();
value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsInt();
} 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));
} else if (propertyObjType.equals("number")) {
if (object.has("default")) {
value = object.get("default");
} else if (propertyObjType.equals(BasicConstant.NUMBER)) {
if (object.has(BasicConstant.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 {
value = object.get("mock").getAsJsonObject().get("mock").getAsNumber();
value = object.get(BasicConstant.MOCK).getAsJsonObject().get(BasicConstant.MOCK).getAsNumber();
} 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));
} else if (propertyObjType.equals("boolean")) {
if (object.has("default")) {
value = object.get("default");
} else if (propertyObjType.equals(BasicConstant.BOOLEAN)) {
if (object.has(BasicConstant.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 {
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) {
}
}
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<>();
concept.add(new DocumentElement(propertyName, propertyObjType, "", requiredList.contains(propertyName), true, elements));
JsonArray jsonArray = object.get("items").getAsJsonArray();
analyzeArray(propertyName, jsonArray, elements, requiredList);
} else if (propertyObjType.equals("object")) {
} else if (propertyObjType.equals(BasicConstant.OBJECT)) {
List<DocumentElement> list = new LinkedList<>();
concept.add(new DocumentElement(propertyName, propertyObjType, "", list));
analyzeObject(object, list);
@ -161,8 +169,8 @@ public class JSONSchemaToDocumentUtils {
analyzeArray("", itemsObject, elements, requiredList);
} else if (obj.isJsonObject()) {
List<String> requiredItems = new ArrayList<>();
if (obj.getAsJsonObject().get("required") != null) {
JsonArray jsonElements = obj.getAsJsonObject().get("required").getAsJsonArray();
if (obj.getAsJsonObject().get(BasicConstant.REQUIRED) != null) {
JsonArray jsonElements = obj.getAsJsonObject().get(BasicConstant.REQUIRED).getAsJsonArray();
for (JsonElement jsonElement : jsonElements) {
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) {
Gson gson = new Gson();
JsonElement element = gson.fromJson(jsonSchema, JsonElement.class);
JsonObject rootElement = element.getAsJsonObject();
List<DocumentElement> roots = new LinkedList<>();
analyzeRootSchemaElement(rootElement, roots);
if (rootElement.get("type") != null) {
if (rootElement.get("type").toString().equals("object") || rootElement.get("type").toString().equals("\"object\"")) {
if (rootElement.get(BasicConstant.TYPE) != null) {
if (rootElement.get(BasicConstant.TYPE).toString().equals(BasicConstant.OBJECT)
|| rootElement.get(BasicConstant.TYPE).toString().equals("\"object\"")) {
return new LinkedList<DocumentElement>() {{
this.add(new DocumentElement().newRoot("root", roots));
}};
} else {
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);
if (value instanceof JSONObject) {
List<DocumentElement> childrenElements = new LinkedList<>();
children.add(new DocumentElement("", "object", "", childrenElements));
children.add(new DocumentElement("", BasicConstant.OBJECT, "", childrenElements));
jsonDataFormatting((JSONObject) value, childrenElements);
} else if (value instanceof JSONArray) {
List<DocumentElement> childrenElements = new LinkedList<>();
DocumentElement documentElement = new DocumentElement("", "array", "", childrenElements);
DocumentElement documentElement = new DocumentElement("", BasicConstant.ARRAY, "", childrenElements);
documentElement.setArrayVerification(true);
children.add(documentElement);
jsonDataFormatting((JSONArray) value, childrenElements);
} else {
String type = "string";
String type = BasicConstant.STRING;
if (value != null) {
if (isWholeNumber(value.toString())) {
type = "integer";
type = BasicConstant.INTEGER;
} else if (isNumber(value.toString())) {
type = "number";
type = BasicConstant.NUMBER;
}
}
children.add(new DocumentElement("", type, value, null));
@ -53,23 +53,23 @@ public class JSONToDocumentUtils {
Object value = object.get(key);
if (value instanceof JSONObject) {
List<DocumentElement> childrenElements = new LinkedList<>();
children.add(new DocumentElement(key, "object", "", childrenElements));
children.add(new DocumentElement(key, BasicConstant.OBJECT, "", childrenElements));
jsonDataFormatting((JSONObject) value, childrenElements);
} else if (value instanceof JSONArray) {
List<DocumentElement> childrenElements = new LinkedList<>();
DocumentElement documentElement = new DocumentElement(key, "array", "", childrenElements);
DocumentElement documentElement = new DocumentElement(key, BasicConstant.ARRAY, "", childrenElements);
documentElement.setArrayVerification(true);
children.add(documentElement);
jsonDataFormatting((JSONArray) value, childrenElements);
} else {
String type = "string";
String type = BasicConstant.STRING;
if (value != null) {
if (isWholeNumber(value.toString())) {
type = "integer";
type = BasicConstant.INTEGER;
} else if (isNumber(value.toString())) {
type = "number";
} else if (StringUtils.equalsIgnoreCase(DocumentUtils.getType(value), "boolean")) {
type = "boolean";
type = BasicConstant.NUMBER;
} else if (StringUtils.equalsIgnoreCase(DocumentUtils.getType(value), BasicConstant.BOOLEAN)) {
type = BasicConstant.BOOLEAN;
}
}
children.add(new DocumentElement(key, type, value, null));
@ -83,7 +83,7 @@ public class JSONToDocumentUtils {
Object typeObject = new JSONTokener(json).nextValue();
if (typeObject instanceof net.sf.json.JSONArray) {
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);
jsonDataFormatting(array, children);
} else {
@ -92,7 +92,7 @@ public class JSONToDocumentUtils {
}
} else {
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);
jsonDataFormatting(object, children);
} else {
@ -113,7 +113,7 @@ public class JSONToDocumentUtils {
return getJsonDocument(jsonPrettyPrintString, type);
} else {
return new LinkedList<DocumentElement>() {{
this.add(new DocumentElement().newRoot("object", null));
this.add(new DocumentElement().newRoot(BasicConstant.OBJECT, null));
}};
}
} catch (Exception e) {
@ -140,12 +140,12 @@ public class JSONToDocumentUtils {
//递归遍历当前节点所有的子节点
List<Element> listElement = node.elements();
if (listElement.isEmpty()) {
String type = "string";
String type = BasicConstant.STRING;
if (StringUtils.isNotEmpty(node.getTextTrim())) {
if (isWholeNumber(node.getText())) {
type = "integer";
type = BasicConstant.INTEGER;
} else if (isNumber(node.getText())) {
type = "number";
type = BasicConstant.NUMBER;
}
}
children.add(new DocumentElement(node.getName(), type, node.getTextTrim(), null));
@ -154,7 +154,7 @@ public class JSONToDocumentUtils {
List<Element> elementNodes = element.elements();
if (elementNodes.size() > 0) {
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);//递归
} else {
getNodes(element, children);//递归
@ -176,7 +176,7 @@ public class JSONToDocumentUtils {
if (roots.size() > 1) {
Element node = document.getRootElement();
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;
} else if (roots.size() == 1) {
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;
}
}