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 29b45409a2
commit f0c2d07833
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.commons.utils.JSONUtil;
import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -169,14 +169,14 @@ public class JSONSchemaGenerator {
} }
} else if (propertyObjType.equals(PropertyConstant.ARRAY)) { } else if (propertyObjType.equals(PropertyConstant.ARRAY)) {
// 先设置空值 // 先设置空值
List<Object> array = new LinkedList<>(); JSONArray array = new JSONArray();
JsonArray jsonArray = new JsonArray(); JsonArray jsonArray = new JsonArray();
if (object.has(PropertyConstant.ITEMS)) { if (object.has(PropertyConstant.ITEMS)) {
if (object.get(PropertyConstant.ITEMS).isJsonArray()) { if (object.get(PropertyConstant.ITEMS).isJsonArray()) {
jsonArray = object.get(PropertyConstant.ITEMS).getAsJsonArray(); jsonArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
} else { } else {
JsonObject itemsObject = object.get(PropertyConstant.ITEMS).getAsJsonObject(); JsonObject itemsObject = object.get(PropertyConstant.ITEMS).getAsJsonObject();
array.add(itemsObject); array.put(itemsObject);
} }
} }
for (int i = 0; i < jsonArray.size(); i++) { for (int i = 0; i < jsonArray.size(); i++) {
@ -186,34 +186,34 @@ public class JSONSchemaGenerator {
try { try {
if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.INTEGER)) { if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.INTEGER)) {
int value = jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsInt(); int value = jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsInt();
array.add(value); array.put(value);
} else { } else {
String value = ScriptEngineUtils.buildFunctionCallString(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);
} }
} catch (Exception e) { } catch (Exception e) {
String value = ScriptEngineUtils.buildFunctionCallString(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 if (jsonObject.has(PropertyConstant.ENUM)) { } 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)) { } else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.STRING)) {
if (jsonObject.has(PropertyConstant.DEFAULT)) { 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())) { } 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()); String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
array.add(value); array.put(value);
} else { } else {
array.add(StringUtils.EMPTY); array.put(StringUtils.EMPTY);
} }
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.NUMBER)) { } else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.NUMBER)) {
if (jsonObject.has(PropertyConstant.DEFAULT)) { 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())) { } 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()); String value = ScriptEngineUtils.buildFunctionCallString(jsonObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString());
array.add(value); array.put(value);
} else { } else {
array.add(0); array.put(0);
} }
} else if (jsonObject.has(PropertyConstant.PROPERTIES)) { } else if (jsonObject.has(PropertyConstant.PROPERTIES)) {
JSONObject propertyConcept = JSONUtil.createJsonObject(true); JSONObject propertyConcept = JSONUtil.createJsonObject(true);
@ -223,16 +223,16 @@ public class JSONSchemaGenerator {
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(propertyConcept, propertyKey, propertyObj); analyzeProperty(propertyConcept, propertyKey, propertyObj);
} }
array.add(propertyConcept); array.put(propertyConcept);
} else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE) instanceof JsonPrimitive) { } else if (jsonObject.has(PropertyConstant.TYPE) && jsonObject.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
JSONObject newJsonObj = JSONUtil.createJsonObject(true); JSONObject newJsonObj = JSONUtil.createJsonObject(true);
analyzeProperty(newJsonObj, propertyName + PropertyConstant.ITEM, jsonObject); 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()) { } else if (object.has(PropertyConstant.ITEMS) && object.get(PropertyConstant.ITEMS).isJsonArray()) {
JsonArray itemsObjectArray = object.get(PropertyConstant.ITEMS).getAsJsonArray(); JsonArray itemsObjectArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
array.add(itemsObjectArray); array.put(itemsObjectArray);
} }
} }
concept.put(propertyName, array); concept.put(propertyName, array);

View File

@ -4,15 +4,14 @@ package io.metersphere.api.exec.generator;
import com.google.gson.*; import com.google.gson.*;
import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.utils.EnumPropertyUtil; import io.metersphere.commons.utils.EnumPropertyUtil;
import io.metersphere.commons.utils.JSON;
import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.util.NumberUtils; import org.springframework.util.NumberUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; 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, "@")) { if (StringUtils.startsWith(evlValue, "@")) {
String str = ScriptEngineUtils.calculate(evlValue); String str = ScriptEngineUtils.calculate(evlValue);
switch (evlValue) { switch (evlValue) {
case "@integer": case "@integer":
array.add(NumberUtils.parseNumber(str, Long.class)); array.put(NumberUtils.parseNumber(str, Long.class));
break; break;
case "@boolean": case "@boolean":
array.add(Boolean.parseBoolean(str)); array.put(Boolean.parseBoolean(str));
break; break;
case "@float": case "@float":
array.add(Float.parseFloat(str)); array.put(Float.parseFloat(str));
break; break;
default: default:
array.add(str); array.put(str);
break; break;
} }
} else { } else {
String value = ScriptEngineUtils.buildFunctionCallString(evlValue); String value = ScriptEngineUtils.buildFunctionCallString(evlValue);
array.add(value); array.put(value);
} }
} }
@ -168,14 +167,14 @@ public class JSONSchemaRunTest {
concept.put(propertyName, false); concept.put(propertyName, false);
} }
} else if (propertyObjType.equals(PropertyConstant.ARRAY)) { } else if (propertyObjType.equals(PropertyConstant.ARRAY)) {
List<Object> array = new LinkedList<>(); JSONArray array = new JSONArray();
JsonArray jsonArray = new JsonArray(); JsonArray jsonArray = new JsonArray();
if (object.has(PropertyConstant.ITEMS)) { if (object.has(PropertyConstant.ITEMS)) {
if (object.get(PropertyConstant.ITEMS).isJsonArray()) { if (object.get(PropertyConstant.ITEMS).isJsonArray()) {
jsonArray = object.get(PropertyConstant.ITEMS).getAsJsonArray(); jsonArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
} else { } else {
JsonObject itemsObject = object.get(PropertyConstant.ITEMS).getAsJsonObject(); 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)) { if (StringUtils.equalsIgnoreCase(type, PropertyConstant.STRING)) {
String value = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString(); String value = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString();
array.add(value); array.put(value);
} else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.INTEGER)) { } else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.INTEGER)) {
int value = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsInt(); int value = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsInt();
array.add(value); array.put(value);
} else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.NUMBER)) { } else if (StringUtils.equalsIgnoreCase(type, PropertyConstant.NUMBER)) {
JsonElement valueObj = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK); JsonElement valueObj = itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK);
Number value = valueObj.getAsNumber(); Number value = valueObj.getAsNumber();
if (StringUtils.isNotEmpty(valueObj.getAsString()) && valueObj.getAsString().indexOf(".") != -1) { if (StringUtils.isNotEmpty(valueObj.getAsString()) && valueObj.getAsString().indexOf(".") != -1) {
array.add(value.floatValue()); array.put(value.floatValue());
} else { } else {
array.add(value.longValue()); array.put(value.longValue());
} }
} else { } else {
arrayValueOf(itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString(), array); 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); 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))) { } 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)) { } else if (itemsObject.has(PropertyConstant.TYPE) && itemsObject.get(PropertyConstant.TYPE).getAsString().equals(PropertyConstant.NUMBER)) {
if (isMock(itemsObject)) { if (isMock(itemsObject)) {
arrayValueOf(itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString(), array); arrayValueOf(itemsObject.get(PropertyConstant.MOCK).getAsJsonObject().get(PropertyConstant.MOCK).getAsString(), array);
} else { } else {
array.add(0); array.put(0);
} }
} else if (itemsObject.has(PropertyConstant.PROPERTIES)) { } else if (itemsObject.has(PropertyConstant.PROPERTIES)) {
JSONObject propertyConcept = new JSONObject(true); JSONObject propertyConcept = new JSONObject(true);
@ -224,16 +223,16 @@ public class JSONSchemaRunTest {
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject(); JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(propertyConcept, propertyKey, propertyObj, map); analyzeProperty(propertyConcept, propertyKey, propertyObj, map);
} }
array.add(propertyConcept); array.put(propertyConcept);
} else if (itemsObject.has(PropertyConstant.TYPE) && itemsObject.get(PropertyConstant.TYPE) instanceof JsonPrimitive) { } else if (itemsObject.has(PropertyConstant.TYPE) && itemsObject.get(PropertyConstant.TYPE) instanceof JsonPrimitive) {
JSONObject newJsonObj = new JSONObject(); JSONObject newJsonObj = new JSONObject();
analyzeProperty(newJsonObj, propertyName + PropertyConstant.ITEM, itemsObject, map); 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()) { } else if (object.has(PropertyConstant.ITEMS) && object.get(PropertyConstant.ITEMS).isJsonArray()) {
JsonArray itemsObjectArray = object.get(PropertyConstant.ITEMS).getAsJsonArray(); JsonArray itemsObjectArray = object.get(PropertyConstant.ITEMS).getAsJsonArray();
array.add(itemsObjectArray); array.put(itemsObjectArray);
} }
} }
concept.put(propertyName, array); concept.put(propertyName, array);

View File

@ -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(bodyObj.get("jsonSchema").asText()); String raw = JSONSchemaGenerator.getJson(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

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

View File

@ -17,11 +17,12 @@
:http-request="upload" :http-request="upload"
:beforeUpload="uploadValidate" :beforeUpload="uploadValidate"
ref="uploadLocal"> 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"/> <span slot="file"/>
</el-upload> </el-upload>
</div> </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> :disabled="isReadOnly">{{ $t('permission.project_file.associated_files') }}</el-button>
<i class="el-icon-plus" slot="reference"/> <i class="el-icon-plus" slot="reference"/>
</el-popover> </el-popover>