From f0c2d0783377c8e6a5782e2c41f476909a25bc1a Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Wed, 9 Nov 2022 16:12:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Djsonschema=E5=B1=95=E7=A4=BA=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1019375 --user=王孝刚 【接口测试】接口定义,查看接口文档页面没有反应,接口和日志有报错 https://www.tapd.cn/55049933/s/1293605 --- .../exec/generator/JSONSchemaGenerator.java | 32 ++++++++-------- .../api/exec/generator/JSONSchemaRunTest.java | 37 +++++++++---------- .../metersphere/service/ShareInfoService.java | 2 +- .../scenario/component/ApiComponent.vue | 2 +- .../components/body/ApiBodyFileUpload.vue | 5 ++- 5 files changed, 39 insertions(+), 39 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java index ee967bcd34..80a9c14afa 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaGenerator.java @@ -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 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); diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java index ffb3af4a4b..ddaf7fd738 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/generator/JSONSchemaRunTest.java @@ -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 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 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); diff --git a/api-test/backend/src/main/java/io/metersphere/service/ShareInfoService.java b/api-test/backend/src/main/java/io/metersphere/service/ShareInfoService.java index ec9a9089c7..949f08c8a1 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/ShareInfoService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/ShareInfoService.java @@ -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")) { diff --git a/api-test/frontend/src/business/automation/scenario/component/ApiComponent.vue b/api-test/frontend/src/business/automation/scenario/component/ApiComponent.vue index 7963f00e38..3acc12a9e3 100644 --- a/api-test/frontend/src/business/automation/scenario/component/ApiComponent.vue +++ b/api-test/frontend/src/business/automation/scenario/component/ApiComponent.vue @@ -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 { diff --git a/api-test/frontend/src/business/definition/components/body/ApiBodyFileUpload.vue b/api-test/frontend/src/business/definition/components/body/ApiBodyFileUpload.vue index 4109de424a..bc167404c0 100644 --- a/api-test/frontend/src/business/definition/components/body/ApiBodyFileUpload.vue +++ b/api-test/frontend/src/business/definition/components/body/ApiBodyFileUpload.vue @@ -17,11 +17,12 @@ :http-request="upload" :beforeUpload="uploadValidate" ref="uploadLocal"> - {{ $t('permission.project_file.local_upload') }} + {{ $t('permission.project_file.local_upload') }} - {{ $t('permission.project_file.associated_files') }}