fix: postman 多级目录导入

This commit is contained in:
chenjianxing 2020-07-29 13:16:46 +08:00
parent 5aca08a677
commit 5b9279b116
4 changed files with 48 additions and 28 deletions

View File

@ -8,6 +8,6 @@ import java.util.List;
public class PostmanCollection {
private PostmanCollectionInfo info;
private List<PostmanItem> item;
private List<PostmanKeyValue> variable;
}

View File

@ -2,9 +2,11 @@ package io.metersphere.api.dto.parse.postman;
import lombok.Data;
import java.util.List;
@Data
public class PostmanItem {
private String name;
private PostmanRequest request;
private List<PostmanItem> item;
}

View File

@ -15,10 +15,7 @@ import io.metersphere.commons.constants.PostmanRequestBodyMode;
import org.apache.commons.lang3.StringUtils;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public class PostmanParser extends ApiImportAbstractParser {
@ -35,18 +32,20 @@ public class PostmanParser extends ApiImportAbstractParser {
@Override
public ApiImport parse(InputStream source, ApiTestImportRequest request) {
String testStr = getApiTestStr(source);
PostmanCollection postmanCollection = JSON.parseObject(testStr, PostmanCollection.class);
PostmanCollectionInfo info = postmanCollection.getInfo();
List<Request> requests = parseRequests(postmanCollection);
List<PostmanKeyValue> variables = postmanCollection.getVariable();
ApiImport apiImport = new ApiImport();
List<Scenario> scenarios = new ArrayList<>();
Scenario scenario = new Scenario();
scenario.setRequests(requests);
scenario.setName(info.getName());
setScenarioByRequest(scenario, request);
scenarios.add(scenario);
parseItem(postmanCollection.getItem(), scenario, variables, scenarios);
apiImport.setScenarios(scenarios);
return apiImport;
}
@ -59,23 +58,42 @@ public class PostmanParser extends ApiImportAbstractParser {
return keyValues;
}
private List<Request> parseRequests(PostmanCollection postmanCollection) {
List<PostmanItem> item = postmanCollection.getItem();
private void parseItem(List<PostmanItem> items, Scenario scenario, List<PostmanKeyValue> variables, List<Scenario> scenarios) {
List<Request> requests = new ArrayList<>();
for (PostmanItem requestItem : item) {
HttpRequest request = new HttpRequest();
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()));
request.setBody(parseBody(requestDesc, request));
requests.add(request);
for (PostmanItem item : items) {
List<PostmanItem> childItems = item.getItem();
if (childItems != null) {
Scenario subScenario = new Scenario();
subScenario.setName(item.getName());
subScenario.setEnvironmentId(scenario.getEnvironmentId());
parseItem(childItems, subScenario, variables, scenarios);
} else {
Request request = parseRequest(item);
if (request != null) {
requests.add(request);
}
}
}
return requests;
scenario.setVariables(parseKeyValue(variables));
scenario.setRequests(requests);
scenarios.add(scenario);
}
private Request parseRequest(PostmanItem requestItem) {
HttpRequest request = new HttpRequest();
PostmanRequest requestDesc = requestItem.getRequest();
if (requestDesc == null) {
return null;
}
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()));
request.setBody(parseBody(requestDesc, request));
return request;
}
private Body parseBody(PostmanRequest requestDesc, HttpRequest request) {
@ -88,8 +106,8 @@ public class PostmanParser extends ApiImportAbstractParser {
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
body.setRaw(postmanBody.getString(bodyMode));
body.setType(MsRequestBodyType.RAW.value());
String contentType = postmanBodyRowMap.get(postmanBody.getJSONObject("options").getJSONObject("raw").getString("language"));
addContentType(request, contentType);
String language = postmanBody.getJSONObject("options").getJSONObject(PostmanRequestBodyMode.RAW.value()).getString("language");
body.setFormat(language);
} else if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value()) || StringUtils.equals(bodyMode, PostmanRequestBodyMode.URLENCODED.value())) {
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(postmanBody.getString(bodyMode), PostmanKeyValue.class);
body.setType(MsRequestBodyType.KV.value());

View File

@ -28,7 +28,7 @@
<el-option v-for="(project, index) in projects" :key="index" :label="project.name" :value="project.id"/>
</el-select>
</el-form-item>
<el-form-item v-if="(selectedPlatformValue != 'Postman' && useEnvironment) || selectedPlatformValue == 'Swagger2'" :label="$t('api_test.environment.environment_config')" prop="environmentId">
<el-form-item v-if="useEnvironment || selectedPlatformValue == 'Swagger2'" :label="$t('api_test.environment.environment_config')" prop="environmentId">
<el-select v-if="showEnvironmentSelect" size="small" v-model="formData.environmentId" class="environment-select" clearable>
<el-option v-for="(environment, index) in environments" :key="index" :label="environment.name + ': ' + environment.protocol + '://' + environment.socket" :value="environment.id"/>
<el-button class="environment-button" size="mini" type="primary" @click="openEnvironmentConfig">{{$t('api_test.environment.environment_config')}}</el-button>
@ -39,7 +39,7 @@
</template>
</el-select>
</el-form-item>
<el-form-item v-if="selectedPlatformValue == 'Metersphere'" prop="useEnvironment">
<el-form-item v-if="selectedPlatformValue != 'Swagger2'" prop="useEnvironment">
<el-checkbox v-model="useEnvironment">{{$t('api_test.environment.config_environment')}}</el-checkbox>
</el-form-item>