fix(接口测试): 修复导入Swagger数据展示错误问题

--bug=1018065 --user=赵勇 【接口测试】swagger导入接口定义,接口请求头有信息,但是接口编辑页面没显示请求头数量统计 https://www.tapd.cn/55049933/s/1262261
This commit is contained in:
fit2-zhao 2022-10-14 19:03:35 +08:00 committed by fit2-zhao
parent 68de854ca5
commit aba8a2ba42
20 changed files with 127 additions and 108 deletions

View File

@ -381,7 +381,7 @@ public class ElementUtil {
}
} else {
if (element.has(ElementConstants.HASH_TREE)) {
JSONArray elementJSONArray = element.getJSONArray(ElementConstants.HASH_TREE);
JSONArray elementJSONArray = element.optJSONArray(ElementConstants.HASH_TREE);
relationships(elementJSONArray, referenceRelationships);
}
}
@ -399,7 +399,7 @@ public class ElementUtil {
element.put(ElementConstants.CLAZZ_NAME, clazzMap.get(element.optString(PropertyConstant.TYPE)));
}
if (element.has(ElementConstants.HASH_TREE)) {
JSONArray elementJSONArray = element.getJSONArray(ElementConstants.HASH_TREE);
JSONArray elementJSONArray = element.optJSONArray(ElementConstants.HASH_TREE);
dataFormatting(elementJSONArray);
}
}
@ -411,7 +411,7 @@ public class ElementUtil {
}
formatSampler(element);
if (element != null && element.has(ElementConstants.HASH_TREE)) {
JSONArray elementJSONArray = element.getJSONArray(ElementConstants.HASH_TREE);
JSONArray elementJSONArray = element.optJSONArray(ElementConstants.HASH_TREE);
dataFormatting(elementJSONArray);
}
}
@ -477,7 +477,7 @@ public class ElementUtil {
}
}
if (element.has(ElementConstants.HASH_TREE)) {
JSONArray elementJSONArray = element.getJSONArray(ElementConstants.HASH_TREE);
JSONArray elementJSONArray = element.optJSONArray(ElementConstants.HASH_TREE);
if (isScenarioEnv) {
dataSetDomain(elementJSONArray, config);
} else {

View File

@ -190,7 +190,7 @@ public class MsScenario extends MsTestElement {
if (scenario != null && StringUtils.isNotEmpty(scenario.getScenarioDefinition())) {
JSONObject element = JSONUtil.parseObject(scenario.getScenarioDefinition());
// 历史数据处理
ElementUtil.dataFormatting(element.getJSONArray(ElementConstants.HASH_TREE));
ElementUtil.dataFormatting(element.optJSONArray(ElementConstants.HASH_TREE));
this.setName(scenario.getName());
this.setProjectId(scenario.getProjectId());
LinkedList<MsTestElement> sourceHashTree = mapper.readValue(element.optString(ElementConstants.HASH_TREE), new TypeReference<LinkedList<MsTestElement>>() {

View File

@ -43,7 +43,7 @@ public class ApiEnvironmentRunningParamService {
if (configObj.has("commonConfig")) {
JSONObject commonConfig = configObj.optJSONObject("commonConfig");
if (commonConfig.has("variables")) {
JSONArray variables = commonConfig.getJSONArray("variables");
JSONArray variables = commonConfig.optJSONArray("variables");
List<JSONObject> variableList = new LinkedList<>();
for (Map.Entry<String, String> entry : varMap.entrySet()) {
String key = entry.getKey();

View File

@ -1,9 +1,9 @@
package io.metersphere.api.parse;
import io.metersphere.api.parse.api.ms.NodeTree;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.parse.api.ms.NodeTree;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -19,19 +19,21 @@ public abstract class MsAbstractParser<T> extends ApiImportAbstractParser<T> {
protected List<MsHTTPSamplerProxy> parseMsHTTPSamplerProxy(JSONObject testObject, String tag, boolean isSetUrl) {
JSONObject requests = testObject.optJSONObject(tag);
List<MsHTTPSamplerProxy> msHTTPSamplerProxies = new ArrayList<>();
requests.keySet().forEach(requestName -> {
JSONObject requestObject = requests.optJSONObject(requestName);
String path = requestObject.optString("url");
String method = requestObject.optString("method");
MsHTTPSamplerProxy request = buildRequest(requestName, path, method);
parseBody(requestObject, request.getBody());
parseHeader(requestObject, request.getHeaders());
parsePath(request);
if (isSetUrl) {
request.setUrl(path);
}
msHTTPSamplerProxies.add(request);
});
if (requests != null) {
requests.keySet().forEach(requestName -> {
JSONObject requestObject = requests.optJSONObject(requestName);
String path = requestObject.optString("url");
String method = requestObject.optString("method");
MsHTTPSamplerProxy request = buildRequest(requestName, path, method);
parseBody(requestObject, request.getBody());
parseHeader(requestObject, request.getHeaders());
parsePath(request);
if (isSetUrl) {
request.setUrl(path);
}
msHTTPSamplerProxies.add(request);
});
}
return msHTTPSamplerProxies;
}
@ -64,7 +66,7 @@ public abstract class MsAbstractParser<T> extends ApiImportAbstractParser<T> {
}
private void parseHeader(JSONObject requestObject, List<KeyValue> msHeaders) {
JSONArray headers = requestObject.getJSONArray("headers");
JSONArray headers = requestObject.optJSONArray("headers");
if (headers != null) {
for (int i = 0; i < headers.length(); i++) {
JSONObject header = headers.optJSONObject(i);
@ -77,7 +79,7 @@ public abstract class MsAbstractParser<T> extends ApiImportAbstractParser<T> {
if (requestObject.has("body")) {
Object body = requestObject.get("body");
if (body instanceof JSONArray) {
JSONArray bodies = requestObject.getJSONArray("body");
JSONArray bodies = requestObject.optJSONArray("body");
if (bodies != null) {
StringBuilder bodyStr = new StringBuilder();
for (int i = 0; i < bodies.length(); i++) {

View File

@ -50,18 +50,20 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
protected List<ApiDefinitionWithBLOBs> parsePluginFormat(JSONObject testObject, ApiTestImportRequest importRequest, Boolean isCreateModule) {
List<ApiDefinitionWithBLOBs> results = new ArrayList<>();
testObject.keySet().forEach(tag -> {
if (testObject != null) {
testObject.keySet().forEach(tag -> {
List<MsHTTPSamplerProxy> msHTTPSamplerProxies = parseMsHTTPSamplerProxy(testObject, tag, false);
for (MsHTTPSamplerProxy msHTTPSamplerProxy : msHTTPSamplerProxies) {
ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(msHTTPSamplerProxy.getId(), msHTTPSamplerProxy.getName(), msHTTPSamplerProxy.getPath(), msHTTPSamplerProxy.getMethod(), importRequest);
apiDefinition.setProjectId(this.projectId);
apiDefinition.setModulePath(tag);
apiDefinition.setRequest(JSON.toJSONString(msHTTPSamplerProxy));
apiDefinition.setName(apiDefinition.getPath() + " [" + apiDefinition.getMethod() + "]");
results.add(apiDefinition);
}
});
List<MsHTTPSamplerProxy> msHTTPSamplerProxies = parseMsHTTPSamplerProxy(testObject, tag, false);
for (MsHTTPSamplerProxy msHTTPSamplerProxy : msHTTPSamplerProxies) {
ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(msHTTPSamplerProxy.getId(), msHTTPSamplerProxy.getName(), msHTTPSamplerProxy.getPath(), msHTTPSamplerProxy.getMethod(), importRequest);
apiDefinition.setProjectId(this.projectId);
apiDefinition.setModulePath(tag);
apiDefinition.setRequest(JSON.toJSONString(msHTTPSamplerProxy));
apiDefinition.setName(apiDefinition.getPath() + " [" + apiDefinition.getMethod() + "]");
results.add(apiDefinition);
}
});
}
return results;
}

View File

@ -695,7 +695,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
}};
Set<String> typeKeys = typeMap.keySet();
for (String type : typeKeys) {
JSONArray params = request.getJSONArray(type); // 获得请求参数列表
JSONArray params = request.optJSONArray(type); // 获得请求参数列表
if (params != null) {
for (int i = 0; i < params.length(); ++i) {
JSONObject param = params.optJSONObject(i); // 对于每个参数:
@ -942,7 +942,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
JSONObject statusCodeInfo = new JSONObject();
// build 请求头
JSONObject headers = new JSONObject();
JSONArray headValueList = response.getJSONArray("headers");
JSONArray headValueList = response.optJSONArray("headers");
if (headValueList != null) {
for (Object item : headValueList) {
if (item instanceof JSONObject && ((JSONObject) item).optString("name") != null) {
@ -960,7 +960,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
statusCodeInfo.put("content", buildContent(response));
statusCodeInfo.put("description", "");
// 返回code
JSONArray statusCode = response.getJSONArray("statusCode");
JSONArray statusCode = response.optJSONArray("statusCode");
responseBody.put(statusCode.toString(), statusCodeInfo);
return responseBody;
}
@ -997,19 +997,19 @@ public class Swagger3Parser extends SwaggerAbstractParser {
JSONObject jsonObject = JSONUtil.parseObject(jsonSchema);
JSONArray required = new JSONArray();
if (jsonObject != null) {
required = jsonObject.getJSONArray(PropertyConstant.REQUIRED);
required = jsonObject.optJSONArray(PropertyConstant.REQUIRED);
}
if (required == null) {
JSONObject items = jsonObject.optJSONObject(PropertyConstant.ITEMS);
if (items != null) {
required = items.getJSONArray(PropertyConstant.REQUIRED);
required = items.optJSONArray(PropertyConstant.REQUIRED);
}
}
bodyInfo = buildJsonSchema(jsonObject, required);
}
} else {
try { // 若请求体是一个 object
bodyInfo = buildRequestBodyJsonInfo(body.getJSONArray("raw"));
bodyInfo = buildRequestBodyJsonInfo(body.optJSONArray("raw"));
} catch (Exception e) {
bodyInfo = buildRequestBodyJsonInfo(body.optJSONObject("raw"));
}
@ -1032,7 +1032,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
JSONObject xmlToJson = XMLUtil.XmlToJson(xmlText);
bodyInfo = buildRequestBodyJsonInfo(xmlToJson);
} else if (bodyType != null && (bodyType.equalsIgnoreCase("WWW_FORM") || bodyType.equalsIgnoreCase("Form Data") || bodyType.equalsIgnoreCase("BINARY"))) { // key-value 类格式
JSONObject formData = getformDataProperties(body.getJSONArray("kvs"));
JSONObject formData = getformDataProperties(body.optJSONArray("kvs"));
bodyInfo = buildFormDataSchema(formData);
}
}

View File

@ -296,8 +296,8 @@ public class ApiScenarioImportUtil {
object.put("index", i + 1);
object.put("resourceId", UUID.randomUUID().toString());
hashTree.put(i, object);
if (object.has(ElementConstants.HASH_TREE) && object.getJSONArray(ElementConstants.HASH_TREE) != null) {
formatHashTree(object.getJSONArray(ElementConstants.HASH_TREE));
if (object.has(ElementConstants.HASH_TREE) && object.optJSONArray(ElementConstants.HASH_TREE) != null) {
formatHashTree(object.optJSONArray(ElementConstants.HASH_TREE));
}
}
}

View File

@ -5,6 +5,7 @@ import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.sampler.MsJDBCSampler;
import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.log.utils.ApiDefinitionDiffUtil;
import io.metersphere.log.utils.ReflexObjectUtil;
import io.metersphere.log.utils.diff.json.JacksonDiff;
@ -94,21 +95,21 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
Map<String, String> diffMap = new LinkedHashMap<>();
diffMap.put(TYPE, bloBsIsNew.getString(TYPE));
JsonDiff jsonDiff = new JacksonDiff();
if (bloBsIsNew.getString(TYPE).equals("TCPSampler")) {
MsTCPSampler tcpSamplerNew = JSONUtil.parseObject(bloBsIsNew.toString(), MsTCPSampler.class);
MsTCPSampler tcpSamplerOld = JSONUtil.parseObject(bloBsIsOld.toString(), MsTCPSampler.class);
if (bloBsIsNew.getString(TYPE).equals(ElementConstants.TCP_SAMPLER)) {
MsTCPSampler tcpSamplerNew = JSON.parseObject(bloBsIsNew.toString(), MsTCPSampler.class);
MsTCPSampler tcpSamplerOld = JSON.parseObject(bloBsIsOld.toString(), MsTCPSampler.class);
diffTcp(tcpSamplerNew, tcpSamplerOld, jsonDiff, diffMap);
} else if (bloBsIsNew.getString(TYPE).equals("HTTPSamplerProxy")) {
MsHTTPSamplerProxy httpSamplerProxyNew = JSONUtil.parseObject(bloBsIsNew.toString(), MsHTTPSamplerProxy.class);
MsHTTPSamplerProxy httpSamplerProxyOld = JSONUtil.parseObject(bloBsIsOld.toString(), MsHTTPSamplerProxy.class);
} else if (bloBsIsNew.getString(TYPE).equals(ElementConstants.HTTP_SAMPLER)) {
MsHTTPSamplerProxy httpSamplerProxyNew = JSON.parseObject(bloBsIsNew.toString(), MsHTTPSamplerProxy.class);
MsHTTPSamplerProxy httpSamplerProxyOld = JSON.parseObject(bloBsIsOld.toString(), MsHTTPSamplerProxy.class);
diffHttp(httpSamplerProxyNew, httpSamplerProxyOld, jsonDiff, diffMap);
} else if (bloBsIsNew.getString(TYPE).equals("JDBCSampler")) {
MsJDBCSampler jdbcSamplerNew = JSONUtil.parseObject(bloBsIsNew.toString(), MsJDBCSampler.class);
MsJDBCSampler jdbcSamplerOld = JSONUtil.parseObject(bloBsIsOld.toString(), MsJDBCSampler.class);
} else if (bloBsIsNew.getString(TYPE).equals(ElementConstants.JDBC_SAMPLER)) {
MsJDBCSampler jdbcSamplerNew = JSON.parseObject(bloBsIsNew.toString(), MsJDBCSampler.class);
MsJDBCSampler jdbcSamplerOld = JSON.parseObject(bloBsIsOld.toString(), MsJDBCSampler.class);
diffJdbc(jdbcSamplerNew, jdbcSamplerOld, jsonDiff, diffMap);
} else {
MsDubboSampler dubboSamplerNew = JSONUtil.parseObject(bloBsIsNew.toString(), MsDubboSampler.class);
MsDubboSampler dubboSamplerOld = JSONUtil.parseObject(bloBsIsOld.toString(), MsDubboSampler.class);
MsDubboSampler dubboSamplerNew = JSON.parseObject(bloBsIsNew.toString(), MsDubboSampler.class);
MsDubboSampler dubboSamplerOld = JSON.parseObject(bloBsIsOld.toString(), MsDubboSampler.class);
diffDubbo(dubboSamplerNew, dubboSamplerOld, jsonDiff, diffMap);
}
if (diffMap.size() > 1) {

View File

@ -55,7 +55,7 @@ public class GenerateHashTreeUtil {
ElementUtil.dataFormatting(element);
// 多态JSON普通转换会丢失内容需要通过 ObjectMapper 获取
if (element != null && element.has(ElementConstants.HASH_TREE)) {
LinkedList<MsTestElement> elements = mapper.readValue(element.getJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference<LinkedList<MsTestElement>>() {
LinkedList<MsTestElement> elements = mapper.readValue(element.optJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference<LinkedList<MsTestElement>>() {
});
scenario.setHashTree(elements);
}
@ -76,7 +76,7 @@ public class GenerateHashTreeUtil {
try {
if (element != null && element.has(ElementConstants.HASH_TREE)) {
ElementUtil.dataFormatting(element);
return objectMapper.readValue(element.getJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference<LinkedList<MsTestElement>>() {
return objectMapper.readValue(element.optJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference<LinkedList<MsTestElement>>() {
});
}
} catch (JsonProcessingException e) {

View File

@ -52,7 +52,7 @@ public class HashTreeUtil {
JSONObject commonConfig = configJson.optJSONObject(COMMON_CONFIG);
if (commonConfig.has(VARIABLES)) {
Map<String, String> envHeadMap = new HashMap<>();
JSONArray variablesArr = commonConfig.getJSONArray(VARIABLES);
JSONArray variablesArr = commonConfig.optJSONArray(VARIABLES);
for (int i = 0; i < variablesArr.length(); i++) {
JSONObject object = variablesArr.optJSONObject(i);
if (object.has(NAME) && object.has(VALUE)) {

View File

@ -47,7 +47,7 @@ public class XMLUtil {
jsonToXmlStr(jo, buffer, nowTab.append("\t"));
buffer.append(tab).append("</").append(en.getKey()).append(">\n");
} else if (en.getValue() instanceof JSONArray) {
JSONArray array = jObj.getJSONArray(en.getKey());
JSONArray array = jObj.optJSONArray(en.getKey());
for (int i = 0; i < array.length(); i++) {
buffer.append(tab).append("<").append(en.getKey()).append(">\n");
if (StringUtils.isNotBlank(array.optString(i))) {

View File

@ -116,7 +116,7 @@ public class MockApiUtils {
} else if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM")) {
if (bodyObj.has("kvs")) {
JSONObject bodyParamArr = new JSONObject();
JSONArray kvsArr = bodyObj.getJSONArray("kvs");
JSONArray kvsArr = bodyObj.optJSONArray("kvs");
for (int i = 0; i < kvsArr.length(); i++) {
JSONObject kv = kvsArr.optJSONObject(i);
if (kv.has("name")) {
@ -200,7 +200,7 @@ public class MockApiUtils {
} else if (StringUtils.equalsAny(type, "Form Data", "WWW_FORM")) {
Map<String, String> paramMap = new LinkedHashMap<>();
if (bodyObj.has("kvs")) {
JSONArray kvsArr = bodyObj.getJSONArray("kvs");
JSONArray kvsArr = bodyObj.optJSONArray("kvs");
for (int i = 0; i < kvsArr.length(); i++) {
JSONObject kv = kvsArr.optJSONObject(i);
if (kv.has("name")) {
@ -223,7 +223,7 @@ public class MockApiUtils {
responseDTO.setReturnData(returnStr);
}
if (respObj.has("statusCode")) {
JSONArray statusCodeArray = respObj.getJSONArray("statusCode");
JSONArray statusCodeArray = respObj.optJSONArray("statusCode");
int code = 200;
if (statusCodeArray != null) {
for (int i = 0; i < statusCodeArray.length(); i++) {
@ -241,7 +241,7 @@ public class MockApiUtils {
responseDTO.setReturnCode(code);
}
if (respObj.has("headers")) {
JSONArray jsonArray = respObj.getJSONArray("headers");
JSONArray jsonArray = respObj.optJSONArray("headers");
Map<String, String> headMap = new HashMap<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject headObj = jsonArray.optJSONObject(i);

View File

@ -335,7 +335,7 @@ public class MockConfigService {
JSONObject expectParamsObj = mockExpectRequestObj.optJSONObject("params");
if (expectParamsObj.has("headers")) {
//检测headers
JSONArray headerArr = expectParamsObj.getJSONArray("headers");
JSONArray headerArr = expectParamsObj.optJSONArray("headers");
for (int i = 0; i < headerArr.length(); i++) {
JSONObject jsonObject = headerArr.optJSONObject(i);
if (jsonObject.has("name") && jsonObject.has("value")) {
@ -359,7 +359,7 @@ public class MockConfigService {
paramsFilterType = expectBodyObject.optString("paramsFilterType");
}
if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM") && expectBodyObject.has("kvs")) {
JSONArray kvsArr = expectBodyObject.getJSONArray("kvs");
JSONArray kvsArr = expectBodyObject.optJSONArray("kvs");
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(kvsArr);
if (CollectionUtils.isNotEmpty(mockConfigRequestParams)) {
if (!MockApiUtils.checkParamsCompliance(jsonArray, mockConfigRequestParams, StringUtils.equals(paramsFilterType, "And"))) {
@ -386,7 +386,7 @@ public class MockConfigService {
}
if (expectParamsObj.has("arguments")) {
JSONArray argumentsArray = expectParamsObj.getJSONArray("arguments");
JSONArray argumentsArray = expectParamsObj.optJSONArray("arguments");
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(argumentsArray);
if (!MockApiUtils.checkParamsCompliance(requestMockParams.getQueryParamsObj(), mockConfigRequestParams, StringUtils.equals(paramsFilterType, "And"))) {
return false;
@ -398,7 +398,7 @@ public class MockConfigService {
restFilterType = expectParamsObj.optString("restFilterType");
}
if (expectParamsObj.has("rest")) {
JSONArray restArray = expectParamsObj.getJSONArray("rest");
JSONArray restArray = expectParamsObj.optJSONArray("rest");
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(restArray);
if (!MockApiUtils.checkParamsCompliance(requestMockParams.getRestParamsObj(), mockConfigRequestParams, StringUtils.equals(restFilterType, "And"))) {
return false;
@ -424,7 +424,7 @@ public class MockConfigService {
mockExpectJson = mockExpectJsonItem;
}
} else {
JSONArray jsonArray = mockExpectRequestObj.getJSONArray("variables");
JSONArray jsonArray = mockExpectRequestObj.optJSONArray("variables");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.optJSONObject(i);
String name = "";
@ -470,14 +470,14 @@ public class MockConfigService {
if (requestObj.has("params")) {
JSONObject paramsObj = requestObj.optJSONObject("params");
if (paramsObj.has("headers")) {
JSONArray headArray = paramsObj.getJSONArray("headers");
JSONArray headArray = paramsObj.optJSONArray("headers");
boolean isHeadMatch = MockApiUtils.matchRequestHeader(headArray, requestHeaderMap);
if (!isHeadMatch) {
return null;
}
//判断rest为空
if (paramsObj.has("rest")) {
JSONArray restArray = paramsObj.getJSONArray("rest");
JSONArray restArray = paramsObj.optJSONArray("rest");
for (int i = 0; i < restArray.length(); i++) {
JSONObject restObj = restArray.optJSONObject(i);
if (restObj.has("name") && restObj.has("value")) {
@ -487,7 +487,7 @@ public class MockConfigService {
}
//判断arguments为空
if (paramsObj.has("arguments")) {
JSONArray argumentsArray = paramsObj.getJSONArray("arguments");
JSONArray argumentsArray = paramsObj.optJSONArray("arguments");
for (int i = 0; i < argumentsArray.length(); i++) {
JSONObject argumentsObj = argumentsArray.optJSONObject(i);
if (argumentsObj.has("name") && argumentsObj.has("value")) {
@ -513,7 +513,7 @@ public class MockConfigService {
}
} else if (StringUtils.equalsAnyIgnoreCase(type, "KeyValue", "Form Data", "WWW_FORM")) {
if (bodyObj.has("kvs")) {
JSONArray kvsArray = bodyObj.getJSONArray("kvs");
JSONArray kvsArray = bodyObj.optJSONArray("kvs");
for (int i = 0; i < kvsArray.length(); i++) {
JSONObject kvsObj = kvsArray.optJSONObject(i);
if (kvsObj.has("name") && kvsObj.has("value")) {
@ -547,7 +547,7 @@ public class MockConfigService {
}
} else {
JSONObject mockExpectJson = new JSONObject();
JSONArray jsonArray = requestObj.getJSONArray("variables");
JSONArray jsonArray = requestObj.optJSONArray("variables");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.optJSONObject(i);
String name = "";
@ -583,7 +583,7 @@ public class MockConfigService {
if (responseObj.has("responseResult")) {
JSONObject responseJsonObj = responseObj.optJSONObject("responseResult");
if (responseJsonObj.has("headers")) {
JSONArray jsonArray = responseJsonObj.getJSONArray("headers");
JSONArray jsonArray = responseJsonObj.optJSONArray("headers");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.optJSONObject(i);
if (object.has("name") && object.has("value")) {
@ -618,7 +618,7 @@ public class MockConfigService {
}
}
} else {
JSONArray jsonArray = responseObj.getJSONArray("httpHeads");
JSONArray jsonArray = responseObj.optJSONArray("httpHeads");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.optJSONObject(i);
String name = null;
@ -746,7 +746,7 @@ public class MockConfigService {
//url参数赋值
if (requestObj.has("arguments")) {
try {
JSONArray headArr = requestObj.getJSONArray("arguments");
JSONArray headArr = requestObj.optJSONArray("arguments");
for (int index = 0; index < headArr.length(); index++) {
JSONObject headObj = headArr.optJSONObject(index);
@ -759,7 +759,7 @@ public class MockConfigService {
}
if (requestObj.has("rest")) {
try {
JSONArray headArr = requestObj.getJSONArray("rest");
JSONArray headArr = requestObj.optJSONArray("rest");
for (int index = 0; index < headArr.length(); index++) {
JSONObject headObj = headArr.optJSONObject(index);
if (headObj.has("name") && !restParamList.contains(headObj.has("name"))) {
@ -778,7 +778,7 @@ public class MockConfigService {
if (StringUtils.equalsAny(type, "Form Data", "WWW_FORM")) {
if (bodyObj.has("kvs")) {
JSONArray kvsArr = bodyObj.getJSONArray("kvs");
JSONArray kvsArr = bodyObj.optJSONArray("kvs");
for (int i = 0; i < kvsArr.length(); i++) {
JSONObject kv = kvsArr.optJSONObject(i);
if (kv.has("name") && !formDataList.contains(kv.has("name"))) {

View File

@ -100,8 +100,8 @@ public class MsHashTreeService {
}
}
}
if (object != null && object.getJSONArray(HASH_TREE) != null) {
setHashTree(object.getJSONArray(HASH_TREE));
if (object != null && object.optJSONArray(HASH_TREE) != null) {
setHashTree(object.optJSONArray(HASH_TREE));
}
}
}
@ -122,7 +122,7 @@ public class MsHashTreeService {
if (StringUtils.equalsIgnoreCase(element.optString(REFERENCED), REF)) {
JSONObject refElement = JSONUtil.parseObject(apiTestCase.getRequest());
ElementUtil.dataFormatting(refElement);
JSONArray array = refElement.getJSONArray(HASH_TREE);
JSONArray array = refElement.optJSONArray(HASH_TREE);
ElementUtil.copyBean(element, refElement);
element.put(HEADERS, refElement.opt(HEADERS));
element.put(REST, refElement.opt(REST));
@ -133,9 +133,9 @@ public class MsHashTreeService {
element.put(ARGUMENTS, refElement.opt(ARGUMENTS));
element.put(PROJECT_ID, apiTestCase.getProjectId());
if (array != null) {
JSONArray sourceHashTree = element.getJSONArray(HASH_TREE);
JSONArray sourceHashTree = element.optJSONArray(HASH_TREE);
Map<String, List<JSONObject>> groupMap = ElementUtil.group(sourceHashTree);
Map<String, List<JSONObject>> targetGroupMap = ElementUtil.group(refElement.getJSONArray(HASH_TREE));
Map<String, List<JSONObject>> targetGroupMap = ElementUtil.group(refElement.optJSONArray(HASH_TREE));
List<JSONObject> pre = ElementUtil.mergeHashTree(groupMap.get("PRE"), targetGroupMap.get("PRE"));
List<JSONObject> post = ElementUtil.mergeHashTree(groupMap.get("POST"), targetGroupMap.get("POST"));
@ -223,7 +223,7 @@ public class MsHashTreeService {
hashTree.put(i, element);
}
if (element.has(HASH_TREE)) {
JSONArray elementJSONArray = element.getJSONArray(HASH_TREE);
JSONArray elementJSONArray = element.optJSONArray(HASH_TREE);
dataFormatting(elementJSONArray);
}
}
@ -236,7 +236,7 @@ public class MsHashTreeService {
element = this.setRequest(element);
}
if (element != null && element.has(HASH_TREE)) {
JSONArray elementJSONArray = element.getJSONArray(HASH_TREE);
JSONArray elementJSONArray = element.optJSONArray(HASH_TREE);
dataFormatting(elementJSONArray);
}
}

View File

@ -111,7 +111,7 @@ public class ApiScenarioReferenceIdService {
if (!jsonObject.has(MsHashTreeService.HASH_TREE)) {
return returnList;
}
JSONArray hashTree = jsonObject.getJSONArray(MsHashTreeService.HASH_TREE);
JSONArray hashTree = jsonObject.optJSONArray(MsHashTreeService.HASH_TREE);
for (int index = 0; index < hashTree.length(); index++) {
JSONObject item = hashTree.optJSONObject(index);
if (item == null) {
@ -140,7 +140,7 @@ public class ApiScenarioReferenceIdService {
returnList.add(saveItem);
}
if (item.has(MsHashTreeService.HASH_TREE)) {
returnList.addAll(this.deepElementRelation(scenario.getId(), item.getJSONArray(MsHashTreeService.HASH_TREE)));
returnList.addAll(this.deepElementRelation(scenario.getId(), item.optJSONArray(MsHashTreeService.HASH_TREE)));
}
}
}
@ -191,7 +191,7 @@ public class ApiScenarioReferenceIdService {
deepRelations.add(saveItem);
}
if (item.has(MsHashTreeService.HASH_TREE)) {
deepRelations.addAll(this.deepElementRelation(scenarioId, item.getJSONArray(MsHashTreeService.HASH_TREE)));
deepRelations.addAll(this.deepElementRelation(scenarioId, item.optJSONArray(MsHashTreeService.HASH_TREE)));
}
}
}

View File

@ -185,7 +185,7 @@ public class ApiScenarioReportStructureService {
StepTreeDTO dto = new StepTreeDTO(name, resourceId, element.optString(TYPE), resourceId, 1);
dto.setAllIndex(null);
if (element.has(HASH_TREE) && !REQUESTS.contains(dto.getType())) {
JSONArray elementJSONArray = element.getJSONArray(HASH_TREE);
JSONArray elementJSONArray = element.optJSONArray(HASH_TREE);
dataFormatting(elementJSONArray, dto, id, reportType);
}
return dto;
@ -221,7 +221,7 @@ public class ApiScenarioReportStructureService {
}
dto.getChildren().add(children);
if (element.has(HASH_TREE) && !REQUESTS.contains(children.getType())) {
JSONArray elementJSONArray = element.getJSONArray(HASH_TREE);
JSONArray elementJSONArray = element.optJSONArray(HASH_TREE);
dataFormatting(elementJSONArray, children, id, reportType);
}
}

View File

@ -406,7 +406,7 @@ public class ApiScenarioService {
return;
}
JSONObject element = JSONUtil.parseObject(scenario.getScenarioDefinition());
JSONArray hashTree = element.getJSONArray(ElementConstants.HASH_TREE);
JSONArray hashTree = element.optJSONArray(ElementConstants.HASH_TREE);
ApiScenarioImportUtil.formatHashTree(hashTree);
setReferenced(hashTree, scenario.getVersionId(), scenario.getProjectId(), apiTestCaseMapper, apiDefinitionMapper, true);
scenario.setScenarioDefinition(element.toString());
@ -731,8 +731,8 @@ public class ApiScenarioService {
for (int i = 0; i < valueArray.length(); i++) {
try {
JSONObject obj = (JSONObject) valueArray.get(i);
JSONObject targetValue = jsonMerge(obj, (JSONObject) target.getJSONArray(key).get(i));
target.getJSONArray(key).put(i, targetValue);
JSONObject targetValue = jsonMerge(obj, (JSONObject) target.optJSONArray(key).get(i));
target.optJSONArray(key).put(i, targetValue);
} catch (Exception e) {
LogUtil.error(e);
}
@ -783,7 +783,7 @@ public class ApiScenarioService {
ParameterConfig config = new ParameterConfig();
apiScenarioEnvService.setEnvConfig(environmentMap, config);
if (config.getConfig() != null && !config.getConfig().isEmpty()) {
ElementUtil.dataSetDomain(element.getJSONArray(ElementConstants.HASH_TREE), config);
ElementUtil.dataSetDomain(element.optJSONArray(ElementConstants.HASH_TREE), config);
}
return element.toString();
} catch (Exception e) {
@ -1490,7 +1490,7 @@ public class ApiScenarioService {
}
JSONObject element = JSONUtil.parseObject(scenario.getScenarioDefinition());
if (element != null) {
JSONArray hashTree = element.getJSONArray(ElementConstants.HASH_TREE);
JSONArray hashTree = element.optJSONArray(ElementConstants.HASH_TREE);
ApiScenarioImportUtil.formatHashTree(hashTree);
setHashTree(hashTree);
scenario.setScenarioDefinition(element.toString());
@ -1511,19 +1511,19 @@ public class ApiScenarioService {
String refType = object.optString("refType");
if (StringUtils.isNotEmpty(refType)) {
if (refType.equals("CASE")) {
if (object.getJSONArray(ElementConstants.HASH_TREE) == null || object.getJSONArray(ElementConstants.HASH_TREE).length() == 0) {
if (object.optJSONArray(ElementConstants.HASH_TREE) == null || object.optJSONArray(ElementConstants.HASH_TREE).length() == 0) {
ApiTestCaseInfo model = extApiTestCaseMapper.selectApiCaseInfoByPrimaryKey(object.optString("id"));
if (model != null) {
JSONObject element = JSONUtil.parseObject(model.getRequest());
object.put(ElementConstants.HASH_TREE, element.getJSONArray(ElementConstants.HASH_TREE));
object.put(ElementConstants.HASH_TREE, element.optJSONArray(ElementConstants.HASH_TREE));
}
}
}
}
}
if (StringUtils.isNotEmpty(object.optString("refType"))) {
if (object.getJSONArray(ElementConstants.HASH_TREE) != null) {
setHashTree(object.getJSONArray(ElementConstants.HASH_TREE));
if (object.optJSONArray(ElementConstants.HASH_TREE) != null) {
setHashTree(object.optJSONArray(ElementConstants.HASH_TREE));
}
}
@ -2099,12 +2099,12 @@ public class ApiScenarioService {
object.put("environmentMap", new HashMap<>());
}
if (StringUtils.isNotEmpty(object.optString("refType")) && object.optString("refType").equals("CASE")) {
if (object.has(ElementConstants.HASH_TREE) && object.getJSONArray(ElementConstants.HASH_TREE) != null) {
setReferenced(object.getJSONArray(ElementConstants.HASH_TREE), versionId, projectId, apiTestCaseMapper, apiDefinitionMapper, true);
if (object.has(ElementConstants.HASH_TREE) && object.optJSONArray(ElementConstants.HASH_TREE) != null) {
setReferenced(object.optJSONArray(ElementConstants.HASH_TREE), versionId, projectId, apiTestCaseMapper, apiDefinitionMapper, true);
}
} else {
if (object.has(ElementConstants.HASH_TREE) && object.getJSONArray(ElementConstants.HASH_TREE) != null) {
setReferenced(object.getJSONArray(ElementConstants.HASH_TREE), versionId, projectId, apiTestCaseMapper, apiDefinitionMapper, false);
if (object.has(ElementConstants.HASH_TREE) && object.optJSONArray(ElementConstants.HASH_TREE) != null) {
setReferenced(object.optJSONArray(ElementConstants.HASH_TREE), versionId, projectId, apiTestCaseMapper, apiDefinitionMapper, false);
}
}

View File

@ -100,12 +100,9 @@ export default {
this.$emit('runRefresh', {});
let url = '/api/automation/run/debug';
if (this.runData.type === 'UiScenario') {
url = '/ui/automation/run/debug';
}
saveScenario(url, reqObj, this.runData.hashTree, this, (response) => {
if (response.data !== "SUCCESS") {
this.$error(response.message ? response.message : this.$t('commons.run_fail'));
this.$error(response.data ? response.data : this.$t('commons.run_fail'));
this.$emit('errorRefresh');
}
});

View File

@ -273,7 +273,7 @@ import {
initCondition
} from "metersphere-frontend/src/utils/tableUtils";
import HeaderLabelOperate from "metersphere-frontend/src/components/head/HeaderLabelOperate";
import {Body} from "@/business/definition/model/ApiTestModel";
import {Body, KeyValue} from "@/business/definition/model/ApiTestModel";
import {getGraphByCondition} from "@/api/graph";
import ListItemDeleteConfirm from "metersphere-frontend/src/components/ListItemDeleteConfirm";
import MsSearch from "metersphere-frontend/src/components/search/MsSearch";
@ -718,6 +718,11 @@ export default {
}
if (!item.request.headers) {
item.request.headers = [];
} else if (item.request.headers.length === 1) {
let values = item.request.headers.filter(tab => tab.name !== '');
if (values.length > 0) {
item.request.headers.push(new KeyValue({enable: true}))
}
}
if (!item.request.body.kvs) {
item.request.body.kvs = [];
@ -727,8 +732,20 @@ export default {
i.files = []
}
})
if (!item.request.rest) {
item.request.rest = [];
} else if (item.request.rest.length === 1) {
let values = item.request.rest.filter(tab => tab.name !== '');
if (values.length > 0) {
item.request.rest.push(new KeyValue({enable: true}))
}
}
if (item.request.query && item.request.query.length === 1) {
let values = item.request.query.filter(tab => tab.name !== '');
if (values.length > 0) {
item.request.query.push(new KeyValue({enable: true}))
}
}
if (!item.request.arguments) {
item.request.arguments = [{

View File

@ -34,7 +34,7 @@
<el-tooltip class="item-tabs" effect="dark" :content="$t('api_test.definition.request.query_info')"
placement="top-start" slot="label">
<span>{{ $t('api_test.definition.request.query_param') }}
<div class="el-step__icon is-text ms-api-col ms-header" v-if="request.arguments.length>1">
<div class="el-step__icon is-text ms-api-col ms-header" v-if="request.arguments.length > 1">
<div class="el-step__icon-inner">{{ request.arguments.length - 1 }}</div>
</div>
</span>
@ -61,7 +61,7 @@
placement="top-start" slot="label">
<span>
{{ $t('api_test.definition.request.rest_param') }}
<div class="el-step__icon is-text ms-api-col ms-header" v-if="request.rest.length>1">
<div class="el-step__icon is-text ms-api-col ms-header" v-if="request.rest.length > 1">
<div class="el-step__icon-inner">{{ request.rest.length - 1 }}</div>
</div>
</span>