From cd4b7630dbba06cfaba3b89c2698d17b475461f7 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Fri, 24 Mar 2023 18:32:29 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dswagger=E5=AE=9A=E6=97=B6=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E5=80=BC=E4=B8=BAnull=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1024805 --user=王孝刚 【接口测试】swagger定时导入-后端报错-空值显示为null https://www.tapd.cn/55049933/s/1355712 --- .../api/parse/api/Swagger3Parser.java | 23 ++--- .../commons/utils/FakeErrorParse.java | 90 ++++++++++--------- 2 files changed, 59 insertions(+), 54 deletions(-) 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..c8541f6788 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,8 @@ 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 +238,14 @@ 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 +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; 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;