From 75f9488ba3b0a2f42bb8c945c506291f5292e6c9 Mon Sep 17 00:00:00 2001 From: Jianguo-Genius Date: Wed, 18 Sep 2024 17:11:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20s?= =?UTF-8?q?wagger=E5=AF=BC=E5=85=A5=E5=A2=9E=E5=8A=A0token=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=1016360 --user=宋天阳 【Bug转需求】【接口测试】导入DataEase接口-swagger格式-url-导入失败 https://www.tapd.cn/55049933/s/1580083 --- .../metersphere/api/dto/definition/ApiScheduleDTO.java | 2 ++ .../api/dto/definition/SwaggerBasicAuth.java | 3 +++ .../api/dto/definition/request/ApiScheduleRequest.java | 1 + .../io/metersphere/api/dto/request/ImportRequest.java | 2 ++ .../api/parser/api/Swagger3ParserApiDefinition.java | 10 ++++++++++ .../definition/ApiDefinitionScheduleService.java | 2 ++ frontend/src/models/apiTest/management.ts | 1 + .../views/api-test/management/components/import.vue | 9 +++++++++ 8 files changed, 30 insertions(+) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiScheduleDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiScheduleDTO.java index 869070f888..196df64237 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiScheduleDTO.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiScheduleDTO.java @@ -17,6 +17,8 @@ public class ApiScheduleDTO { private String moduleId; @Schema(description = "swagger地址", requiredMode = Schema.RequiredMode.REQUIRED) private String swaggerUrl; + @Schema(description = "swagger token", requiredMode = Schema.RequiredMode.REQUIRED) + private String swaggerToken; @Schema(description = "是否覆盖模块") private Boolean coverModule = false; @Schema(description = "是否同步导入用例") diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/SwaggerBasicAuth.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/SwaggerBasicAuth.java index 8679b412ba..a207a8d500 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/SwaggerBasicAuth.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/SwaggerBasicAuth.java @@ -7,4 +7,7 @@ public class SwaggerBasicAuth { private Boolean authSwitch = false; private String userName; private String password; + + //新增token设置。放在这里也是因为token是身份验证的一种。 + private String token; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/request/ApiScheduleRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/request/ApiScheduleRequest.java index 315c684192..7bb4cf4537 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/request/ApiScheduleRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/request/ApiScheduleRequest.java @@ -31,6 +31,7 @@ public class ApiScheduleRequest { @NotBlank(message = "{api_definition_swagger.swagger_url.not_blank}", groups = {Created.class, Updated.class}) @Size(min = 1, max = 500, message = "{api_definition_swagger.swagger_url.length_range}", groups = {Created.class, Updated.class}) private String swaggerUrl; + private String swaggerToken; private String taskId; @Schema(description = "是否覆盖模块") private Boolean coverModule = false; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ImportRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ImportRequest.java index 8ed7b294ce..d6431e2c87 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ImportRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ImportRequest.java @@ -14,6 +14,8 @@ public class ImportRequest { private String projectId; @Schema(description = "导入的swagger地址") private String swaggerUrl; + @Schema(description = "导入的swagger token") + private String swaggerToken; @Schema(description = "如果是定时任务的时候 需要传入创建人id", requiredMode = Schema.RequiredMode.REQUIRED) private String userId; private String versionId; // 新导入选择的版本 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3ParserApiDefinition.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3ParserApiDefinition.java index 74b43c2d27..b1da06602b 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3ParserApiDefinition.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3ParserApiDefinition.java @@ -118,6 +118,16 @@ public class Swagger3ParserApiDefinition extends HttpApiDefinitionImportAbstract authorizationValue.setValue(authValue); auths.add(authorizationValue); } + + // 设置 headers + if (StringUtils.isNotBlank(request.getSwaggerToken())) { + AuthorizationValue authorizationValue = new AuthorizationValue(); + authorizationValue.setType(HEADER); + authorizationValue.setKeyName("token"); + authorizationValue.setValue(request.getSwaggerToken()); + auths.add(authorizationValue); + } + return CollectionUtils.size(auths) == 0 ? null : auths; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionScheduleService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionScheduleService.java index 31c0779477..a23f0c4974 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionScheduleService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionScheduleService.java @@ -61,6 +61,7 @@ public class ApiDefinitionScheduleService { basicAuth.setUserName(request.getAuthUsername()); basicAuth.setPassword(request.getAuthPassword()); basicAuth.setAuthSwitch(request.isAuthSwitch()); + basicAuth.setToken(request.getSwaggerToken()); apiSwagger.setConfig(ApiDataUtils.toJSONString(basicAuth)); apiDefinitionSwaggerMapper.insertSelective(apiSwagger); @@ -166,6 +167,7 @@ public class ApiDefinitionScheduleService { apiScheduleDTO.setAuthUsername(basicAuth.getUserName()); apiScheduleDTO.setAuthPassword(basicAuth.getPassword()); apiScheduleDTO.setAuthSwitch(basicAuth.getAuthSwitch()); + apiScheduleDTO.setSwaggerToken(basicAuth.getToken()); } apiScheduleDTO.setEnable(schedule.getEnable()); apiScheduleDTO.setValue(schedule.getValue()); diff --git a/frontend/src/models/apiTest/management.ts b/frontend/src/models/apiTest/management.ts index 272cdb1972..e241205af1 100644 --- a/frontend/src/models/apiTest/management.ts +++ b/frontend/src/models/apiTest/management.ts @@ -237,6 +237,7 @@ export interface ImportApiDefinitionRequest { uniquelyIdentifies?: string; resourceId?: string; swaggerUrl?: string; + swaggerToken?: string; moduleId: string; projectId: string; name?: string; diff --git a/frontend/src/views/api-test/management/components/import.vue b/frontend/src/views/api-test/management/components/import.vue index 96e7d7847d..d125215c60 100644 --- a/frontend/src/views/api-test/management/components/import.vue +++ b/frontend/src/views/api-test/management/components/import.vue @@ -157,6 +157,9 @@ allow-clear > + + +
{{ t('apiTestManagement.basicAuth') }} @@ -224,6 +227,9 @@ allow-clear > + + +
{{ t('apiTestManagement.basicAuth') }} @@ -415,6 +421,7 @@ syncMock: true, coverModule: false, swaggerUrl: '', + swaggerToken: '', authSwitch: false, authUsername: '', authPassword: '', @@ -519,6 +526,7 @@ protocol: importForm.value.protocol, moduleId: importForm.value.moduleId, swaggerUrl: importForm.value.swaggerUrl, + swaggerToken: importForm.value.swaggerToken, authSwitch: importForm.value.authSwitch, authUsername: importForm.value.authUsername, authPassword: importForm.value.authPassword, @@ -552,6 +560,7 @@ protocol: importForm.value.protocol, moduleId: importForm.value.moduleId, swaggerUrl: importForm.value.swaggerUrl, + swaggerToken: importForm.value.swaggerToken, authSwitch: importForm.value.authSwitch, authUsername: importForm.value.authUsername, authPassword: importForm.value.authPassword,