diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java index f255015a62..b6b8db6108 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java @@ -26,13 +26,34 @@ import java.util.regex.Pattern; public abstract class PostmanAbstractParserParser extends ApiImportAbstractParser { protected MsHTTPSamplerProxy parsePostman(PostmanItem requestItem) { - PostmanRequest requestDesc = requestItem.getRequest(); - if (requestDesc == null) { + if (requestItem.getRequest() == null) { return null; + } else { + MsHTTPSamplerProxy request = parseHttpSampler(requestItem.getName(), requestItem.getRequest()); + addBodyHeader(request); + PostmanItem.ProtocolProfileBehavior protocolProfileBehavior = requestItem.getProtocolProfileBehavior(); + request.setFollowRedirects(protocolProfileBehavior == null || + protocolProfileBehavior.getFollowRedirects()); + request.setResponseTimeout("60000"); + request.setConnectTimeout("60000"); + return request; } - requestDesc.getAuth(); // todo 认证方式等待优化 + } + + protected MsHTTPSamplerProxy parseResponse(PostmanResponse requestItem) { + if (requestItem.getOriginalRequest() == null) { + return null; + } else { + MsHTTPSamplerProxy request = parseHttpSampler(requestItem.getName(), requestItem.getOriginalRequest()); + addBodyHeader(request); + return request; + } + } + + private MsHTTPSamplerProxy parseHttpSampler(String requestName, PostmanRequest requestDesc) { + // requestDesc.getAuth(); // todo 认证方式等待优化 PostmanUrl url = requestDesc.getUrl(); - MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url == null ? StringUtils.EMPTY : url.getRaw(), requestDesc.getMethod(), + MsHTTPSamplerProxy request = buildRequest(requestName, url == null ? StringUtils.EMPTY : url.getRaw(), requestDesc.getMethod(), (requestDesc.getBody() == null || requestDesc.getBody().get("jsonSchema") == null) ? StringUtils.EMPTY : requestDesc.getBody().get("jsonSchema").textValue()); request.setRest(parseKeyValue(url != null && CollectionUtils.isNotEmpty(url.getVariable()) ? url.getVariable() : new ArrayList<>())); @@ -47,16 +68,6 @@ public abstract class PostmanAbstractParserParser extends ApiImportAbstractPa parseBody(request.getBody(), requestDesc); request.setArguments(parseKeyValue(url == null ? new ArrayList<>() : url.getQuery())); request.setHeaders(parseKeyValue(requestDesc.getHeader())); - addBodyHeader(request); - PostmanItem.ProtocolProfileBehavior protocolProfileBehavior = requestItem.getProtocolProfileBehavior(); - if (protocolProfileBehavior != null && - !protocolProfileBehavior.getFollowRedirects()) { - request.setFollowRedirects(false); - } else { - request.setFollowRedirects(true); - } - request.setResponseTimeout("60000"); - request.setConnectTimeout("60000"); return request; } @@ -169,7 +180,7 @@ public abstract class PostmanAbstractParserParser extends ApiImportAbstractPa } private void parseRawBody(Body body, ObjectNode postmanBody, String bodyMode) { - body.setRaw(parseVariable(postmanBody.get(bodyMode).textValue())); + body.setRaw(parseVariable(postmanBody.get(bodyMode) == null ? StringUtils.EMPTY : postmanBody.get(bodyMode).textValue())); body.setType(MsRequestBodyType.RAW.value()); JsonNode options = postmanBody.get("options"); if (options != null) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/PostmanDefinitionParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/PostmanDefinitionParser.java index 275e0be757..77f749e4bb 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/PostmanDefinitionParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/PostmanDefinitionParser.java @@ -4,21 +4,25 @@ package io.metersphere.api.parse.api; import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.response.HttpResponse; +import io.metersphere.api.parse.PostmanAbstractParserParser; import io.metersphere.api.parse.postman.PostmanCollection; import io.metersphere.api.parse.postman.PostmanItem; -import io.metersphere.api.parse.PostmanAbstractParserParser; +import io.metersphere.api.parse.postman.PostmanResponse; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSONUtil; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.json.JSONObject; import java.io.InputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class PostmanDefinitionParser extends PostmanAbstractParserParser { @@ -34,22 +38,21 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser results = new ArrayList<>(); + Map resultMap = new HashMap<>(); String modulePath = null; if (StringUtils.isNotBlank(postmanCollection.getInfo().getName())) { modulePath = "/" + postmanCollection.getInfo().getName(); } List cases = new ArrayList<>(); - parseItem(postmanCollection.getItem(), modulePath, results, - cases, addCase); - apiImport.setData(results); + parseApiDefinition(postmanCollection.getItem(), modulePath, resultMap, cases); + apiImport.setData(new ArrayList<>(resultMap.values())); apiImport.setCases(cases); return apiImport; } - protected void parseItem(List items, String modulePath, List results, - List cases, boolean addCase) { + protected void parseApiDefinition(List items, String modulePath, Map apiResultMap, + List cases) { for (PostmanItem item : items) { List childItems = item.getItem(); if (childItems != null) { @@ -59,7 +62,7 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser existenceNameList = new ArrayList<>(); + for (PostmanResponse postmanResponse : item.getResponse()) { + ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs(); + BeanUtils.copyBean(apiTestCase, request); + apiTestCase.setApiDefinitionId(request.getId()); + apiTestCase.setPriority("P0"); + apiTestCase.setName(this.getUniqueName(apiTestCase.getName(), existenceNameList)); + MsHTTPSamplerProxy httpResponse = parseResponse(postmanResponse); + apiTestCase.setRequest(JSON.toJSONString(httpResponse)); + cases.add(apiTestCase); + existenceNameList.add(apiTestCase.getName()); + } } } } } + + public String getUniqueName(String originalName, List existenceNameList) { + String returnName = originalName; + int index = 1; + while (existenceNameList.contains(returnName)) { + returnName = originalName + " - " + index; + index++; + } + return returnName; + } } 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 88c18b404d..f0264d8a8b 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 @@ -325,7 +325,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { } private void parseResponseBody(ApiResponse response, Body body) { - body.setRaw(response.getDescription()); + // body.setRaw(response.getDescription()); Content content = response.getContent(); if (content == null) { body.setType(Body.RAW); @@ -1197,22 +1197,25 @@ public class Swagger3Parser extends SwaggerAbstractParser { } } + JSONObject statusCodeInfo = new JSONObject(); + statusCodeInfo.put("headers", headers); + statusCodeInfo.put("content", buildContent(response, schemas)); + statusCodeInfo.put("description", StringUtils.EMPTY); + // 返回code JSONArray statusCode = response.optJSONArray("statusCode"); if (statusCode != null) { - for (int i = 0; i < statusCode.length(); i++) { - JSONObject statusCodeInfo = new JSONObject(); - statusCodeInfo.put("headers", headers); - statusCodeInfo.put("content", buildContent(response, schemas)); - statusCodeInfo.put("description", StringUtils.EMPTY); - JSONObject jsonObject = statusCode.getJSONObject(i); - if (StringUtils.isNotBlank(jsonObject.optString("value"))) { - statusCodeInfo.put("description", jsonObject.optString("value")); - } - if (StringUtils.isNotBlank(jsonObject.optString("name"))) { - responseBody.put(jsonObject.optString("name"), statusCodeInfo); - } + JSONObject jsonObject = statusCode.getJSONObject(0); + if (StringUtils.isNotBlank(jsonObject.optString("value"))) { + statusCodeInfo.put("description", jsonObject.optString("value")); } + if (StringUtils.isNotBlank(jsonObject.optString("name"))) { + responseBody.put(jsonObject.optString("name"), statusCodeInfo); + } else { + responseBody.put("200", statusCodeInfo); + } + } else { + responseBody.put("200", statusCodeInfo); } return responseBody; } diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java index 81279aa4d5..0b50e0b812 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java @@ -760,7 +760,7 @@ public class ApiDefinitionImportUtilService { } else { apiDefinition.setVersionId(apiTestImportRequest.getDefaultVersion()); } - boolean newCreate = !StringUtils.equals(ApiImportPlatform.Swagger2.name(), apiDefinitionImportParamDTO.getApiTestImportRequest().getPlatform()) + boolean newCreate = !StringUtils.equalsAnyIgnoreCase(apiDefinitionImportParamDTO.getApiTestImportRequest().getPlatform(), ApiImportPlatform.Swagger2.name(), ApiImportPlatform.Postman.name()) && !StringUtils.isNotBlank(apiDefinitionImportParamDTO.getApiTestImportRequest().getSwaggerUrl()) && !StringUtils.equals("idea", apiDefinitionImportParamDTO.getApiTestImportRequest().getOrigin()); caseList = setRequestAndAddNewCase(apiDefinition, caseList, newCreate);