From 1384dabbea2bc4f2c05d0aa201694041f39b9f09 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Wed, 22 May 2024 14:54:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiDefinitionImportUtilService.java | 252 ++++++++++-------- 1 file changed, 144 insertions(+), 108 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java index 9792e6bfe6..e343f82a28 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java @@ -16,14 +16,13 @@ import io.metersphere.api.dto.definition.ApiModuleRequest; import io.metersphere.api.dto.request.ImportRequest; import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.dto.request.http.MsHeader; -import io.metersphere.api.dto.request.http.QueryParam; -import io.metersphere.api.dto.request.http.RestParam; import io.metersphere.api.dto.request.http.body.*; import io.metersphere.api.dto.schema.JsonSchemaItem; import io.metersphere.api.mapper.*; import io.metersphere.api.parser.ImportParser; import io.metersphere.api.parser.ImportParserFactory; import io.metersphere.api.utils.ApiDataUtils; +import io.metersphere.project.api.KeyValueEnableParam; import io.metersphere.project.constants.PropertyConstant; import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ExtBaseProjectVersionMapper; @@ -49,7 +48,6 @@ import io.metersphere.system.uid.NumGenerator; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; @@ -567,112 +565,148 @@ public class ApiDefinitionImportUtilService { } public boolean dataIsSame(MsHTTPElement dbRequest, MsHTTPElement importRequest) { - boolean same = true; - //判断请求头是否一样 - List dbHeaders = dbRequest.getHeaders(); - List importHeaders = importRequest.getHeaders(); - if (CollectionUtils.isNotEmpty(dbHeaders) || CollectionUtils.isNotEmpty(importHeaders)) { - List dbHeaderKeys = dbHeaders.stream().map(MsHeader::getKey).toList(); - List importHeaderKeys = importHeaders.stream().map(MsHeader::getKey).toList(); - if (paramsIsSame(dbHeaderKeys, importHeaderKeys)) { + // 判断请求头是否一样 + if (headersHasDifferent(dbRequest.getHeaders(), importRequest.getHeaders())) { + return false; + } + + // 判断请求参数是否一样 + if (paramsAreSame(dbRequest.getQuery(), importRequest.getQuery()) || + paramsAreSame(dbRequest.getRest(), importRequest.getRest())) { + return false; + } + + // 判断请求体是否一样 + return bodyAreSame(dbRequest.getBody(), importRequest.getBody()); + } + + private boolean headersHasDifferent(List dbHeaders, List importHeaders) { + if (CollectionUtils.isEmpty(dbHeaders) && CollectionUtils.isEmpty(importHeaders)) { + return false; + } + + List dbHeaderKeys = dbHeaders.stream().map(MsHeader::getKey).toList(); + List importHeaderKeys = importHeaders.stream().map(MsHeader::getKey).toList(); + + return keysHasDifferent(dbHeaderKeys, importHeaderKeys); + } + + private boolean paramsAreSame(List dbParams, List importParams) { + if (CollectionUtils.isEmpty(dbParams) && CollectionUtils.isEmpty(importParams)) { + return false; + } + + List dbParamKeys = dbParams.stream().map(KeyValueEnableParam::getKey).toList(); + List importParamKeys = importParams.stream().map(KeyValueEnableParam::getKey).toList(); + + return keysHasDifferent(dbParamKeys, importParamKeys); + } + + private boolean bodyAreSame(Body dbBody, Body importBody) { + if (dbBody == null && importBody == null) { + return true; + } + + if (dbBody != null && importBody != null) { + if (!StringUtils.equals(dbBody.getBodyType(), importBody.getBodyType())) { return false; } + + return switch (Body.BodyType.valueOf(dbBody.getBodyType())) { + case Body.BodyType.FORM_DATA -> + !formBodyHaDifferent(dbBody.getFormDataBody(), importBody.getFormDataBody()); + case Body.BodyType.WWW_FORM -> + !wwwFormBodyHasDifferent(dbBody.getWwwFormBody(), importBody.getWwwFormBody()); + case Body.BodyType.RAW -> rawBodyAreSame(dbBody.getRawBody(), importBody.getRawBody()); + case Body.BodyType.JSON -> jsonBodyAreSame(dbBody.getJsonBody(), importBody.getJsonBody()); + case Body.BodyType.XML -> xmlBodyAreSame(dbBody.getXmlBody(), importBody.getXmlBody()); + default -> true; + }; } - //判断请求参数是否一样 query rest body - List dbQuery = dbRequest.getQuery(); - List importQuery = importRequest.getQuery(); - if (CollectionUtils.isNotEmpty(dbQuery) || CollectionUtils.isNotEmpty(importQuery)) { - List dbQueryKeys = dbQuery.stream().map(QueryParam::getKey).toList(); - List importQueryKeys = importQuery.stream().map(QueryParam::getKey).toList(); - if (paramsIsSame(dbQueryKeys, importQueryKeys)) { + + return true; + } + + private boolean xmlBodyAreSame(XmlBody xmlBody, XmlBody importXmlBody) { + if (xmlBody == null && importXmlBody == null) { + return true; + } + return xmlBody != null && importXmlBody != null && StringUtils.equals(xmlBody.getValue(), importXmlBody.getValue()); + + } + + private boolean formBodyHaDifferent(FormDataBody dbFormBody, FormDataBody importFormBody) { + if (dbFormBody == null && importFormBody == null) { + return false; + } + + if (dbFormBody != null && importFormBody != null) { + List dbFormValues = dbFormBody.getFormValues(); + List importFormValues = importFormBody.getFormValues(); + + return keysHasDifferent( + dbFormValues.stream().map(FormDataKV::getKey).toList(), + importFormValues.stream().map(FormDataKV::getKey).toList() + ); + } + + return false; + } + + private boolean wwwFormBodyHasDifferent(WWWFormBody dbWwwBody, WWWFormBody importWwwBody) { + if (dbWwwBody == null && importWwwBody == null) { + return false; + } + + if (dbWwwBody != null && importWwwBody != null) { + List dbWwwValues = dbWwwBody.getFormValues(); + List importWwwValues = importWwwBody.getFormValues(); + + return keysHasDifferent( + dbWwwValues.stream().map(WWWFormKV::getKey).toList(), + importWwwValues.stream().map(WWWFormKV::getKey).toList() + ); + } + + return false; + } + + private boolean rawBodyAreSame(RawBody dbRawBody, RawBody importRawBody) { + if (dbRawBody == null && importRawBody == null) { + return true; + } + return dbRawBody != null && importRawBody != null && StringUtils.equals(dbRawBody.getValue(), importRawBody.getValue()); + } + + private boolean jsonBodyAreSame(JsonBody dbJsonBody, JsonBody importJsonBody) { + if (dbJsonBody == null && importJsonBody == null) { + return true; + } + + if (dbJsonBody != null && importJsonBody != null) { + if (!StringUtils.equals(dbJsonBody.getJsonValue(), importJsonBody.getJsonValue())) { return false; } - } - List dbRest = dbRequest.getRest(); - List importRest = importRequest.getRest(); - if (CollectionUtils.isNotEmpty(dbRest) || CollectionUtils.isNotEmpty(importRest)) { - List dbRestKeys = dbRest.stream().map(RestParam::getKey).toList(); - List importRestKeys = importRest.stream().map(RestParam::getKey).toList(); - if (paramsIsSame(dbRestKeys, importRestKeys)) { - return false; - } - } - //判断请求体是否一样 - if (dbRequest.getBody() != null || importRequest.getBody() != null) { - //判断请求体的参数 - Body dbbody = dbRequest.getBody(); - Body importBody = importRequest.getBody(); - if (dbbody != null && importBody != null) { - if (!StringUtils.equals(dbbody.getBodyType(), importBody.getBodyType())) { - return false; - } - //判断null类型 - StringUtils.equals(String.valueOf(dbbody.getNoneBody()), String.valueOf(importBody.getNoneBody())); - //判断form类型 - if (StringUtils.equals(dbbody.getBodyType(), Body.BodyType.FORM_DATA.name())) { - FormDataBody formDataBody = dbbody.getFormDataBody(); - FormDataBody importFormDataBody = importBody.getFormDataBody(); - if (ObjectUtils.isNotEmpty(formDataBody) || ObjectUtils.isNotEmpty(importFormDataBody)) { - List formValues = formDataBody.getFormValues(); - List importFormValues = importFormDataBody.getFormValues(); - if (CollectionUtils.isNotEmpty(formValues) || CollectionUtils.isNotEmpty(importFormValues)) { - List dbFormKeys = formValues.stream().map(FormDataKV::getKey).toList(); - List importFormKeys = importFormValues.stream().map(FormDataKV::getKey).toList(); - if (paramsIsSame(dbFormKeys, importFormKeys)) { - return false; - } - } - } - } - if (StringUtils.equals(dbbody.getBodyType(), Body.BodyType.WWW_FORM.name())) { - //判读www类型 - WWWFormBody wwwBody = dbbody.getWwwFormBody(); - WWWFormBody importWwwBody = importBody.getWwwFormBody(); - if (ObjectUtils.isNotEmpty(wwwBody) || ObjectUtils.isNotEmpty(importWwwBody)) { - List wwwValues = wwwBody.getFormValues(); - List importWwwValues = importWwwBody.getFormValues(); - if (CollectionUtils.isNotEmpty(wwwValues) || CollectionUtils.isNotEmpty(importWwwValues)) { - List dbWwwKeys = wwwValues.stream().map(WWWFormKV::getKey).toList(); - List importWwwKeys = importWwwValues.stream().map(WWWFormKV::getKey).toList(); - if (paramsIsSame(dbWwwKeys, importWwwKeys)) { - return false; - } - } - } - } - //TODO 判断binary类型 + JsonSchemaItem dbJsonSchema = dbJsonBody.getJsonSchema(); + JsonSchemaItem importJsonSchema = importJsonBody.getJsonSchema(); - //判断raw类型 - if (StringUtils.equals(dbbody.getBodyType(), Body.BodyType.RAW.name())) { - RawBody rawBody = dbbody.getRawBody(); - RawBody importRawBody = importBody.getRawBody(); - if (ObjectUtils.isNotEmpty(rawBody) || ObjectUtils.isNotEmpty(importRawBody)) { - return false; - } - } - //判断json类型 - if (StringUtils.equals(dbbody.getBodyType(), Body.BodyType.JSON.name())) { - //判断json类型 - JsonBody jsonBody = dbbody.getJsonBody(); - JsonBody importJsonBody = importBody.getJsonBody(); - if (ObjectUtils.isNotEmpty(jsonBody) || ObjectUtils.isNotEmpty(importJsonBody)) { - if (!StringUtils.equals(jsonBody.getJsonValue(), importJsonBody.getJsonValue())) { - return false; - } - //判断jsonschema - JsonSchemaItem jsonSchema = jsonBody.getJsonSchema(); - JsonSchemaItem importJsonSchema = importJsonBody.getJsonSchema(); - if (jsonSchema != null && importJsonSchema != null) { - return jsonSchemaIsSame(jsonSchema, importJsonSchema); - } - } - } - } + return jsonSchemasAreSame(dbJsonSchema, importJsonSchema); } + return true; + } - return same; + private boolean jsonSchemasAreSame(JsonSchemaItem dbJsonSchema, JsonSchemaItem importJsonSchema) { + if (dbJsonSchema == null && importJsonSchema == null) { + return true; + } + + if (dbJsonSchema != null && importJsonSchema != null) { + return jsonSchemaIsSame(dbJsonSchema, importJsonSchema); + } + + return true; } //判断jsonschema的参数是否一样 @@ -689,17 +723,16 @@ public class ApiDefinitionImportUtilService { if (jsonSchema == null && importJsonSchema == null) { return true; } - assert jsonSchema != null; - if (!StringUtils.equals(jsonSchema.getType(), importJsonSchema.getType())) { + if (jsonSchema != null && !StringUtils.equals(jsonSchema.getType(), importJsonSchema.getType())) { return false; } - if (StringUtils.equals(jsonSchema.getType(), PropertyConstant.OBJECT)) { + if (jsonSchema != null && StringUtils.equals(jsonSchema.getType(), PropertyConstant.OBJECT)) { Map properties = jsonSchema.getProperties(); Map importProperties = importJsonSchema.getProperties(); if (MapUtils.isNotEmpty(properties) || MapUtils.isNotEmpty(importProperties)) { List dbJsonKeys = properties.keySet().stream().toList(); List importJsonKeys = importProperties.keySet().stream().toList(); - if (paramsIsSame(dbJsonKeys, importJsonKeys)) { + if (keysHasDifferent(dbJsonKeys, importJsonKeys)) { return false; } //遍历判断每个参数是否一样 @@ -713,7 +746,7 @@ public class ApiDefinitionImportUtilService { } } } - if (StringUtils.equals(jsonSchema.getType(), PropertyConstant.ARRAY)) { + if (jsonSchema != null && StringUtils.equals(jsonSchema.getType(), PropertyConstant.ARRAY)) { JsonSchemaItem items = jsonSchema.getItems(); JsonSchemaItem importItems = importJsonSchema.getItems(); if (items != null && importItems != null) { @@ -725,13 +758,16 @@ public class ApiDefinitionImportUtilService { return same; } - private static boolean paramsIsSame(List dbRestKeys, List importRestKeys) { - if (dbRestKeys.size() != importRestKeys.size()) { + private static boolean keysHasDifferent(List dbKeys, List importKeys) { + if (CollectionUtils.isEmpty(dbKeys) && CollectionUtils.isEmpty(importKeys)) { + return false; + } + if (dbKeys.size() != importKeys.size()) { return true; } //看看是否有差集 - List differenceRest = dbRestKeys.stream().filter(t -> !importRestKeys.contains(t)).toList(); - return !CollectionUtils.isEmpty(differenceRest); + List differenceRest = dbKeys.stream().filter(t -> !importKeys.contains(t)).toList(); + return CollectionUtils.isNotEmpty(differenceRest); } private Boolean getFullCoverage(Boolean fullCoverage) {