refactor(接口测试): 重构json schema格式转换和数据生成相关逻辑

This commit is contained in:
fit2-zhao 2023-01-31 10:16:17 +08:00 committed by fit2-zhao
parent 5d20b59db4
commit 29f66ceddb
15 changed files with 639 additions and 710 deletions

View File

@ -1,6 +1,6 @@
package io.metersphere.api.dto.scenario; package io.metersphere.api.dto.scenario;
import io.metersphere.api.exec.generator.JSONSchemaRunTest; import io.metersphere.api.exec.generator.JSONSchemaBuilder;
import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.StorageConstants; import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.FileUtils;
@ -99,7 +99,7 @@ public class Body {
if (StringUtils.isNotBlank(this.type) && StringUtils.equals(this.type, JSON_STR)) { if (StringUtils.isNotBlank(this.type) && StringUtils.equals(this.type, JSON_STR)) {
if (StringUtils.isNotEmpty(this.format) && this.getJsonSchema() != null if (StringUtils.isNotEmpty(this.format) && this.getJsonSchema() != null
&& JSON_SCHEMA.equals(this.format)) { && JSON_SCHEMA.equals(this.format)) {
this.raw = StringEscapeUtils.unescapeJava(JSONSchemaRunTest.getJson(JSONUtil.toJSONString(this.getJsonSchema()))); this.raw = StringEscapeUtils.unescapeJava(JSONSchemaBuilder.generator(JSONUtil.toJSONString(this.getJsonSchema())));
} else { } else {
try { try {
if (StringUtils.isNotEmpty(this.getRaw())) { if (StringUtils.isNotEmpty(this.getRaw())) {

View File

@ -0,0 +1,45 @@
package io.metersphere.api.exec.generator;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FormatterUtil {
public static boolean isNumber(Object obj) {
if (ObjectUtils.isEmpty(obj)) {
return false;
}
Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
Matcher isNum = pattern.matcher(obj.toString());
if (!isNum.matches()) {
return false;
}
return true;
}
public static JsonElement getElementValue(JsonObject object) {
return object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK);
}
public static String getMockValue(JsonObject object) {
return ScriptEngineUtils.buildFunctionCallString(getStrValue(object));
}
public static String getStrValue(JsonObject object) {
return getElementValue(object).getAsString();
}
public static boolean isMockValue(JsonObject object) {
return object.has(PropertyConstant.MOCK)
&& object.get(PropertyConstant.MOCK).getAsJsonObject() != null
&& FormatterUtil.getElementValue(object) != null
&& StringUtils.isNotEmpty(FormatterUtil.getElementValue(object).getAsString());
}
}

View File

@ -0,0 +1,287 @@
package io.metersphere.api.exec.generator;
import com.google.gson.*;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.util.NumberUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class JSONSchemaBuilder {
private static void analyzeSchema(String json, JSONObject rootObj, Map<String, String> map) {
Gson gson = new Gson();
JsonElement element = gson.fromJson(json, JsonElement.class);
JsonObject rootElement = element.getAsJsonObject();
analyzeRootSchemaElement(rootElement, rootObj, map);
}
private static void analyzeRootSchemaElement(JsonObject rootElement, JSONObject rootObj, Map<String, String> map) {
if (rootElement.has(PropertyConstant.TYPE) || rootElement.has(PropertyConstant.ALL_OF)) {
analyzeObject(rootElement, rootObj, map);
}
if (rootElement.has("definitions")) {
analyzeDefinitions(rootElement, map);
}
}
private static void analyzeObject(JsonObject object, JSONObject rootObj, Map<String, String> map) {
if (object.has(PropertyConstant.ALL_OF)) {
JsonArray allOfArray = object.get(PropertyConstant.ALL_OF).getAsJsonArray();
for (JsonElement allOfElement : allOfArray) {
JsonObject allOfElementObj = allOfElement.getAsJsonObject();
if (allOfElementObj.has(PropertyConstant.PROPERTIES)) {
formatObject(map, allOfElementObj, rootObj);
}
}
} else if (object.has(PropertyConstant.PROPERTIES)) {
formatObject(map, object, rootObj);
} else if (object.has(PropertyConstant.TYPE)) {
if (object.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.ARRAY)) {
analyzeProperty(rootObj, PropertyConstant.MS_OBJECT, object, map);
} else if (!object.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.OBJECT)) {
analyzeProperty(rootObj, object.getAsString(), object, map);
}
}
}
public static boolean isBoolean(String value) {
return "true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value);
}
private static void valueOf(String evlValue, String propertyName, JSONObject concept) {
if (StringUtils.startsWith(evlValue, "@")) {
String str = ScriptEngineUtils.calculate(evlValue);
switch (evlValue) {
case "@integer":
concept.put(propertyName, NumberUtils.parseNumber(str, Long.class));
break;
case "@boolean":
concept.put(propertyName, Boolean.parseBoolean(str));
break;
case "@float":
concept.put(propertyName, Float.parseFloat(str));
break;
default:
concept.put(propertyName, str);
break;
}
} else {
concept.put(propertyName, ScriptEngineUtils.buildFunctionCallString(evlValue));
}
}
private static void arrayValueOf(String evlValue, JSONArray array) {
if (StringUtils.startsWith(evlValue, "@")) {
String str = ScriptEngineUtils.calculate(evlValue);
switch (evlValue) {
case "@integer":
array.put(NumberUtils.parseNumber(str, Long.class));
break;
case "@boolean":
array.put(Boolean.parseBoolean(str));
break;
case "@float":
array.put(Float.parseFloat(str));
break;
default:
array.put(str);
break;
}
} else {
array.put(ScriptEngineUtils.buildFunctionCallString(evlValue));
}
}
private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object, Map<String, String> map) {
if (!object.has(PropertyConstant.TYPE)) {
return;
}
String propertyObjType = null;
if (object.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
propertyObjType = object.get(PropertyConstant.TYPE).getAsString();
}
if (propertyObjType.equals(PropertyConstant.STRING) || propertyObjType.equals(PropertyConstant.ENUM)) {
concept.put(propertyName, FormatterUtil.getMockValue(object));
} else if (propertyObjType.equals(PropertyConstant.INTEGER) || propertyObjType.equals(PropertyConstant.NUMBER)) {
try {
concept.put(propertyName, 0);
if (FormatterUtil.isMockValue(object)) {
Number value = FormatterUtil.getElementValue(object).getAsNumber();
if (value.toString().indexOf(".") == -1) {
concept.put(propertyName, value.longValue());
} else {
concept.put(propertyName, value.floatValue());
}
}
} catch (Exception e) {
valueOf(FormatterUtil.getElementValue(object).getAsString(), propertyName, concept);
}
} else if (propertyObjType.equals(PropertyConstant.BOOLEAN)) {
// 先设置空值
concept.put(propertyName, false);
try {
if (FormatterUtil.isMockValue(object)) {
String value = FormatterUtil.getMockValue(object);
if (StringUtils.isNotEmpty(value)) {
if (value.indexOf("\"") != -1) {
value = value.replaceAll("\"", StringUtils.EMPTY);
}
if (isBoolean(value)) {
concept.put(propertyName, Boolean.valueOf(value));
} else {
concept.put(propertyName, value);
String key = StringUtils.join("\"", propertyName, "\"", ": \"", value, "\"");
String targetValue = StringUtils.join("\"", propertyName, "\"", ":", value);
map.put(key, targetValue);
}
}
}
} catch (Exception e) {
concept.put(propertyName, false);
}
} else if (propertyObjType.equals(PropertyConstant.ARRAY)) {
analyzeArray(concept, propertyName, object, map);
} else if (propertyObjType.equals(PropertyConstant.OBJECT)) {
JSONObject obj = new JSONObject();
concept.put(propertyName, obj);
analyzeObject(object, obj, map);
} else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) {
concept.put(propertyName, JSONObject.NULL);
}
}
private static void analyzeArray(JSONObject concept, String propertyName, JsonObject object, Map<String, String> map) {
JSONArray array = new JSONArray();
if (object.has(PropertyConstant.ITEMS)) {
if (object.get(PropertyConstant.ITEMS).isJsonArray()) {
JsonArray jsonArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
loopArray(jsonArray, array, propertyName, object, map);
} else {
array.put(object.get(PropertyConstant.ITEMS).getAsJsonObject());
}
}
concept.put(propertyName, array);
}
private static void loopArray(JsonArray sourceArray, JSONArray targetArray, String propertyName, JsonObject object, Map<String, String> map) {
sourceArray.forEach(element -> {
JsonObject itemsObject = element.getAsJsonObject();
if (object.has(PropertyConstant.ITEMS)) {
if (FormatterUtil.isMockValue(itemsObject)) {
formatItems(itemsObject, targetArray);
} else if (itemsObject.has(PropertyConstant.TYPE) && (itemsObject.has(PropertyConstant.ENUM) || itemsObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.STRING))) {
targetArray.put(FormatterUtil.getMockValue(itemsObject));
} else if (itemsObject.has(PropertyConstant.TYPE) && itemsObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.NUMBER)) {
if (FormatterUtil.isMockValue(itemsObject)) {
arrayValueOf(FormatterUtil.getStrValue(itemsObject), targetArray);
} else {
targetArray.put(0);
}
} else if (itemsObject.has(PropertyConstant.PROPERTIES)) {
JSONObject propertyConcept = new JSONObject(true);
formatObject(map, itemsObject, propertyConcept);
targetArray.put(propertyConcept);
} else if (itemsObject.has(PropertyConstant.TYPE) && itemsObject.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
JSONObject newJsonObj = new JSONObject();
analyzeProperty(newJsonObj, propertyName + PropertyConstant.ITEM, itemsObject, map);
targetArray.put(newJsonObj.get(propertyName + PropertyConstant.ITEM));
}
} else if (object.has(PropertyConstant.ITEMS) && object.get(PropertyConstant.ITEMS).isJsonArray()) {
JsonArray itemsObjectArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
targetArray.put(itemsObjectArray);
}
});
}
private static void formatItems(JsonObject itemsObject, JSONArray array) {
try {
String type = StringUtils.EMPTY;
if (itemsObject.has(PropertyConstant.TYPE)) {
type = itemsObject.get(PropertyConstant.TYPE).getAsString();
}
if (StringUtils.equalsIgnoreCase(type, PropertyConstant.STRING)) {
String value = FormatterUtil.getStrValue(itemsObject);
array.put(value);
} else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.INTEGER)) {
int value = FormatterUtil.getElementValue(itemsObject).getAsInt();
array.put(value);
} else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.NUMBER)) {
JsonElement valueObj = FormatterUtil.getElementValue(itemsObject);
Number value = valueObj.getAsNumber();
if (StringUtils.isNotEmpty(valueObj.getAsString()) && valueObj.getAsString().indexOf(".") != -1) {
array.put(value.floatValue());
} else {
array.put(value.longValue());
}
} else {
arrayValueOf(FormatterUtil.getStrValue(itemsObject), array);
}
} catch (Exception e) {
arrayValueOf(FormatterUtil.getStrValue(itemsObject), array);
}
}
private static void formatObject(Map<String, String> map, JsonObject jsonObject, JSONObject concept) {
JsonObject propertiesObj = jsonObject.get(PropertyConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(concept, propertyKey, propertyObj, map);
}
}
private static void analyzeDefinitions(JsonObject object, Map<String, String> map) {
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();
JSONObject obj = new JSONObject();
analyzeRootSchemaElement(definitionObj, obj, map);
}
}
}
private static String formerJson(String jsonSchema, Map<String, String> map) {
JSONObject root = new JSONObject(true);
analyzeSchema(jsonSchema, root, map);
// 格式化返回
if (root.opt(PropertyConstant.MS_OBJECT) != null) {
return root.get(PropertyConstant.MS_OBJECT).toString();
}
return root.toString();
}
public static String generator(String jsonSchema) {
try {
if (StringUtils.isEmpty(jsonSchema)) {
return null;
}
Map<String, String> processMap = new HashMap<>();
String json = formerJson(jsonSchema, processMap);
if (MapUtils.isNotEmpty(processMap)) {
for (String str : processMap.keySet()) {
json = json.replace(str, processMap.get(str));
}
}
json = StringUtils.chomp(json.trim());
if (StringUtils.startsWith(json, "[") && StringUtils.endsWith(json, "]")) {
return JSONUtil.parserArray(json);
} else {
return JSONUtil.parserObject(json);
}
} catch (Exception ex) {
return jsonSchema;
}
}
}

View File

@ -1,285 +0,0 @@
package io.metersphere.api.exec.generator;
import com.google.gson.*;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.utils.EnumPropertyUtil;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;
import java.util.Map.Entry;
public class JSONSchemaGenerator {
private static void generator(String json, JSONObject obj) {
analyzeSchema(json, obj);
}
private static void analyzeSchema(String json, JSONObject rootObj) {
Gson gson = new Gson();
JsonElement element = gson.fromJson(json, JsonElement.class);
JsonObject rootElement = element.getAsJsonObject();
analyzeRootSchemaElement(rootElement, rootObj);
}
private static void analyzeRootSchemaElement(JsonObject rootElement, JSONObject rootObj) {
if (rootElement.has(PropertyConstant.TYPE) || rootElement.has(PropertyConstant.ALL_OF)) {
analyzeObject(rootElement, rootObj);
}
if (rootElement.has("definitions")) {
analyzeDefinitions(rootElement);
}
}
private static void analyzeObject(JsonObject object, JSONObject rootObj) {
if (object != null && object.has(PropertyConstant.ALL_OF)) {
JsonArray allOfArray = object.get(PropertyConstant.ALL_OF).getAsJsonArray();
for (JsonElement allOfElement : allOfArray) {
JsonObject allOfElementObj = allOfElement.getAsJsonObject();
if (!allOfElementObj.has(PropertyConstant.PROPERTIES)) {
continue;
}
JsonObject propertiesObj = allOfElementObj.get(PropertyConstant.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(PropertyConstant.PROPERTIES)) {
JsonObject propertiesObj = object.get(PropertyConstant.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(PropertyConstant.ADDITIONAL_PROPERTIES)) {
JsonObject propertiesObj = object.get(PropertyConstant.ADDITIONAL_PROPERTIES).getAsJsonObject();
analyzeProperty(rootObj, PropertyConstant.ADDITIONAL_PROPERTIES, propertiesObj);
} else if (object.has(PropertyConstant.TYPE) && object.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.ARRAY)) {
analyzeProperty(rootObj, PropertyConstant.MS_OBJECT, object);
} else if (object.has(PropertyConstant.TYPE) && !object.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.OBJECT)) {
analyzeProperty(rootObj, object.getAsString(), object);
}
}
private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object) {
if (object.has(PropertyConstant.TYPE)) {
String propertyObjType = StringUtils.EMPTY;
if (object.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
propertyObjType = object.get(PropertyConstant.TYPE).getAsString();
} else if (object.get(PropertyConstant.TYPE) instanceof JsonArray) {
JsonArray typeArray = (JsonArray) object.get(PropertyConstant.TYPE).getAsJsonArray();
propertyObjType = typeArray.get(0).getAsString();
if (typeArray.size() > 1) {
if (typeArray.get(1).getAsString().equals("null")) {
// 暂不处理后续使用时再加
}
}
}
if (object.has(PropertyConstant.DEFAULT)) {
concept.put(propertyName, object.get(PropertyConstant.DEFAULT).getAsString());
} else if (object.has(PropertyConstant.ENUM)) {
try {
if (object.has(PropertyConstant.MOCK) && object.get(PropertyConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString())) {
Object value = object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK);
concept.put(propertyName, value);
} else {
List<Object> list = EnumPropertyUtil.analyzeEnumProperty(object);
if (list.size() > 0) {
int index = (int) (Math.random() * list.size());
concept.put(propertyName, list.get(index));
}
}
} catch (Exception e) {
concept.put(propertyName, StringUtils.EMPTY);
}
} else if (propertyObjType.equals(PropertyConstant.STRING)) {
// 先设置空值
concept.put(propertyName, StringUtils.EMPTY);
if (object.has("format")) {
String propertyFormat = object.get("format").getAsString();
if (propertyFormat.equals("date-time")) {
}
}
if (object.has(PropertyConstant.DEFAULT)) {
concept.put(propertyName, object.get(PropertyConstant.DEFAULT));
}
if (object.has(PropertyConstant.MOCK) && object.get(PropertyConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
concept.put(propertyName, value);
}
} else if (propertyObjType.equals(PropertyConstant.INTEGER)) {
// 先设置空值
concept.put(propertyName, 0);
if (object.has(PropertyConstant.DEFAULT)) {
concept.put(propertyName, object.get(PropertyConstant.DEFAULT));
}
if (object.has(PropertyConstant.MOCK) && object.get(PropertyConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString())) {
try {
int value = object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsInt();
concept.put(propertyName, value);
} catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
concept.put(propertyName, value);
}
}
} else if (propertyObjType.equals(PropertyConstant.NUMBER)) {
// 先设置空值
concept.put(propertyName, 0);
if (object.has(PropertyConstant.DEFAULT)) {
concept.put(propertyName, object.get(PropertyConstant.DEFAULT));
}
if (object.has(PropertyConstant.MOCK) && object.get(PropertyConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString())) {
try {
Number value = object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.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(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
concept.put(propertyName, value);
}
}
} else if (propertyObjType.equals(PropertyConstant.BOOLEAN)) {
// 先设置空值
concept.put(propertyName, false);
if (object.has(PropertyConstant.DEFAULT)) {
concept.put(propertyName, object.get(PropertyConstant.DEFAULT));
}
if (object.has(PropertyConstant.MOCK) && object.get(PropertyConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).toString());
try {
if (StringUtils.isNotEmpty(value)) {
if (value.indexOf("\"") != -1) {
value = value.replaceAll("\"", StringUtils.EMPTY);
}
concept.put(propertyName, Boolean.valueOf(value));
}
} catch (Exception e) {
concept.put(propertyName, value);
}
}
} else if (propertyObjType.equals(PropertyConstant.ARRAY)) {
// 先设置空值
JSONArray array = new JSONArray();
JsonArray jsonArray = new JsonArray();
if (object.has(PropertyConstant.ITEMS)) {
if (object.get(PropertyConstant.ITEMS).isJsonArray()) {
jsonArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
} else {
JsonObject itemsObject = object.get(PropertyConstant.ITEMS).getAsJsonObject();
array.put(itemsObject);
}
}
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
if (object.has(PropertyConstant.ITEMS)) {
if (jsonObject.has(PropertyConstant.MOCK) && jsonObject.get(PropertyConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString())) {
try {
if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.INTEGER)) {
int value = jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsInt();
array.put(value);
} else {
String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
array.put(value);
}
} catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
array.put(value);
}
} else if (jsonObject.has(PropertyConstant.ENUM)) {
array.put(EnumPropertyUtil.analyzeEnumProperty(jsonObject));
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.STRING)) {
if (jsonObject.has(PropertyConstant.DEFAULT)) {
array.put(jsonObject.get(PropertyConstant.DEFAULT));
} else if (jsonObject.has(PropertyConstant.MOCK) && jsonObject.get(PropertyConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
array.put(value);
} else {
array.put(StringUtils.EMPTY);
}
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.NUMBER)) {
if (jsonObject.has(PropertyConstant.DEFAULT)) {
array.put(jsonObject.get(PropertyConstant.DEFAULT));
} else if (jsonObject.has(PropertyConstant.MOCK) && jsonObject.get(PropertyConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString())) {
String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
array.put(value);
} else {
array.put(0);
}
} else if (jsonObject.has(PropertyConstant.PROPERTIES)) {
JSONObject propertyConcept = JSONUtil.createJsonObject(true);
JsonObject propertiesObj = jsonObject.get(PropertyConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(propertyConcept, propertyKey, propertyObj);
}
array.put(propertyConcept);
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
JSONObject newJsonObj = JSONUtil.createJsonObject(true);
analyzeProperty(newJsonObj, propertyName + PropertyConstant.ITEM, jsonObject);
array.put(newJsonObj.get(propertyName + PropertyConstant.ITEM));
}
} else if (object.has(PropertyConstant.ITEMS) && object.get(PropertyConstant.ITEMS).isJsonArray()) {
JsonArray itemsObjectArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
array.put(itemsObjectArray);
}
}
concept.put(propertyName, array);
} else if (propertyObjType.equals(PropertyConstant.OBJECT)) {
JSONObject obj = JSONUtil.createJsonObject(true);
concept.put(propertyName, obj);
analyzeObject(object, obj);
} else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) {
concept.put(propertyName, JSONObject.NULL);
}
}
}
private static void analyzeDefinitions(JsonObject object) {
JsonObject definitionsObj = object.get("definitions").getAsJsonObject();
for (Entry<String, JsonElement> entry : definitionsObj.entrySet()) {
String definitionKey = entry.getKey();
JsonObject definitionObj = definitionsObj.get(definitionKey).getAsJsonObject();
JSONObject obj = JSONUtil.createJsonObject(true);
analyzeRootSchemaElement(definitionObj, obj);
}
}
private static String formerJson(String jsonSchema) {
JSONObject root = JSONUtil.createJsonObject(true);
generator(jsonSchema, root);
// 格式化返回
if (root.opt(PropertyConstant.MS_OBJECT) != null) {
return root.get(PropertyConstant.MS_OBJECT).toString();
}
return root.toString();
}
public static String getJson(String jsonSchema) {
try {
if (StringUtils.isEmpty(jsonSchema)) {
return null;
}
String value = formerJson(jsonSchema);
if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) {
return JSONUtil.parserArray(value);
} else {
return JSONUtil.parserObject(value);
}
} catch (Exception ex) {
return jsonSchema;
}
}
}

View File

@ -0,0 +1,266 @@
package io.metersphere.api.exec.generator;
import com.google.gson.*;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.utils.EnumPropertyUtil;
import io.metersphere.commons.utils.JSONUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;
import java.util.Map.Entry;
public class JSONSchemaParser {
private static void analyzeSchema(String json, JSONObject rootObj) {
Gson gson = new Gson();
JsonElement element = gson.fromJson(json, JsonElement.class);
JsonObject rootElement = element.getAsJsonObject();
analyzeRootSchemaElement(rootElement, rootObj);
}
private static void analyzeRootSchemaElement(JsonObject rootElement, JSONObject rootObj) {
if (rootElement.has(PropertyConstant.TYPE) || rootElement.has(PropertyConstant.ALL_OF)) {
analyzeObject(rootElement, rootObj);
}
if (rootElement.has("definitions")) {
analyzeDefinitions(rootElement);
}
}
private static void analyzeObject(JsonObject object, JSONObject rootObj) {
if (object != null && object.has(PropertyConstant.ALL_OF)) {
JsonArray allOfArray = object.get(PropertyConstant.ALL_OF).getAsJsonArray();
for (JsonElement allOfElement : allOfArray) {
JsonObject allOfElementObj = allOfElement.getAsJsonObject();
if (!allOfElementObj.has(PropertyConstant.PROPERTIES)) {
continue;
}
formatObject(allOfElementObj, rootObj);
}
} else if (object.has(PropertyConstant.PROPERTIES)) {
formatObject(object, rootObj);
} else if (object.has(PropertyConstant.ADDITIONAL_PROPERTIES)) {
analyzeProperty(rootObj, PropertyConstant.ADDITIONAL_PROPERTIES, object.get(PropertyConstant.ADDITIONAL_PROPERTIES).getAsJsonObject());
} else if (object.has(PropertyConstant.TYPE) && object.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.ARRAY)) {
analyzeProperty(rootObj, PropertyConstant.MS_OBJECT, object);
} else if (object.has(PropertyConstant.TYPE) && !object.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.OBJECT)) {
analyzeProperty(rootObj, object.getAsString(), object);
}
}
private static void formatObject(JsonObject object, JSONObject rootObj) {
JsonObject propertiesObj = object.get(PropertyConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(rootObj, propertyKey, propertyObj);
}
}
private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object) {
if (!object.has(PropertyConstant.TYPE)) {
return;
}
String type = StringUtils.EMPTY;
if (object.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
type = object.get(PropertyConstant.TYPE).getAsString();
}
if (object.has(PropertyConstant.DEFAULT)) {
concept.put(propertyName, object.get(PropertyConstant.DEFAULT).getAsString());
} else if (object.has(PropertyConstant.ENUM)) {
concept.put(propertyName, StringUtils.EMPTY);
if (FormatterUtil.isMockValue(object)) {
concept.put(propertyName, FormatterUtil.getElementValue(object));
} else {
List<Object> list = EnumPropertyUtil.analyzeEnumProperty(object);
if (CollectionUtils.isNotEmpty(list)) {
int index = (int) (Math.random() * list.size());
concept.put(propertyName, list.get(index));
}
}
} else if (StringUtils.equals(type, PropertyConstant.STRING)) {
// 先设置空值
concept.put(propertyName, StringUtils.EMPTY);
if (object.has(PropertyConstant.DEFAULT)) {
concept.put(propertyName, object.get(PropertyConstant.DEFAULT));
}
if (FormatterUtil.isMockValue(object)) {
String value = FormatterUtil.getStrValue(object);
concept.put(propertyName, value);
}
} else if (StringUtils.equals(type, PropertyConstant.INTEGER)) {
// 先设置空值
concept.put(propertyName, 0);
if (object.has(PropertyConstant.DEFAULT)) {
concept.put(propertyName, object.get(PropertyConstant.DEFAULT));
}
// 设置mock值
if (FormatterUtil.isMockValue(object)) {
if (FormatterUtil.isNumber(FormatterUtil.getStrValue(object))) {
int value = FormatterUtil.getElementValue(object).getAsInt();
concept.put(propertyName, value);
} else {
String value = FormatterUtil.getStrValue(object);
concept.put(propertyName, value);
}
}
} else if (StringUtils.equals(type, PropertyConstant.NUMBER)) {
// 先设置空值
concept.put(propertyName, 0);
if (object.has(PropertyConstant.DEFAULT)) {
concept.put(propertyName, object.get(PropertyConstant.DEFAULT));
}
if (FormatterUtil.isMockValue(object)) {
if (FormatterUtil.isNumber(FormatterUtil.getStrValue(object))) {
Number value = FormatterUtil.getElementValue(object).getAsNumber();
if (value.toString().indexOf(".") == -1) {
concept.put(propertyName, value.longValue());
} else {
concept.put(propertyName, value.floatValue());
}
} else {
concept.put(propertyName, FormatterUtil.getStrValue(object));
}
}
} else if (StringUtils.equals(type, PropertyConstant.BOOLEAN)) {
// 先设置空值
concept.put(propertyName, false);
if (object.has(PropertyConstant.DEFAULT)) {
concept.put(propertyName, object.get(PropertyConstant.DEFAULT));
}
if (FormatterUtil.isMockValue(object)) {
String value = FormatterUtil.getStrValue(object);
if (StringUtils.isNotEmpty(value)) {
if (value.indexOf("\"") != -1) {
value = value.replaceAll("\"", StringUtils.EMPTY);
}
concept.put(propertyName, Boolean.valueOf(value));
}
}
} else if (StringUtils.equals(type, PropertyConstant.ARRAY)) {
analyzeArray(concept, propertyName, object);
} else if (StringUtils.equals(type, PropertyConstant.OBJECT)) {
JSONObject obj = JSONUtil.createJsonObject(true);
concept.put(propertyName, obj);
analyzeObject(object, obj);
} else if (StringUtils.equalsIgnoreCase(type, "null")) {
concept.put(propertyName, JSONObject.NULL);
} else {
concept.put(propertyName, StringUtils.EMPTY);
}
}
private static void analyzeArray(JSONObject concept, String propertyName, JsonObject object) {
JSONArray array = new JSONArray();
if (object.has(PropertyConstant.ITEMS)) {
if (object.get(PropertyConstant.ITEMS).isJsonArray()) {
JsonArray jsonArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
loopArray(array, jsonArray, object, propertyName);
} else {
JsonObject itemsObject = object.get(PropertyConstant.ITEMS).getAsJsonObject();
array.put(itemsObject);
}
}
concept.put(propertyName, array);
}
private static void loopArray(JSONArray array, JsonArray jsonArray, JsonObject object, String propertyName) {
jsonArray.forEach(element -> {
JsonObject jsonObject = element.getAsJsonObject();
if (object.has(PropertyConstant.ITEMS)) {
if (FormatterUtil.isMockValue(jsonObject)) {
if (jsonObject.has(PropertyConstant.TYPE)
&& jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.INTEGER)
&& FormatterUtil.isNumber(FormatterUtil.getStrValue(jsonObject))) {
array.put(FormatterUtil.getElementValue(jsonObject).getAsInt());
} else {
array.put(FormatterUtil.getStrValue(jsonObject));
}
} else if (jsonObject.has(PropertyConstant.ENUM)) {
array.put(EnumPropertyUtil.analyzeEnumProperty(jsonObject));
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.STRING)) {
if (jsonObject.has(PropertyConstant.DEFAULT)) {
array.put(jsonObject.get(PropertyConstant.DEFAULT));
} else if (FormatterUtil.isMockValue(jsonObject)) {
array.put(FormatterUtil.getStrValue(jsonObject));
} else {
array.put(StringUtils.EMPTY);
}
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.NUMBER)) {
if (jsonObject.has(PropertyConstant.DEFAULT)) {
array.put(jsonObject.get(PropertyConstant.DEFAULT));
} else if (FormatterUtil.isMockValue(jsonObject)) {
array.put(FormatterUtil.getStrValue(jsonObject));
} else {
array.put(0);
}
} else if (jsonObject.has(PropertyConstant.PROPERTIES)) {
JSONObject propertyConcept = JSONUtil.createJsonObject(true);
formatObject(jsonObject, propertyConcept);
array.put(propertyConcept);
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
JSONObject newJsonObj = JSONUtil.createJsonObject(true);
analyzeProperty(newJsonObj, propertyName + PropertyConstant.ITEM, jsonObject);
array.put(newJsonObj.get(propertyName + PropertyConstant.ITEM));
}
} else if (object.has(PropertyConstant.ITEMS) && object.get(PropertyConstant.ITEMS).isJsonArray()) {
JsonArray itemsObjectArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
array.put(itemsObjectArray);
}
});
}
private static void analyzeDefinitions(JsonObject object) {
JsonObject definitionsObj = object.get("definitions").getAsJsonObject();
for (Entry<String, JsonElement> entry : definitionsObj.entrySet()) {
String definitionKey = entry.getKey();
JsonObject definitionObj = definitionsObj.get(definitionKey).getAsJsonObject();
JSONObject obj = JSONUtil.createJsonObject(true);
analyzeRootSchemaElement(definitionObj, obj);
}
}
private static String formerJson(String jsonSchema) {
JSONObject root = JSONUtil.createJsonObject(true);
analyzeSchema(jsonSchema, root);
// 格式化返回
if (root.opt(PropertyConstant.MS_OBJECT) != null) {
return root.get(PropertyConstant.MS_OBJECT).toString();
}
return root.toString();
}
public static String schemaToJson(String jsonSchema) {
try {
if (StringUtils.isEmpty(jsonSchema)) {
return null;
}
String value = formerJson(jsonSchema);
if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) {
return JSONUtil.parserArray(value);
} else {
return JSONUtil.parserObject(value);
}
} catch (Exception ex) {
return jsonSchema;
}
}
public static String preview(String jsonSchema) {
try {
String value = JSONSchemaBuilder.generator(jsonSchema);
if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) {
return JSONUtil.parserArray(value);
} else {
return JSONUtil.parserObject(value);
}
} catch (Exception ex) {
return jsonSchema;
}
}
}

View File

@ -1,304 +0,0 @@
package io.metersphere.api.exec.generator;
import com.google.gson.*;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.util.NumberUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class JSONSchemaRunTest {
private static void generator(String json, JSONObject obj, Map<String, String> map) {
analyzeSchema(json, obj, map);
}
private static void analyzeSchema(String json, JSONObject rootObj, Map<String, String> map) {
Gson gson = new Gson();
JsonElement element = gson.fromJson(json, JsonElement.class);
JsonObject rootElement = element.getAsJsonObject();
analyzeRootSchemaElement(rootElement, rootObj, map);
}
private static void analyzeRootSchemaElement(JsonObject rootElement, JSONObject rootObj, Map<String, String> map) {
if (rootElement.has(PropertyConstant.TYPE) || rootElement.has(PropertyConstant.ALL_OF)) {
analyzeObject(rootElement, rootObj, map);
}
if (rootElement.has("definitions")) {
analyzeDefinitions(rootElement, map);
}
}
private static boolean isMock(JsonObject itemsObject) {
return (itemsObject.has(PropertyConstant.MOCK) && itemsObject.get(PropertyConstant.MOCK).getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString()));
}
private static void analyzeObject(JsonObject object, JSONObject rootObj, Map<String, String> map) {
if (object.has(PropertyConstant.ALL_OF)) {
JsonArray allOfArray = object.get(PropertyConstant.ALL_OF).getAsJsonArray();
for (JsonElement allOfElement : allOfArray) {
JsonObject allOfElementObj = allOfElement.getAsJsonObject();
if (allOfElementObj.has(PropertyConstant.PROPERTIES)) {
JsonObject propertiesObj = allOfElementObj.get(PropertyConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(rootObj, propertyKey, propertyObj, map);
}
}
}
} else if (object.has(PropertyConstant.PROPERTIES)) {
JsonObject propertiesObj = object.get(PropertyConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(rootObj, propertyKey, propertyObj, map);
}
} else if (object.has(PropertyConstant.TYPE) && object.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.ARRAY)) {
analyzeProperty(rootObj, PropertyConstant.MS_OBJECT, object, map);
} else if (object.has(PropertyConstant.TYPE) && !object.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.OBJECT)) {
analyzeProperty(rootObj, object.getAsString(), object, map);
}
}
public static boolean isBoolean(String value) {
return "true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value);
}
private static void valueOf(String evlValue, String propertyName, JSONObject concept) {
if (StringUtils.startsWith(evlValue, "@")) {
String str = ScriptEngineUtils.calculate(evlValue);
switch (evlValue) {
case "@integer":
concept.put(propertyName, NumberUtils.parseNumber(str, Long.class));
break;
case "@boolean":
concept.put(propertyName, Boolean.parseBoolean(str));
break;
case "@float":
concept.put(propertyName, Float.parseFloat(str));
break;
default:
concept.put(propertyName, str);
break;
}
} else {
String value = ScriptEngineUtils.buildFunctionCallString(evlValue);
concept.put(propertyName, value);
}
}
private static void arrayValueOf(String evlValue, JSONArray array) {
if (StringUtils.startsWith(evlValue, "@")) {
String str = ScriptEngineUtils.calculate(evlValue);
switch (evlValue) {
case "@integer":
array.put(NumberUtils.parseNumber(str, Long.class));
break;
case "@boolean":
array.put(Boolean.parseBoolean(str));
break;
case "@float":
array.put(Float.parseFloat(str));
break;
default:
array.put(str);
break;
}
} else {
String value = ScriptEngineUtils.buildFunctionCallString(evlValue);
array.put(value);
}
}
private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object, Map<String, String> map) {
if (!object.has(PropertyConstant.TYPE)) {
return;
}
String propertyObjType = null;
if (object.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
propertyObjType = object.get(PropertyConstant.TYPE).getAsString();
}
if (propertyObjType.equals(PropertyConstant.STRING) || propertyObjType.equals(PropertyConstant.ENUM)) {
concept.put(propertyName, getValue(object));
} else if (propertyObjType.equals(PropertyConstant.INTEGER) || propertyObjType.equals(PropertyConstant.NUMBER)) {
try {
concept.put(propertyName, 0);
if (isMock(object)) {
Number value = object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsNumber();
if (value.toString().indexOf(".") == -1) {
concept.put(propertyName, value.longValue());
} else {
concept.put(propertyName, value.floatValue());
}
}
} catch (Exception e) {
valueOf(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString(), propertyName, concept);
}
} else if (propertyObjType.equals(PropertyConstant.BOOLEAN)) {
// 先设置空值
concept.put(propertyName, false);
try {
if (isMock(object)) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).toString());
if (StringUtils.isNotEmpty(value)) {
if (value.indexOf("\"") != -1) {
value = value.replaceAll("\"", StringUtils.EMPTY);
}
if (isBoolean(value)) {
concept.put(propertyName, Boolean.valueOf(value));
} else {
concept.put(propertyName, value);
map.put("\"" + propertyName + "\"" + ": \"" + value + "\"", "\"" + propertyName + "\"" + ":" + value);
}
}
}
} catch (Exception e) {
concept.put(propertyName, false);
}
} else if (propertyObjType.equals(PropertyConstant.ARRAY)) {
JSONArray array = new JSONArray();
JsonArray jsonArray = new JsonArray();
if (object.has(PropertyConstant.ITEMS)) {
if (object.get(PropertyConstant.ITEMS).isJsonArray()) {
jsonArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
} else {
JsonObject itemsObject = object.get(PropertyConstant.ITEMS).getAsJsonObject();
array.put(itemsObject);
}
}
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject itemsObject = jsonArray.get(i).getAsJsonObject();
if (object.has(PropertyConstant.ITEMS)) {
if (isMock(itemsObject)) {
try {
String type = StringUtils.EMPTY;
if (itemsObject.has(PropertyConstant.TYPE)) {
type = itemsObject.get(PropertyConstant.TYPE).getAsString();
}
if (StringUtils.equalsIgnoreCase(type, PropertyConstant.STRING)) {
String value = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString();
array.put(value);
} else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.INTEGER)) {
int value = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsInt();
array.put(value);
} else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.NUMBER)) {
JsonElement valueObj = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK);
Number value = valueObj.getAsNumber();
if (StringUtils.isNotEmpty(valueObj.getAsString()) && valueObj.getAsString().indexOf(".") != -1) {
array.put(value.floatValue());
} else {
array.put(value.longValue());
}
} else {
arrayValueOf(itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString(), array);
}
} catch (Exception e) {
arrayValueOf(itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString(), array);
}
} else if (itemsObject.has(PropertyConstant.TYPE) && (itemsObject.has(PropertyConstant.ENUM) || itemsObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.STRING))) {
array.put(getValue(itemsObject));
} else if (itemsObject.has(PropertyConstant.TYPE) && itemsObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.NUMBER)) {
if (isMock(itemsObject)) {
arrayValueOf(itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString(), array);
} else {
array.put(0);
}
} else if (itemsObject.has(PropertyConstant.PROPERTIES)) {
JSONObject propertyConcept = new JSONObject(true);
JsonObject propertiesObj = itemsObject.get(PropertyConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(propertyConcept, propertyKey, propertyObj, map);
}
array.put(propertyConcept);
} else if (itemsObject.has(PropertyConstant.TYPE) && itemsObject.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
JSONObject newJsonObj = new JSONObject();
analyzeProperty(newJsonObj, propertyName + PropertyConstant.ITEM, itemsObject, map);
array.put(newJsonObj.get(propertyName + PropertyConstant.ITEM));
}
} else if (object.has(PropertyConstant.ITEMS) && object.get(PropertyConstant.ITEMS).isJsonArray()) {
JsonArray itemsObjectArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
array.put(itemsObjectArray);
}
}
concept.put(propertyName, array);
} else if (propertyObjType.equals(PropertyConstant.OBJECT)) {
JSONObject obj = new JSONObject();
concept.put(propertyName, obj);
analyzeObject(object, obj, map);
} else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) {
concept.put(propertyName, JSONObject.NULL);
}
}
private static Object getValue(JsonObject object) {
try {
if (isMock(object)) {
String value = ScriptEngineUtils.buildFunctionCallString(object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
return value;
}
} catch (Exception e) {
return object.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK);
}
return StringUtils.EMPTY;
}
private static void analyzeDefinitions(JsonObject object, Map<String, String> map) {
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();
JSONObject obj = new JSONObject();
analyzeRootSchemaElement(definitionObj, obj, map);
}
}
}
private static String formerJson(String jsonSchema, Map<String, String> map) {
JSONObject root = new JSONObject(true);
generator(jsonSchema, root, map);
// 格式化返回
if (root.opt(PropertyConstant.MS_OBJECT) != null) {
return root.get(PropertyConstant.MS_OBJECT).toString();
}
return root.toString();
}
public static String getJson(String jsonSchema) {
try {
if (StringUtils.isEmpty(jsonSchema)) {
return null;
}
Map<String, String> map = new HashMap<>();
String json = formerJson(jsonSchema, map);
if (MapUtils.isNotEmpty(map)) {
for (String str : map.keySet()) {
json = json.replace(str, map.get(str));
}
}
String value = StringUtils.chomp(json.trim());
if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) {
return JSONUtil.parserArray(value);
} else {
return JSONUtil.parserObject(value);
}
} catch (Exception ex) {
return jsonSchema;
}
}
}

View File

@ -1,4 +1,4 @@
package io.metersphere.service; package io.metersphere.api.exec.generator;
import com.apifan.common.random.source.DateTimeSource; import com.apifan.common.random.source.DateTimeSource;
import com.apifan.common.random.source.InternetSource; import com.apifan.common.random.source.InternetSource;
@ -6,7 +6,6 @@ import com.apifan.common.random.source.NumberSource;
import com.google.gson.*; import com.google.gson.*;
import com.mifmif.common.regex.Generex; import com.mifmif.common.regex.Generex;
import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject; import org.json.JSONObject;
@ -15,8 +14,6 @@ import java.time.LocalDate;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* 生成测试数据 * 生成测试数据
@ -177,27 +174,15 @@ public class TestDataGenerator {
} catch (Exception e) { } catch (Exception e) {
return value; return value;
} }
} }
public static boolean isNumber(Object obj) {
if (ObjectUtils.isEmpty(obj)) {
return false;
}
Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
Matcher isNum = pattern.matcher(obj.toString());
if (!isNum.matches()) {
return false;
}
return true;
}
public static Object analyzeInteger(JsonObject object) { public static Object analyzeInteger(JsonObject object) {
// 先设置空值 // 先设置空值
if (object.has(DEFAULT) && isNumber(object.get(DEFAULT))) { if (object.has(DEFAULT) && FormatterUtil.isNumber(object.get(DEFAULT))) {
return object.get(DEFAULT).getAsInt(); return object.get(DEFAULT).getAsInt();
} }
Object mockValue = getMockValue(object); Object mockValue = getMockValue(object);
if (mockValue != null && isNumber(mockValue)) { if (mockValue != null && FormatterUtil.isNumber(mockValue)) {
return Integer.parseInt(mockValue.toString()); return Integer.parseInt(mockValue.toString());
} }
int minimum = 1; int minimum = 1;
@ -212,11 +197,11 @@ public class TestDataGenerator {
} }
public static Object analyzeNumber(JsonObject object) { public static Object analyzeNumber(JsonObject object) {
if (object != null && object.has(DEFAULT) && isNumber(object.has(DEFAULT))) { if (object != null && object.has(DEFAULT) && FormatterUtil.isNumber(object.has(DEFAULT))) {
return object.get(DEFAULT).getAsFloat(); return object.get(DEFAULT).getAsFloat();
} }
Object mockValue = getMockValue(object); Object mockValue = getMockValue(object);
if (mockValue != null && isNumber(mockValue)) { if (mockValue != null && FormatterUtil.isNumber(mockValue)) {
return Float.parseFloat(mockValue.toString()); return Float.parseFloat(mockValue.toString());
} }
float maximum = 200001.0f; float maximum = 200001.0f;

View File

@ -4,7 +4,7 @@ import io.metersphere.api.dto.mock.ApiDefinitionResponseDTO;
import io.metersphere.api.dto.mock.MockConfigRequestParams; import io.metersphere.api.dto.mock.MockConfigRequestParams;
import io.metersphere.api.dto.mock.RequestMockParams; import io.metersphere.api.dto.mock.RequestMockParams;
import io.metersphere.api.dto.shell.filter.ScriptFilter; import io.metersphere.api.dto.shell.filter.ScriptFilter;
import io.metersphere.api.exec.generator.JSONSchemaGenerator; import io.metersphere.api.exec.generator.JSONSchemaParser;
import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.enums.MockParamConditionEnums; import io.metersphere.commons.enums.MockParamConditionEnums;
@ -12,6 +12,7 @@ import io.metersphere.commons.enums.MockRequestType;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*; import io.metersphere.commons.utils.*;
import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -19,7 +20,6 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
import jakarta.servlet.http.HttpServletRequest;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
@ -88,7 +88,7 @@ public class MockApiUtils {
if (isJsonSchema) { if (isJsonSchema) {
if (bodyObj.has("jsonSchema")) { if (bodyObj.has("jsonSchema")) {
String bodyReturnStr = bodyObj.optJSONObject("jsonSchema").toString(); String bodyReturnStr = bodyObj.optJSONObject("jsonSchema").toString();
jsonString = JSONSchemaGenerator.getJson(bodyReturnStr); jsonString = JSONSchemaParser.schemaToJson(bodyReturnStr);
} }
} else { } else {
if (bodyObj.has("raw")) { if (bodyObj.has("raw")) {
@ -188,7 +188,7 @@ public class MockApiUtils {
} }
} }
if (isJsonSchema && bodyObj.has("jsonSchema")) { if (isJsonSchema && bodyObj.has("jsonSchema")) {
String json = JSONSchemaGenerator.getJson(bodyObj.optJSONObject("jsonSchema").toString()); String json = JSONSchemaParser.schemaToJson(bodyObj.optJSONObject("jsonSchema").toString());
if (StringUtils.isNotEmpty(json)) { if (StringUtils.isNotEmpty(json)) {
returnStr = json; returnStr = json;
} }
@ -305,7 +305,7 @@ public class MockApiUtils {
} }
} }
if (isJsonSchema && bodyObj.has("jsonSchema")) { if (isJsonSchema && bodyObj.has("jsonSchema")) {
String json = JSONSchemaGenerator.getJson(bodyObj.optJSONObject("jsonSchema").toString()); String json = JSONSchemaParser.schemaToJson(bodyObj.optJSONObject("jsonSchema").toString());
if (StringUtils.isNotEmpty(json)) { if (StringUtils.isNotEmpty(json)) {
returnStr = json; returnStr = json;
} }

View File

@ -1,6 +1,6 @@
package io.metersphere.controller; package io.metersphere.controller;
import io.metersphere.service.TestDataGenerator; import io.metersphere.api.exec.generator.TestDataGenerator;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;

View File

@ -13,7 +13,7 @@ import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.swaggerurl.SwaggerTaskResult; import io.metersphere.api.dto.swaggerurl.SwaggerTaskResult;
import io.metersphere.api.dto.swaggerurl.SwaggerUrlRequest; import io.metersphere.api.dto.swaggerurl.SwaggerUrlRequest;
import io.metersphere.api.exec.api.ApiExecuteService; import io.metersphere.api.exec.api.ApiExecuteService;
import io.metersphere.api.exec.generator.JSONSchemaGenerator; import io.metersphere.api.exec.generator.JSONSchemaParser;
import io.metersphere.api.exec.queue.ExecThreadPoolExecutor; import io.metersphere.api.exec.queue.ExecThreadPoolExecutor;
import io.metersphere.api.parse.api.ApiDefinitionImport; import io.metersphere.api.parse.api.ApiDefinitionImport;
import io.metersphere.base.domain.ApiDefinition; import io.metersphere.base.domain.ApiDefinition;
@ -34,13 +34,12 @@ import io.metersphere.log.annotation.MsAuditLog;
import io.metersphere.notice.annotation.SendNotice; import io.metersphere.notice.annotation.SendNotice;
import io.metersphere.request.ResetOrderRequest; import io.metersphere.request.ResetOrderRequest;
import io.metersphere.service.definition.ApiDefinitionService; import io.metersphere.service.definition.ApiDefinitionService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List; import java.util.List;
@ -299,7 +298,12 @@ public class ApiDefinitionController {
@PostMapping("/preview") @PostMapping("/preview")
public String preview(@RequestBody String jsonSchema) { public String preview(@RequestBody String jsonSchema) {
return JSONSchemaGenerator.getJson(jsonSchema); return JSONSchemaParser.preview(jsonSchema);
}
@PostMapping("/schema-json")
public String schemaToJson(@RequestBody String jsonSchema) {
return JSONSchemaParser.schemaToJson(jsonSchema);
} }
@GetMapping("/mock-environment/{projectId}") @GetMapping("/mock-environment/{projectId}")

View File

@ -7,7 +7,7 @@ import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import io.metersphere.api.dto.automation.ApiScenarioReportResult; import io.metersphere.api.dto.automation.ApiScenarioReportResult;
import io.metersphere.api.dto.share.*; import io.metersphere.api.dto.share.*;
import io.metersphere.api.exec.generator.JSONSchemaGenerator; import io.metersphere.api.exec.generator.JSONSchemaParser;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ShareInfoMapper; import io.metersphere.base.mapper.ShareInfoMapper;
import io.metersphere.base.mapper.UserMapper; import io.metersphere.base.mapper.UserMapper;
@ -277,7 +277,7 @@ public class ShareInfoService extends BaseShareInfoService {
if (isJsonSchema) { if (isJsonSchema) {
jsonSchemaBodyDTO.setJsonSchema(bodyObj.get("jsonSchema")); jsonSchemaBodyDTO.setJsonSchema(bodyObj.get("jsonSchema"));
apiInfoDTO.setJsonSchemaBody(jsonSchemaBodyDTO); apiInfoDTO.setJsonSchemaBody(jsonSchemaBodyDTO);
String raw = JSONSchemaGenerator.getJson(JSONUtil.toJSONString(bodyObj.get("jsonSchema"))); String raw = JSONSchemaParser.schemaToJson(JSONUtil.toJSONString(bodyObj.get("jsonSchema")));
this.setPreviewData(previewJsonArray, raw); this.setPreviewData(previewJsonArray, raw);
} else { } else {
if (bodyObj.has("raw")) { if (bodyObj.has("raw")) {

View File

@ -161,6 +161,10 @@ export function apiPreview(params) {
return post('/api/definition/preview', params); return post('/api/definition/preview', params);
} }
export function schemaToJson(params) {
return post('/api/definition/schema-json', params);
}
export function apiDebug(file, files, params) { export function apiDebug(file, files, params) {
let url = '/api/definition/run/debug'; let url = '/api/definition/run/debug';
return fileUpload(url, file, files, params); return fileUpload(url, file, files, params);

View File

@ -43,7 +43,7 @@
</el-tab-pane> </el-tab-pane>
<el-tab-pane v-if="showPreview" :label="$t('schema.preview')" name="preview"> <el-tab-pane v-if="showPreview" :label="$t('schema.preview')" name="preview">
<div style="min-height: 200px"> <div style="min-height: 200px">
<pre style="width: 100%; white-space: pre-wrap;">{{ this.preview }}</pre> <pre style="width: 100%; white-space: pre-wrap">{{ this.preview }}</pre>
</div> </div>
</el-tab-pane> </el-tab-pane>
</el-tabs> </el-tabs>
@ -148,7 +148,7 @@ export default {
if (this.activeName === 'preview') { if (this.activeName === 'preview') {
this.loading = true; this.loading = true;
// //
MsConvert.schemaToJsonStr(this.schema.root, (result) => { MsConvert.preview(this.schema.root, (result) => {
this.preview = result; this.preview = result;
this.loading = false; this.loading = false;
}); });

View File

@ -6,7 +6,7 @@ const isNumber = require('lodash.isnumber');
const isObject = require('lodash.isobject'); const isObject = require('lodash.isobject');
const isString = require('lodash.isstring'); const isString = require('lodash.isstring');
const { post } = require('@/api/base-network'); const { post } = require('@/api/base-network');
const { apiPreview } = require('@/api/definition'); const { schemaToJson, apiPreview } = require('@/api/definition');
const isArray = Array.isArray; const isArray = Array.isArray;
class Convert { class Convert {
@ -248,6 +248,14 @@ class Convert {
* @param callback * @param callback
*/ */
schemaToJsonStr(schema, callback) { schemaToJsonStr(schema, callback) {
schemaToJson(schema).then((response) => {
if (callback) {
callback(response.data);
}
});
}
preview(schema, callback) {
apiPreview(schema).then((response) => { apiPreview(schema).then((response) => {
if (callback) { if (callback) {
callback(response.data); callback(response.data);

View File

@ -1,28 +1,20 @@
package io.metersphere.metadata.service; package io.metersphere.metadata.service;
import io.metersphere.request.variable.ScenarioVariable;
import io.metersphere.dto.Body;
import io.metersphere.dto.KeyValue;
import io.metersphere.request.BodyFile;
import io.metersphere.base.domain.FileAssociation; import io.metersphere.base.domain.FileAssociation;
import io.metersphere.base.domain.FileAssociationExample; import io.metersphere.base.domain.FileAssociationExample;
import io.metersphere.base.mapper.FileAssociationMapper; import io.metersphere.base.mapper.FileAssociationMapper;
import io.metersphere.commons.constants.FileAssociationType; import io.metersphere.request.BodyFile;
import io.metersphere.plugin.core.MsTestElement; import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.mybatis.spring.SqlSessionUtils; import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
@Service @Service
public class FileAssociationService { public class FileAssociationService {
@ -101,77 +93,4 @@ public class FileAssociationService {
} }
} }
} }
// public void saveApi(String id, MsTestElement request, String type) {
// this.deleteByResourceId(id);
// if (StringUtils.isNotEmpty(id) && request != null && StringUtils.equalsIgnoreCase(request.getType(), HTTPSamplerProxy.class.getSimpleName())) {
// MsHTTPSamplerProxy samplerProxy = (MsHTTPSamplerProxy) request;
// List<BodyFile> files = getRefFiles(samplerProxy.getBody());
// this.save(files, type, id);
// }
// }
//
// public void saveScenario(String id, MsTestElement scenario) {
// this.deleteByResourceId(id);
// List<BodyFile> files = new ArrayList<>();
// if (scenario != null && scenario instanceof MsScenario) {
// MsScenario msScenario = (MsScenario) scenario;
// if (!CollectionUtils.isEmpty(msScenario.getVariables())) {
// msScenario.getVariables().stream().filter(ScenarioVariable::isCSVValid).forEach(keyValue -> {
// files.addAll(keyValue.getFiles().stream().filter(BodyFile::isRef).collect(Collectors.toList()));
// });
// }
// }
// if (StringUtils.isNotEmpty(id) && scenario != null &&
// !CollectionUtils.isEmpty(scenario.getHashTree())) {
// this.getHashTree(scenario.getHashTree(), files);
// if (!CollectionUtils.isEmpty(files)) {
// List<BodyFile> list = files.stream().distinct().collect(Collectors.toList());
// this.save(list, FileAssociationType.SCENARIO.name(), id);
// }
// }
// }
//
// public void saveEnvironment(String id, String config, String type) {
// this.deleteByResourceId(id);
// List<BodyFile> files = new ArrayList<>();
// if (StringUtils.isNotEmpty(config)) {
// JSONObject commonConfig = JSONObject.parseObject(config).getJSONObject("commonConfig");
// List<ScenarioVariable> list = JSONObject.parseArray(commonConfig.getString("variables"), ScenarioVariable.class);
// list.stream().filter(ScenarioVariable::isCSVValid).forEach(keyValue -> {
// files.addAll(keyValue.getFiles().stream().filter(BodyFile::isRef).collect(Collectors.toList()));
// });
// }
// if (!CollectionUtils.isEmpty(files)) {
// List<BodyFile> list = files.stream().distinct().collect(Collectors.toList());
// this.save(list, type, id);
// }
// }
// private void getHashTree(List<MsTestElement> testElements, List<BodyFile> files) {
// testElements.forEach(item -> {
// if (StringUtils.equalsIgnoreCase(item.getType(), HTTPSamplerProxy.class.getSimpleName())) {
// MsHTTPSamplerProxy samplerProxy = (MsHTTPSamplerProxy) item;
// List<BodyFile> itemFiles = getRefFiles(samplerProxy.getBody());
// files.addAll(itemFiles);
// } else if (!CollectionUtils.isEmpty(item.getHashTree())) {
// this.getHashTree(item.getHashTree(), files);
// }
// });
// }
// private List<BodyFile> getRefFiles(Body body) {
// List<BodyFile> files = new ArrayList<>();
// if (body != null && !CollectionUtils.isEmpty(body.getKvs())) {
// body.getKvs().stream().filter(KeyValue::isFile).filter(KeyValue::isEnable).forEach(keyValue -> {
// files.addAll(keyValue.getFiles().stream().filter(BodyFile::isRef).collect(Collectors.toList()));
// });
// }
// if (body != null && !CollectionUtils.isEmpty(body.getBinary())) {
// body.getBinary().stream().filter(KeyValue::isFile).filter(KeyValue::isEnable).forEach(keyValue -> {
// files.addAll(keyValue.getFiles().stream().filter(BodyFile::isRef).collect(Collectors.toList()));
// });
// }
// return files;
// }
} }