fix(接口测试): 修复jsonschema展示失败的缺陷

--bug=1019375 --user=王孝刚 【接口测试】接口定义,查看接口文档页面没有反应,接口和日志有报错
https://www.tapd.cn/55049933/s/1293605
This commit is contained in:
wxg0103 2022-11-09 16:12:25 +08:00 committed by wxg0103
parent ccbb0da54c
commit 2e0982cea8
5 changed files with 39 additions and 39 deletions

View File

@ -6,9 +6,9 @@ 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.LinkedList;
import java.util.List;
import java.util.Map.Entry;
@ -169,14 +169,14 @@ public class JSONSchemaGenerator {
}
} else if (propertyObjType.equals(PropertyConstant.ARRAY)) {
// 先设置空值
List<Object> array = new LinkedList<>();
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.add(itemsObject);
array.put(itemsObject);
}
}
for (int i = 0; i < jsonArray.size(); i++) {
@ -186,34 +186,34 @@ public class JSONSchemaGenerator {
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.add(value);
array.put(value);
} else {
String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
array.add(value);
array.put(value);
}
} catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
array.add(value);
array.put(value);
}
} else if (jsonObject.has(PropertyConstant.ENUM)) {
array.add(EnumPropertyUtil.analyzeEnumProperty(jsonObject));
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.add(jsonObject.get(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.add(value);
array.put(value);
} else {
array.add(StringUtils.EMPTY);
array.put(StringUtils.EMPTY);
}
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.NUMBER)) {
if (jsonObject.has(PropertyConstant.DEFAULT)) {
array.add(jsonObject.get(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.add(value);
array.put(value);
} else {
array.add(0);
array.put(0);
}
} else if (jsonObject.has(PropertyConstant.PROPERTIES)) {
JSONObject propertyConcept = JSONUtil.createJsonObject(true);
@ -223,16 +223,16 @@ public class JSONSchemaGenerator {
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(propertyConcept, propertyKey, propertyObj);
}
array.add(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.add(newJsonObj.get(propertyName + PropertyConstant.ITEM));
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.add(itemsObjectArray);
array.put(itemsObjectArray);
}
}
concept.put(propertyName, array);

View File

@ -4,15 +4,14 @@ 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.JSON;
import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.collections4.CollectionUtils;
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.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -98,26 +97,26 @@ public class JSONSchemaRunTest {
}
}
private static void arrayValueOf(String evlValue, List<Object> array) {
private static void arrayValueOf(String evlValue, JSONArray array) {
if (StringUtils.startsWith(evlValue, "@")) {
String str = ScriptEngineUtils.calculate(evlValue);
switch (evlValue) {
case "@integer":
array.add(NumberUtils.parseNumber(str, Long.class));
array.put(NumberUtils.parseNumber(str, Long.class));
break;
case "@boolean":
array.add(Boolean.parseBoolean(str));
array.put(Boolean.parseBoolean(str));
break;
case "@float":
array.add(Float.parseFloat(str));
array.put(Float.parseFloat(str));
break;
default:
array.add(str);
array.put(str);
break;
}
} else {
String value = ScriptEngineUtils.buildFunctionCallString(evlValue);
array.add(value);
array.put(value);
}
}
@ -168,14 +167,14 @@ public class JSONSchemaRunTest {
concept.put(propertyName, false);
}
} else if (propertyObjType.equals(PropertyConstant.ARRAY)) {
List<Object> array = new LinkedList<>();
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.add(itemsObject);
array.put(itemsObject);
}
}
@ -190,17 +189,17 @@ public class JSONSchemaRunTest {
}
if (StringUtils.equalsIgnoreCase(type, PropertyConstant.STRING)) {
String value = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString();
array.add(value);
array.put(value);
} else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.INTEGER)) {
int value = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsInt();
array.add(value);
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.add(value.floatValue());
array.put(value.floatValue());
} else {
array.add(value.longValue());
array.put(value.longValue());
}
} else {
arrayValueOf(itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString(), array);
@ -209,12 +208,12 @@ public class JSONSchemaRunTest {
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.add(getValue(itemsObject));
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.add(0);
array.put(0);
}
} else if (itemsObject.has(PropertyConstant.PROPERTIES)) {
JSONObject propertyConcept = new JSONObject(true);
@ -224,16 +223,16 @@ public class JSONSchemaRunTest {
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(propertyConcept, propertyKey, propertyObj, map);
}
array.add(propertyConcept);
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.add(newJsonObj.get(propertyName + PropertyConstant.ITEM));
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.add(itemsObjectArray);
array.put(itemsObjectArray);
}
}
concept.put(propertyName, array);

View File

@ -277,7 +277,7 @@ public class ShareInfoService extends BaseShareInfoService {
if (isJsonSchema) {
jsonSchemaBodyDTO.setJsonSchema(bodyObj.get("jsonSchema"));
apiInfoDTO.setJsonSchemaBody(jsonSchemaBodyDTO);
String raw = JSONSchemaGenerator.getJson(bodyObj.get("jsonSchema").asText());
String raw = JSONSchemaGenerator.getJson(JSONUtil.toJSONString(bodyObj.get("jsonSchema")));
this.setPreviewData(previewJsonArray, raw);
} else {
if (bodyObj.has("raw")) {

View File

@ -496,7 +496,7 @@ export default {
this.currentScenarioData = undefined;
this.getParentVariables(this.node);
getOwnerProjectIds().then(res => {
const project = res.data.find(p => p === resource.projectId);
const project = res.data.find(p => p === this.request.projectId);
if (!project) {
this.$warning(this.$t('automation.project_no_permission'));
} else {

View File

@ -17,11 +17,12 @@
:http-request="upload"
:beforeUpload="uploadValidate"
ref="uploadLocal">
<el-button type="text" :disabled="isReadOnly"> {{ $t('permission.project_file.local_upload') }}</el-button>
<el-button type="text" :disabled="isReadOnly"
v-if="!isReadOnly"> {{ $t('permission.project_file.local_upload') }}</el-button>
<span slot="file"/>
</el-upload>
</div>
<el-button type="text" @click="associationFile"
<el-button type="text" @click="associationFile" v-if="!isReadOnly"
:disabled="isReadOnly">{{ $t('permission.project_file.associated_files') }}</el-button>
<i class="el-icon-plus" slot="reference"/>
</el-popover>