feat(接口测试): 修复json-schma转字符串时顺序错乱的问题

--bug=1018267 --user=宋天阳 【接口测试】请求体json-schema和json字符串,显示顺序不一致
https://www.tapd.cn/55049933/s/1266321
This commit is contained in:
song-tianyang 2022-10-18 17:29:53 +08:00 committed by 建国
parent c5babcf763
commit 595071602a
5 changed files with 28 additions and 17 deletions

View File

@ -3,6 +3,7 @@ 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.JSONObject;
@ -215,7 +216,7 @@ public class JSONSchemaGenerator {
array.add(0);
}
} else if (jsonObject.has(PropertyConstant.PROPERTIES)) {
JSONObject propertyConcept = new JSONObject(true);
JSONObject propertyConcept = JSONUtil.createJsonObject(true);
JsonObject propertiesObj = jsonObject.get(PropertyConstant.PROPERTIES).getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
@ -225,7 +226,7 @@ public class JSONSchemaGenerator {
array.add(propertyConcept);
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
JSONObject newJsonObj = new JSONObject();
JSONObject newJsonObj = JSONUtil.createJsonObject(true);
analyzeProperty(newJsonObj, propertyName + PropertyConstant.ITEM, jsonObject);
array.add(newJsonObj.get(propertyName + PropertyConstant.ITEM));
}
@ -236,7 +237,7 @@ public class JSONSchemaGenerator {
}
concept.put(propertyName, array);
} else if (propertyObjType.equals(PropertyConstant.OBJECT)) {
JSONObject obj = new JSONObject();
JSONObject obj = JSONUtil.createJsonObject(true);
concept.put(propertyName, obj);
analyzeObject(object, obj);
} else if (StringUtils.equalsIgnoreCase(propertyObjType, "null")) {
@ -251,13 +252,13 @@ public class JSONSchemaGenerator {
for (Entry<String, JsonElement> entry : definitionsObj.entrySet()) {
String definitionKey = entry.getKey();
JsonObject definitionObj = definitionsObj.get(definitionKey).getAsJsonObject();
JSONObject obj = new JSONObject();
JSONObject obj = JSONUtil.createJsonObject(true);
analyzeRootSchemaElement(definitionObj, obj);
}
}
private static String formerJson(String jsonSchema) {
JSONObject root = new JSONObject(true);
JSONObject root = JSONUtil.createJsonObject(true);
generator(jsonSchema, root);
// 格式化返回
if (root.opt(PropertyConstant.MS_OBJECT) != null) {

View File

@ -2,7 +2,6 @@ package io.metersphere.commons.utils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@ -20,11 +19,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;
public class JSONUtil {
private static final ObjectMapper objectMapper = new ObjectMapper();
@ -54,6 +49,22 @@ public class JSONUtil {
}
}
public static JSONObject createJsonObject(boolean isOrderliness) {
JSONObject returnObj = new JSONObject();
if (isOrderliness) {
try {
Class jsonObjectClass = returnObj.getClass();//获取Class对象
Field field = jsonObjectClass.getDeclaredField("map");
field.setAccessible(true);
field.set(returnObj, new LinkedHashMap<>());
field.setAccessible(false);
} catch (Exception e) {
LogUtil.error("生成有序JSONObject失败", e);
}
}
return returnObj;
}
/**
* 解析JSONObject对象到具体类递归算法
*

View File

@ -21,16 +21,13 @@ import java.util.Map;
public class ResponseUtil {
public static RequestResultExpandDTO parseByRequestResult(RequestResult baseResult) {
//根据responseheader的信息来处理返回数据
baseResult = ResponseUtil.parseResponseBodyByHeader(baseResult);
//解析是否含有误报库信息
ErrorReportLibraryParseDTO errorCodeDTO = ErrorReportLibraryUtil.parseAssertions(baseResult);
RequestResult requestResult = errorCodeDTO.getResult();
RequestResultExpandDTO expandDTO = new RequestResultExpandDTO();
BeanUtils.copyBean(expandDTO, requestResult);
if (CollectionUtils.isNotEmpty(errorCodeDTO.getErrorCodeList())) {
Map<String, String> expandMap = new HashMap<>();
expandMap.put(ApiReportStatus.FAKE_ERROR.name(), errorCodeDTO.getErrorCodeStr());

View File

@ -22,7 +22,8 @@
</el-col>
<el-col class="assertion-btn">
<el-tooltip :content="$t('test_resource_pool.enable_disable')" placement="top" v-if="edit">
<el-switch v-model="regex.enable" class="enable-switch" size="mini" :disabled="isReadOnly" style="width: 30px;margin-right:10px"/>
<el-switch v-model="regex.enable" class="enable-switch" size="mini" :disabled="isReadOnly"
style="width: 30px;margin-right:10px"/>
</el-tooltip>
<el-button :disabled="isReadOnly" type="danger" size="mini" icon="el-icon-delete" circle @click="remove"

View File

@ -22,7 +22,8 @@
</el-col>
<el-col class="assertion-btn">
<el-tooltip :content="$t('test_resource_pool.enable_disable')" placement="top" v-if="edit">
<el-switch v-model="regex.enable" class="enable-switch" size="mini" :disabled="isReadOnly" style="width: 30px;margin-right:10px"/>
<el-switch v-model="regex.enable" class="enable-switch" size="mini" :disabled="isReadOnly"
style="width: 30px;margin-right:10px"/>
</el-tooltip>
<el-button :disabled="isReadOnly" type="danger" size="mini" icon="el-icon-delete" circle @click="remove"
@ -116,6 +117,6 @@ export default {
.assertion-btn {
text-align: center;
width: 80px;
width: 180px;
}
</style>