diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger2Parser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger2Parser.java index 7c8eafafe6..f5732f7a1f 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger2Parser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger2Parser.java @@ -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.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; -import io.metersphere.commons.constants.RequestTypeConstants; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.commons.constants.PropertyConstant; +import io.metersphere.commons.constants.RequestTypeConstants; import io.metersphere.commons.constants.SwaggerParameterType; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.JSON; @@ -51,7 +51,7 @@ public class Swagger2Parser extends SwaggerAbstractParser { sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String //注:有一特殊情况,swagger2.0 文件里如果在response的parameter参数下的properties的参数里存在 required 为string类型, //swagger2.0不会导入,需替换一下 - sourceStr = replaceStr(sourceStr); + sourceStr = replaceStr(sourceStr); JSONObject jsonObject = JSONUtil.parseObject(sourceStr); 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 rests) { 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) { @@ -281,12 +283,16 @@ public class Swagger2Parser extends SwaggerAbstractParser { private void parseCookieParameters(Parameter parameter, List headers) { 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 headers) { 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()); } diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java index 3ff4ae43b6..a71db46e83 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java @@ -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,9 @@ public class Swagger3Parser extends SwaggerAbstractParser { private void parsePathParameters(Parameter parameter, List 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 +239,16 @@ public class Swagger3Parser extends SwaggerAbstractParser { private void parseCookieParameters(Parameter parameter, List 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 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 +644,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; diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/FakeErrorParse.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/FakeErrorParse.java index 0e5dba1abb..7710a961ba 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/FakeErrorParse.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/FakeErrorParse.java @@ -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 bloBs = service.selectByExampleWithBLOBs(example); - List regexList = new ArrayList<>(); - bloBs.forEach(item -> { - if (StringUtils.isNotEmpty(item.getContent())) { - try { - Map 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 bloBs = service.selectByExampleWithBLOBs(example); + List regexList = new ArrayList<>(); + bloBs.forEach(item -> { + if (StringUtils.isNotEmpty(item.getContent())) { + try { + Map 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 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 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;