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

--bug=1024805 --user=王孝刚 【接口测试】swagger定时导入-后端报错-空值显示为null
https://www.tapd.cn/55049933/s/1355712
This commit is contained in:
wxg0103 2023-03-27 09:48:31 +08:00 committed by fit2-zhao
parent 7e4d494393
commit 8c979efa57
3 changed files with 73 additions and 59 deletions

View File

@ -7,9 +7,9 @@ import io.metersphere.api.dto.definition.request.variable.JsonSchemaItem;
import io.metersphere.api.dto.definition.response.HttpResponse; import io.metersphere.api.dto.definition.response.HttpResponse;
import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.constants.RequestTypeConstants;
import io.metersphere.commons.constants.SwaggerParameterType; import io.metersphere.commons.constants.SwaggerParameterType;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSON;
@ -51,7 +51,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String
//有一特殊情况swagger2.0 文件里如果在response的parameter参数下的properties的参数里存在 required 为string类型 //有一特殊情况swagger2.0 文件里如果在response的parameter参数下的properties的参数里存在 required 为string类型
//swagger2.0不会导入需替换一下 //swagger2.0不会导入需替换一下
sourceStr = replaceStr(sourceStr); sourceStr = replaceStr(sourceStr);
JSONObject jsonObject = JSONUtil.parseObject(sourceStr); JSONObject jsonObject = JSONUtil.parseObject(sourceStr);
if (jsonObject.opt("swagger") == null || jsonObject.opt("swagger") == "null" || jsonObject.opt("swagger") == StringUtils.SPACE) { if (jsonObject.opt("swagger") == null || jsonObject.opt("swagger") == "null" || jsonObject.opt("swagger") == StringUtils.SPACE) {
@ -265,7 +265,9 @@ public class Swagger2Parser extends SwaggerAbstractParser {
private void parsePathParameters(Parameter parameter, List<KeyValue> rests) { private void parsePathParameters(Parameter parameter, List<KeyValue> rests) {
PathParameter pathParameter = (PathParameter) parameter; PathParameter pathParameter = (PathParameter) parameter;
rests.add(new KeyValue(pathParameter.getName(), getDefaultValue(pathParameter), getDefaultStringValue(parameter.getDescription()), pathParameter.getRequired())); rests.add(new KeyValue(pathParameter.getName(),
pathParameter.getExample() != null ? String.valueOf(pathParameter.getExample()) : null,
getDefaultStringValue(parameter.getDescription()), pathParameter.getRequired()));
} }
private String getDefaultValue(AbstractSerializableParameter parameter) { private String getDefaultValue(AbstractSerializableParameter parameter) {
@ -281,12 +283,16 @@ public class Swagger2Parser extends SwaggerAbstractParser {
private void parseCookieParameters(Parameter parameter, List<KeyValue> headers) { private void parseCookieParameters(Parameter parameter, List<KeyValue> headers) {
CookieParameter cookieParameter = (CookieParameter) parameter; CookieParameter cookieParameter = (CookieParameter) parameter;
addCookie(headers, cookieParameter.getName(), getDefaultValue(cookieParameter), 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) { private void parseHeaderParameters(Parameter parameter, List<KeyValue> headers) {
HeaderParameter headerParameter = (HeaderParameter) parameter; HeaderParameter headerParameter = (HeaderParameter) parameter;
addHeader(headers, headerParameter.getName(), getDefaultValue(headerParameter), getDefaultStringValue(headerParameter.getDescription()), addHeader(headers, headerParameter.getName(),
headerParameter.getExample() != null ? String.valueOf(headerParameter.getExample()) : null,
getDefaultStringValue(headerParameter.getDescription()),
StringUtils.EMPTY, parameter.getRequired()); StringUtils.EMPTY, parameter.getRequired());
} }

View File

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

View File

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