From 458583ce1f2e0d8474a836c4799c6fd4bb8940cc Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Thu, 9 Jul 2020 11:39:05 +0800 Subject: [PATCH] =?UTF-8?q?Swagger=E5=AF=BC=E5=85=A5=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/parse/swagger/SwaggerApi.java | 17 ++ .../api/dto/parse/swagger/SwaggerInfo.java | 11 ++ .../dto/parse/swagger/SwaggerParameter.java | 13 ++ .../api/dto/parse/swagger/SwaggerRequest.java | 16 ++ .../api/dto/parse/swagger/SwaggerTag.java | 9 + .../metersphere/api/parse/PostmanParser.java | 167 ------------------ .../metersphere/api/parse/SwaggerParser.java | 106 +++++++++++ 7 files changed, 172 insertions(+), 167 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerApi.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerInfo.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerParameter.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerRequest.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerTag.java create mode 100644 backend/src/main/java/io/metersphere/api/parse/SwaggerParser.java diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerApi.java b/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerApi.java new file mode 100644 index 0000000000..9eeeed860d --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerApi.java @@ -0,0 +1,17 @@ +package io.metersphere.api.dto.parse.swagger; + +import com.alibaba.fastjson.JSONObject; +import lombok.Data; + +import java.util.List; + +@Data +public class SwaggerApi { + private String swagger; + private SwaggerInfo info; + private String host; + private String basePath; + private List schemes; + private List tags; + private JSONObject paths; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerInfo.java b/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerInfo.java new file mode 100644 index 0000000000..56d538f340 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerInfo.java @@ -0,0 +1,11 @@ +package io.metersphere.api.dto.parse.swagger; + +import lombok.Data; + +@Data +public class SwaggerInfo { + private String version; + private String title; + private String description; + private String termsOfService; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerParameter.java b/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerParameter.java new file mode 100644 index 0000000000..63196e0211 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerParameter.java @@ -0,0 +1,13 @@ +package io.metersphere.api.dto.parse.swagger; + +import lombok.Data; + +@Data +public class SwaggerParameter { + private String name; + private String in; + private String description; + private Boolean required; + private String type; + private String format; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerRequest.java b/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerRequest.java new file mode 100644 index 0000000000..d4e0e9e59a --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerRequest.java @@ -0,0 +1,16 @@ +package io.metersphere.api.dto.parse.swagger; + +import lombok.Data; + +import java.util.List; + +@Data +public class SwaggerRequest { + private List tags; + private String summary; + private String description; + private String operationId; + private List consumes; + private List produces; + private SwaggerParameter parameters; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerTag.java b/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerTag.java new file mode 100644 index 0000000000..14e6ef9bd3 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/swagger/SwaggerTag.java @@ -0,0 +1,9 @@ +package io.metersphere.api.dto.parse.swagger; + +import lombok.Data; + +@Data +public class SwaggerTag { + private String name; + private String description; +} diff --git a/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java b/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java index 8b676fa1c6..6345093cd1 100644 --- a/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java @@ -27,173 +27,6 @@ public class PostmanParser extends ApiImportAbstractParser { @Override public ApiImport parse(InputStream source) { String testStr = getApiTestStr(source); - -// String testStr = "{\n" + -// "\t\"info\": {\n" + -// "\t\t\"_postman_id\": \"9721cd51-8626-4f61-9ac1-e77b8399cca8\",\n" + -// "\t\t\"name\": \"test\",\n" + -// "\t\t\"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"\n" + -// "\t},\n" + -// "\t\"item\": [\n" + -// "\t\t{\n" + -// "\t\t\t\"name\": \"test\",\n" + -// "\t\t\t\"request\": {\n" + -// "\t\t\t\t\"auth\": {\n" + -// "\t\t\t\t\t\"type\": \"basic\",\n" + -// "\t\t\t\t\t\"basic\": [\n" + -// "\t\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\t\"key\": \"password\",\n" + -// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + -// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + -// "\t\t\t\t\t\t},\n" + -// "\t\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\t\"key\": \"username\",\n" + -// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + -// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + -// "\t\t\t\t\t\t}\n" + -// "\t\t\t\t\t]\n" + -// "\t\t\t\t},\n" + -// "\t\t\t\t\"method\": \"POST\",\n" + -// "\t\t\t\t\"header\": [\n" + -// "\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\"key\": \"aaa\",\n" + -// "\t\t\t\t\t\t\"value\": \"testH\",\n" + -// "\t\t\t\t\t\t\"type\": \"text\"\n" + -// "\t\t\t\t\t}\n" + -// "\t\t\t\t],\n" + -// "\t\t\t\t\"body\": {\n" + -// "\t\t\t\t\t\"mode\": \"raw\",\n" + -// "\t\t\t\t\t\"raw\": \"\\n\\n\",\n" + -// "\t\t\t\t\t\"options\": {\n" + -// "\t\t\t\t\t\t\"raw\": {\n" + -// "\t\t\t\t\t\t\t\"language\": \"html\"\n" + -// "\t\t\t\t\t\t}\n" + -// "\t\t\t\t\t}\n" + -// "\t\t\t\t},\n" + -// "\t\t\t\t\"url\": {\n" + -// "\t\t\t\t\t\"raw\": \"https://localhost:8080?tset=test\",\n" + -// "\t\t\t\t\t\"protocol\": \"https\",\n" + -// "\t\t\t\t\t\"host\": [\n" + -// "\t\t\t\t\t\t\"localhost\"\n" + -// "\t\t\t\t\t],\n" + -// "\t\t\t\t\t\"port\": \"8080\",\n" + -// "\t\t\t\t\t\"query\": [\n" + -// "\t\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\t\"key\": \"tset\",\n" + -// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + -// "\t\t\t\t\t\t\t\"description\": \"test\"\n" + -// "\t\t\t\t\t\t}\n" + -// "\t\t\t\t\t]\n" + -// "\t\t\t\t},\n" + -// "\t\t\t\t\"description\": \"dd\"\n" + -// "\t\t\t},\n" + -// "\t\t\t\"response\": []\n" + -// "\t\t},\n" + -// "\t\t{\n" + -// "\t\t\t\"name\": \"test Copy\",\n" + -// "\t\t\t\"request\": {\n" + -// "\t\t\t\t\"auth\": {\n" + -// "\t\t\t\t\t\"type\": \"basic\",\n" + -// "\t\t\t\t\t\"basic\": [\n" + -// "\t\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\t\"key\": \"password\",\n" + -// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + -// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + -// "\t\t\t\t\t\t},\n" + -// "\t\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\t\"key\": \"username\",\n" + -// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + -// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + -// "\t\t\t\t\t\t}\n" + -// "\t\t\t\t\t]\n" + -// "\t\t\t\t},\n" + -// "\t\t\t\t\"method\": \"POST\",\n" + -// "\t\t\t\t\"header\": [\n" + -// "\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\"key\": \"testH\",\n" + -// "\t\t\t\t\t\t\"value\": \"testH\",\n" + -// "\t\t\t\t\t\t\"type\": \"text\"\n" + -// "\t\t\t\t\t}\n" + -// "\t\t\t\t],\n" + -// "\t\t\t\t\"body\": {\n" + -// "\t\t\t\t\t\"mode\": \"raw\",\n" + -// "\t\t\t\t\t\"raw\": \"{\\\"name\\\":\\\"test\\\"}\\n\",\n" + -// "\t\t\t\t\t\"options\": {\n" + -// "\t\t\t\t\t\t\"raw\": {\n" + -// "\t\t\t\t\t\t\t\"language\": \"json\"\n" + -// "\t\t\t\t\t\t}\n" + -// "\t\t\t\t\t}\n" + -// "\t\t\t\t},\n" + -// "\t\t\t\t\"url\": {\n" + -// "\t\t\t\t\t\"raw\": \"http://localhost:8081\",\n" + -// "\t\t\t\t\t\"protocol\": \"http\",\n" + -// "\t\t\t\t\t\"host\": [\n" + -// "\t\t\t\t\t\t\"localhost\"\n" + -// "\t\t\t\t\t],\n" + -// "\t\t\t\t\t\"port\": \"8081\"\n" + -// "\t\t\t\t},\n" + -// "\t\t\t\t\"description\": \"dd\"\n" + -// "\t\t\t},\n" + -// "\t\t\t\"response\": []\n" + -// "\t\t},\n" + -// "\t\t{\n" + -// "\t\t\t\"name\": \"test Copy Copy\",\n" + -// "\t\t\t\"request\": {\n" + -// "\t\t\t\t\"auth\": {\n" + -// "\t\t\t\t\t\"type\": \"basic\",\n" + -// "\t\t\t\t\t\"basic\": [\n" + -// "\t\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\t\"key\": \"password\",\n" + -// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + -// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + -// "\t\t\t\t\t\t},\n" + -// "\t\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\t\"key\": \"username\",\n" + -// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + -// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + -// "\t\t\t\t\t\t}\n" + -// "\t\t\t\t\t]\n" + -// "\t\t\t\t},\n" + -// "\t\t\t\t\"method\": \"POST\",\n" + -// "\t\t\t\t\"header\": [\n" + -// "\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\"key\": \"testH\",\n" + -// "\t\t\t\t\t\t\"value\": \"testH\",\n" + -// "\t\t\t\t\t\t\"type\": \"text\"\n" + -// "\t\t\t\t\t}\n" + -// "\t\t\t\t],\n" + -// "\t\t\t\t\"body\": {\n" + -// "\t\t\t\t\t\"mode\": \"urlencoded\",\n" + -// "\t\t\t\t\t\"urlencoded\": [\n" + -// "\t\t\t\t\t\t{\n" + -// "\t\t\t\t\t\t\t\"key\": \"test\",\n" + -// "\t\t\t\t\t\t\t\"value\": \"tset\",\n" + -// "\t\t\t\t\t\t\t\"description\": \"dd\",\n" + -// "\t\t\t\t\t\t\t\"type\": \"text\"\n" + -// "\t\t\t\t\t\t}\n" + -// "\t\t\t\t\t],\n" + -// "\t\t\t\t\t\"options\": {\n" + -// "\t\t\t\t\t\t\"raw\": {\n" + -// "\t\t\t\t\t\t\t\"language\": \"json\"\n" + -// "\t\t\t\t\t\t}\n" + -// "\t\t\t\t\t}\n" + -// "\t\t\t\t},\n" + -// "\t\t\t\t\"url\": {\n" + -// "\t\t\t\t\t\"raw\": \"http://localhost:8081\",\n" + -// "\t\t\t\t\t\"protocol\": \"http\",\n" + -// "\t\t\t\t\t\"host\": [\n" + -// "\t\t\t\t\t\t\"localhost\"\n" + -// "\t\t\t\t\t],\n" + -// "\t\t\t\t\t\"port\": \"8081\"\n" + -// "\t\t\t\t},\n" + -// "\t\t\t\t\"description\": \"dd\"\n" + -// "\t\t\t},\n" + -// "\t\t\t\"response\": []\n" + -// "\t\t}\n" + -// "\t],\n" + -// "\t\"protocolProfileBehavior\": {}\n" + -// "}"; - PostmanCollection postmanCollection = JSON.parseObject(testStr.toString(), PostmanCollection.class); PostmanCollectionInfo info = postmanCollection.getInfo(); List requests = parseRequests(postmanCollection); diff --git a/backend/src/main/java/io/metersphere/api/parse/SwaggerParser.java b/backend/src/main/java/io/metersphere/api/parse/SwaggerParser.java new file mode 100644 index 0000000000..e3ff9db8f2 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/parse/SwaggerParser.java @@ -0,0 +1,106 @@ +package io.metersphere.api.parse; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import io.metersphere.api.dto.parse.ApiImport; +import io.metersphere.api.dto.parse.postman.PostmanItem; +import io.metersphere.api.dto.parse.postman.PostmanRequest; +import io.metersphere.api.dto.parse.postman.PostmanUrl; +import io.metersphere.api.dto.parse.swagger.SwaggerApi; +import io.metersphere.api.dto.parse.swagger.SwaggerInfo; +import io.metersphere.api.dto.parse.swagger.SwaggerRequest; +import io.metersphere.api.dto.scenario.Request; +import io.metersphere.api.dto.scenario.Scenario; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class SwaggerParser extends ApiImportAbstractParser { + + @Override + public ApiImport parse(InputStream source) { + String testStr = getApiTestStr(source); + + SwaggerApi swaggerApi = JSON.parseObject(testStr.toString(), SwaggerApi.class); + + SwaggerInfo info = swaggerApi.getInfo(); + + String title = info.getTitle(); + + +// List requests = parseRequests(swaggerApi); +// ApiImport apiImport = new ApiImport(); +// List scenarios = new ArrayList<>(); +// Scenario scenario = new Scenario(); +// scenario.setRequests(requests); +// scenario.setName(info.getName()); +// scenarios.add(scenario); +// apiImport.setScenarios(scenarios); +// return apiImport; + return null; + } + +// private List parseRequests(SwaggerApi swaggerApi) { +// JSONObject paths = swaggerApi.getPaths(); +// +// Set pathNames = paths.keySet(); +// +// for (String path : pathNames) { +// JSONObject pathObject = paths.getJSONObject(path); +// Set methods = pathObject.keySet(); +// for (String method : methods) { +// SwaggerRequest swaggerRequest = JSON.parseObject(pathObject.getJSONObject(method).toJSONString(), SwaggerRequest.class); +// Request request = new Request(); +// request.setName(swaggerRequest.getOperationId()); +// request.setUrl(url.getRaw()); +// request.setPath(.getRaw()); +// request.setUseEnvironment(false); +// request.setMethod(requestDesc.getMethod()); +// request.setHeaders(parseKeyValue(requestDesc.getHeader())); +// request.setParameters(parseKeyValue(url.getQuery())); +// +// } +// } +// +// List item = postmanCollection.getItem(); +// List requests = new ArrayList<>(); +// for (PostmanItem requestItem : item) { +// Request request = new Request(); +// PostmanRequest requestDesc = requestItem.getRequest(); +// PostmanUrl url = requestDesc.getUrl(); +// request.setName(requestItem.getName()); +// request.setUrl(url.getRaw()); +// request.setUseEnvironment(false); +// request.setMethod(requestDesc.getMethod()); +// request.setHeaders(parseKeyValue(requestDesc.getHeader())); +// request.setParameters(parseKeyValue(url.getQuery())); +// Body body = new Body(); +// JSONObject postmanBody = requestDesc.getBody(); +// String bodyMode = postmanBody.getString("mode"); +// if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) { +// body.setRaw(postmanBody.getString(bodyMode)); +// body.setType(MsRequestBodyType.RAW.value()); +// String contentType = postmanBody.getJSONObject("options").getJSONObject("raw").getString("language"); +// List headers = request.getHeaders(); +// boolean hasContentType = false; +// for (KeyValue header : headers) { +// if (StringUtils.equalsIgnoreCase(header.getName(), "Content-Type")) { +// hasContentType = true; +// } +// } +// if (!hasContentType) { +// headers.add(new KeyValue("Content-Type", contentType)); +// } +// } else if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value()) || StringUtils.equals(bodyMode, PostmanRequestBodyMode.URLENCODED.value())) { +// List postmanKeyValues = JSON.parseArray(postmanBody.getString(bodyMode), PostmanKeyValue.class); +// body.setType(MsRequestBodyType.KV.value()); +// body.setKvs(parseKeyValue(postmanKeyValues)); +// } +// request.setBody(body); +// requests.add(request); +// } +// return requests; +// } +}