fix(接口测试): 修复导入swagger中query参数带中文,导入失败的缺陷

--bug=1028500 --user=王孝刚
【接口测试】github#26025,swagger-url带中文导入metersphere失败
https://www.tapd.cn/55049933/s/1420806
This commit is contained in:
wxg0103 2023-09-25 17:05:31 +08:00 committed by wxg0103
parent 9125a69248
commit 7f7afdc566
2 changed files with 10 additions and 13 deletions

View File

@ -109,32 +109,30 @@ public class Swagger3Parser extends SwaggerAbstractParser {
} }
} }
// 设置 query 参数 // 设置 query 参数
if (!CollectionUtils.isEmpty(request.getArguments())) {
StringBuilder pathBuilder = new StringBuilder(); StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append(request.getSwaggerUrl()); pathBuilder.append(request.getSwaggerUrl());
if (!request.getSwaggerUrl().contains("?")) { if (StringUtils.isNotBlank(request.getSwaggerUrl()) && !request.getSwaggerUrl().contains("?")) {
pathBuilder.append("?"); pathBuilder.append("?");
} }
if (!CollectionUtils.isEmpty(request.getArguments())) {
for (KeyValue keyValue : request.getArguments()) { for (KeyValue keyValue : request.getArguments()) {
if (StringUtils.isNotBlank(keyValue.getName())) { if (StringUtils.isNotBlank(keyValue.getName())) {
AuthorizationValue authorizationValue = new AuthorizationValue(); AuthorizationValue authorizationValue = new AuthorizationValue();
authorizationValue.setType("query"); authorizationValue.setType("query");
authorizationValue.setKeyName(keyValue.getName()); authorizationValue.setKeyName(keyValue.getName());
try { try {
authorizationValue.setValue(keyValue.isUrlEncode() ? URLEncoder.encode(keyValue.getValue(), StandardCharsets.UTF_8) : keyValue.getValue()); authorizationValue.setValue(URLEncoder.encode(keyValue.getValue(), StandardCharsets.UTF_8));
} catch (Exception e) { } catch (Exception e) {
LogUtil.info("swagger3 url encode error: " + e); LogUtil.info("swagger3 url encode error: " + e);
} }
pathBuilder.append(keyValue.getName()).append("=").append(authorizationValue.getValue()).append("&"); pathBuilder.append(keyValue.getName()).append("=").append(authorizationValue.getValue()).append("&");
auths.add(authorizationValue);
}
} }
} }
request.setSwaggerUrl(pathBuilder.substring(0, pathBuilder.length() - 1)); request.setSwaggerUrl(pathBuilder.substring(0, pathBuilder.length() - 1));
}
return CollectionUtils.size(auths) == 0 ? null : auths; return CollectionUtils.size(auths) == 0 ? null : auths;
} }
private List<ApiDefinitionWithBLOBs> parseRequests(OpenAPI openAPI, ApiTestImportRequest importRequest) { private List<ApiDefinitionWithBLOBs> parseRequests(OpenAPI openAPI, ApiTestImportRequest importRequest) {
Paths paths = openAPI.getPaths(); Paths paths = openAPI.getPaths();

View File

@ -8,7 +8,7 @@
:data="environments" :item-operators="environmentOperators" :add-fuc="addEnvironment" :data="environments" :item-operators="environmentOperators" :add-fuc="addEnvironment"
:env-add-permission="ENV_CREATE" :env-add-permission="ENV_CREATE"
:delete-fuc="openDelEnv" @itemSelected="environmentSelected" ref="environmentItems"/> :delete-fuc="openDelEnv" @itemSelected="environmentSelected" ref="environmentItems"/>
<environment-edit :if-create="ifCreate" :environment="currentEnvironment" <environment-edit :if-create="ifCreate" :environment="currentEnvironment" v-if="visible"
ref="environmentEdit" :is-read-only="isReadOnly" ref="environmentEdit" :is-read-only="isReadOnly"
@confirm="save" :is-project="true" :key="currentEnvironment.id" @confirm="save" :is-project="true" :key="currentEnvironment.id"
@close="close" @refreshAfterSave="refresh"> @close="close" @refreshAfterSave="refresh">
@ -164,9 +164,7 @@ export default {
} }
}) })
this.$refs.environmentEdit._save(newEnvironment); this.$refs.environmentEdit._save(newEnvironment);
this.environments.unshift(newEnvironment); this.getEnvironments();
this.$refs.environmentItems.itemSelected(this.environments.length - 1, newEnvironment);
this.refresh();
}, },
validateEnvironment(environment) { validateEnvironment(environment) {
if (!this.$refs.environmentEdit.validate()) { if (!this.$refs.environmentEdit.validate()) {
@ -199,6 +197,7 @@ export default {
if (this.projectId) { if (this.projectId) {
this.result = getEnvironmentByProjectId(this.projectId).then(response => { this.result = getEnvironmentByProjectId(this.projectId).then(response => {
this.environments = response.data; this.environments = response.data;
this.currentEnvironment.id = null;
if (this.environments.length > 0) { if (this.environments.length > 0) {
if (this.selectEnvironmentId) { if (this.selectEnvironmentId) {
const index = this.environments.findIndex(e => e.id === this.selectEnvironmentId); const index = this.environments.findIndex(e => e.id === this.selectEnvironmentId);