fix(接口定义): 无法使用jsonpath推荐断言 (#17754)

--bug=1016162 --user=王孝刚
【接口测试】接口提取参数,执行ms登录接口,使用推荐jsonpath提取,提示响应结果不是json格式【+github#17747】
https://www.tapd.cn/55049933/s/1239847

Co-authored-by: wxg0103 <727495428@qq.com>
This commit is contained in:
MeterSphere Bot 2022-09-07 15:10:01 +08:00 committed by GitHub
parent 98337bf6dd
commit 2b4e350357
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 54 deletions

View File

@ -376,12 +376,7 @@ public class ApiAutomationService {
} }
deleteUpdateBodyFile(scenario, beforeScenario); deleteUpdateBodyFile(scenario, beforeScenario);
if (CollectionUtils.isNotEmpty(request.getScenarioDefinition().getHashTree())) {
request.getScenarioDefinition().getHashTree().stream().forEach(hashTree -> {
//删除不需要的文件
FileUtils.deleteBodyFiles(hashTree);
});
}
scenario.setCreateUser(null); // 更新时不更新创建人 scenario.setCreateUser(null); // 更新时不更新创建人
ApiScenarioExample example = new ApiScenarioExample(); ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andIdEqualTo(scenario.getId()).andVersionIdEqualTo(request.getVersionId()); example.createCriteria().andIdEqualTo(scenario.getId()).andVersionIdEqualTo(request.getVersionId());

View File

@ -357,8 +357,7 @@ public class ApiDefinitionService {
MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class); MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class);
mockConfigService.updateMockReturnMsgByApi(returnModel); mockConfigService.updateMockReturnMsgByApi(returnModel);
FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles); FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles);
//删除不需要的文件
FileUtils.deleteBodyFiles(request.getRequest());
return getById(returnModel.getId()); return getById(returnModel.getId());
} }

View File

@ -1,13 +1,9 @@
package io.metersphere.commons.utils; package io.metersphere.commons.utils;
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.dto.scenario.request.BodyFile; import io.metersphere.api.dto.scenario.request.BodyFile;
import io.metersphere.base.domain.JarConfig; import io.metersphere.base.domain.JarConfig;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.JarConfigService; import io.metersphere.service.JarConfigService;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@ -23,7 +19,6 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@ -124,44 +119,7 @@ public class FileUtils {
}); });
} }
} }
public static void deleteBodyFiles(MsTestElement request) {
if (StringUtils.isNotBlank(request.getId())) {
String path = BODY_FILE_DIR + File.separator + request.getId();
File testDir = new File(path);
if (!testDir.exists()) {
return;
}
List<String> files = new ArrayList<>();
if (request != null && StringUtils.equalsIgnoreCase(request.getType(), HTTPSamplerProxy.class.getSimpleName())) {
MsHTTPSamplerProxy samplerProxy = (MsHTTPSamplerProxy) request;
Body body = samplerProxy.getBody();
if (body != null && !CollectionUtils.isEmpty(body.getKvs())) {
body.getKvs().stream().filter(KeyValue::isFile).forEach(keyValue -> {
files.addAll(keyValue.getFiles().stream().map(BodyFile::getName).collect(Collectors.toList()));
});
}
if (body != null && !CollectionUtils.isEmpty(body.getBinary())) {
body.getBinary().stream().filter(KeyValue::isFile).filter(KeyValue::isEnable).forEach(keyValue -> {
files.addAll(keyValue.getFiles().stream().map(BodyFile::getName).collect(Collectors.toList()));
});
}
}
File[] optFilesName = testDir.listFiles();
if (CollectionUtils.isNotEmpty(files)) {
for (File f : optFilesName) {
if (!files.contains(f.getName())) {
f.delete();
}
}
} else {
FileUtil.deleteContents(testDir);
testDir.delete();
}
}
}
public static void copyBodyFiles(String sourceId, String targetId) { public static void copyBodyFiles(String sourceId, String targetId) {
try { try {
String sourcePath = BODY_FILE_DIR + "/" + sourceId; String sourcePath = BODY_FILE_DIR + "/" + sourceId;

View File

@ -1,5 +1,6 @@
<template> <template>
<ms-drawer class="json-path-picker" :visible="visible" :size="30" @close="close" direction="right" v-clickoutside="close"> <ms-drawer v-clickoutside="close" :size="30" :visible="visible" class="json-path-picker" direction="right"
@close="close">
<template v-slot:header> <template v-slot:header>
<ms-instructions-icon :content="tip"/> <ms-instructions-icon :content="tip"/>
{{ tip }} {{ tip }}
@ -69,9 +70,14 @@ export default {
this.data = {}; this.data = {};
try { try {
let stringedJSON = objStr.replace(/:\s*([-+Ee0-9.]+)/g, ': "$1"'); let stringedJSON = objStr.replace(/:\s*([-+Ee0-9.]+)/g, ': "$1"');
// let param;
let JSONBig = require('json-bigint')({"storeAsString": true}); let JSONBig = require('json-bigint')({"storeAsString": true});
let param = JSON.parse(JSON.stringify(JSONBig.parse(stringedJSON))); //
try {
param = JSON.parse(JSON.stringify(JSONBig.parse(stringedJSON)));
} catch (e) {
param = JSON.parse(JSON.stringify(JSONBig.parse(objStr)));
}
if (param instanceof Array) { if (param instanceof Array) {
this.$warning('不支持解析JSON数组'); this.$warning('不支持解析JSON数组');
return; return;