fix(接口测试): 修复swagger定时同步值为null的缺陷

--bug=1024805 --user=王孝刚 【接口测试】swagger定时导入-后端报错-空值显示为null
https://www.tapd.cn/55049933/s/1355712
This commit is contained in:
wxg0103 2023-03-24 18:32:29 +08:00 committed by fit2-zhao
parent 4718431789
commit cd4b7630db
2 changed files with 59 additions and 54 deletions

View File

@ -3,25 +3,25 @@ package io.metersphere.api.parse.api;
import com.fasterxml.jackson.databind.JsonNode;
import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.SwaggerApiExportResult;
import io.metersphere.api.parse.api.swagger.SwaggerApiInfo;
import io.metersphere.api.parse.api.swagger.SwaggerInfo;
import io.metersphere.api.parse.api.swagger.SwaggerParams;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.variable.JsonSchemaItem;
import io.metersphere.api.dto.definition.response.HttpResponse;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.api.parse.api.swagger.SwaggerApiInfo;
import io.metersphere.api.parse.api.swagger.SwaggerInfo;
import io.metersphere.api.parse.api.swagger.SwaggerParams;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.Project;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.i18n.Translator;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.XMLUtil;
import io.metersphere.i18n.Translator;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.headers.Header;
@ -228,7 +228,8 @@ public class Swagger3Parser extends SwaggerAbstractParser {
private void parsePathParameters(Parameter parameter, List<KeyValue> rests) {
PathParameter pathParameter = (PathParameter) parameter;
rests.add(new KeyValue(pathParameter.getName(), String.valueOf(pathParameter.getExample()), getDefaultStringValue(parameter.getDescription())));
rests.add(new KeyValue(pathParameter.getName(), pathParameter.getExample() != null ? String.valueOf(pathParameter.getExample()) : null,
getDefaultStringValue(parameter.getDescription())));
}
private String getDefaultStringValue(String val) {
@ -237,12 +238,14 @@ public class Swagger3Parser extends SwaggerAbstractParser {
private void parseCookieParameters(Parameter parameter, List<KeyValue> headers) {
CookieParameter cookieParameter = (CookieParameter) parameter;
addCookie(headers, cookieParameter.getName(), String.valueOf(cookieParameter.getExample()), getDefaultStringValue(cookieParameter.getDescription()), parameter.getRequired());
addCookie(headers, cookieParameter.getName(), cookieParameter.getExample() != null ? String.valueOf(cookieParameter.getExample()) : null,
getDefaultStringValue(cookieParameter.getDescription()), parameter.getRequired());
}
private void parseHeaderParameters(Parameter parameter, List<KeyValue> headers) {
HeaderParameter headerParameter = (HeaderParameter) parameter;
addHeader(headers, headerParameter.getName(), String.valueOf(headerParameter.getExample()), getDefaultStringValue(headerParameter.getDescription()), StringUtils.EMPTY, parameter.getRequired());
addHeader(headers, headerParameter.getName(), headerParameter.getExample() != null ? String.valueOf(headerParameter.getExample()) : null,
getDefaultStringValue(headerParameter.getDescription()), StringUtils.EMPTY, parameter.getRequired());
}
private HttpResponse parseResponse(ApiResponses responses) {
@ -638,7 +641,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
if (queryParameter.getExample() != null) {
return String.valueOf(queryParameter.getExample());
} else {
if (jsonSchemaItem != null && jsonSchemaItem.getDefaultValue()!=null) {
if (jsonSchemaItem != null && jsonSchemaItem.getDefaultValue() != null) {
return String.valueOf(jsonSchemaItem.getDefaultValue());
}
return null;

View File

@ -28,57 +28,59 @@ public class FakeErrorParse {
if (StringUtils.isNotBlank(result.getFakeErrorMessage())) {
FakeError errorReportDTO = JsonUtils.parseObject(result.getFakeErrorMessage(), FakeError.class);
ErrorReportLibraryService service = CommonBeanFactory.getBean(ErrorReportLibraryService.class);
ErrorReportLibraryExample example = new ErrorReportLibraryExample();
example.createCriteria().andProjectIdEqualTo(errorReportDTO.getProjectId()).andStatusEqualTo(true);
List<ErrorReportLibraryWithBLOBs> bloBs = service.selectByExampleWithBLOBs(example);
List<MsRegexDTO> regexList = new ArrayList<>();
bloBs.forEach(item -> {
if (StringUtils.isNotEmpty(item.getContent())) {
try {
Map<String, Object> assertionMap = JSON.parseObject(item.getContent(), Map.class);
if (assertionMap != null) {
MsRegexDTO regexConfig = JSON.parseObject(JSONUtil.toJSONString(assertionMap.get("regexConfig")), MsRegexDTO.class);
regexConfig.setErrorCode(item.getErrorCode());
regexList.add(regexConfig);
if (service != null) {
ErrorReportLibraryExample example = new ErrorReportLibraryExample();
example.createCriteria().andProjectIdEqualTo(errorReportDTO.getProjectId()).andStatusEqualTo(true);
List<ErrorReportLibraryWithBLOBs> bloBs = service.selectByExampleWithBLOBs(example);
List<MsRegexDTO> regexList = new ArrayList<>();
bloBs.forEach(item -> {
if (StringUtils.isNotEmpty(item.getContent())) {
try {
Map<String, Object> assertionMap = JSON.parseObject(item.getContent(), Map.class);
if (assertionMap != null) {
MsRegexDTO regexConfig = JSON.parseObject(JSONUtil.toJSONString(assertionMap.get("regexConfig")), MsRegexDTO.class);
regexConfig.setErrorCode(item.getErrorCode());
regexList.add(regexConfig);
}
} catch (Exception e) {
LogUtil.error(e);
}
} catch (Exception e) {
LogUtil.error(e);
}
}
});
//根据配置来筛选断言获取误报编码获取接口状态是否是误报
List<String> errorCodeList = new ArrayList<>();
regexList.forEach(item -> {
if (StringUtils.isNotEmpty(item.getSubject())) {
switch (item.getSubject()) {
case "Response Code" ->
item.setPass(parseResponseCode(result.getResponseResult().getResponseCode(), item.getValue(), item.getCondition()));
});
//根据配置来筛选断言获取误报编码获取接口状态是否是误报
List<String> errorCodeList = new ArrayList<>();
regexList.forEach(item -> {
if (StringUtils.isNotEmpty(item.getSubject())) {
switch (item.getSubject()) {
case "Response Code" ->
item.setPass(parseResponseCode(result.getResponseResult().getResponseCode(), item.getValue(), item.getCondition()));
case "Response Headers" ->
item.setPass(parseResponseCode(result.getResponseResult().getHeaders(), item.getValue(), item.getCondition()));
case "Response Headers" ->
item.setPass(parseResponseCode(result.getResponseResult().getHeaders(), item.getValue(), item.getCondition()));
case "Response Data" ->
item.setPass(parseResponseCode(result.getResponseResult().getBody(), item.getValue(), item.getCondition()));
default -> item.setPass(false);
case "Response Data" ->
item.setPass(parseResponseCode(result.getResponseResult().getBody(), item.getValue(), item.getCondition()));
default -> item.setPass(false);
}
}
if (item.isPass()) {
errorCodeList.add(item.getErrorCode());
}
});
boolean higherThanError = errorReportDTO.isHigherThanError();
boolean higherThanSuccess = errorReportDTO.isHigherThanSuccess();
if (CollectionUtils.isNotEmpty(errorCodeList)) {
if ((higherThanError && !result.isSuccess()) || (higherThanSuccess && result.isSuccess())) {
fakeError.setRequestStatus(ApiReportStatus.FAKE_ERROR.name());
}
fakeError.setErrorCodeList(errorCodeList);
}
if (item.isPass()) {
errorCodeList.add(item.getErrorCode());
}
});
boolean higherThanError = errorReportDTO.isHigherThanError();
boolean higherThanSuccess = errorReportDTO.isHigherThanSuccess();
if (CollectionUtils.isNotEmpty(errorCodeList)) {
if ((higherThanError && !result.isSuccess()) || (higherThanSuccess && result.isSuccess())) {
fakeError.setRequestStatus(ApiReportStatus.FAKE_ERROR.name());
}
fakeError.setErrorCodeList(errorCodeList);
LogUtil.info(" FAKE_ERROR result: config-higherThanError:" + higherThanError
+ ", config-higherThanSuccess:" + higherThanSuccess
+ ", resultIsSuccess: " + result.isSuccess()
+ ", isFakeError: " + ((higherThanError && !result.isSuccess()) || (higherThanSuccess && result.isSuccess()))
+ "; status:" + fakeError.getRequestStatus());
}
LogUtil.info(" FAKE_ERROR result: config-higherThanError:" + higherThanError
+ ", config-higherThanSuccess:" + higherThanSuccess
+ ", resultIsSuccess: " + result.isSuccess()
+ ", isFakeError: " + ((higherThanError && !result.isSuccess()) || (higherThanSuccess && result.isSuccess()))
+ "; status:" + fakeError.getRequestStatus());
}
fakeError.setResult(result);
return fakeError;