From a135ea696cfe49bdac2d10e4df111220a0152418 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Thu, 30 May 2024 15:56:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dswagger=20url=E6=97=A0=E6=B3=95=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E6=97=B6=EF=BC=8C=E6=97=A0=E6=B3=95=E7=BB=93=E6=9D=9F?= =?UTF-8?q?=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1041489 --user=王孝刚 【接口测试】接口swagger导入-当前服务无法访问导入url时-一直导入中-无任何提示 https://www.tapd.cn/55049933/s/1523705 --- .../main/resources/i18n/commons.properties | 2 ++ .../resources/i18n/commons_en_US.properties | 4 +++- .../resources/i18n/commons_zh_CN.properties | 4 +++- .../resources/i18n/commons_zh_TW.properties | 4 +++- .../ApiDefinitionImportUtilService.java | 22 +++++++++++++++++++ 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/backend/framework/sdk/src/main/resources/i18n/commons.properties b/backend/framework/sdk/src/main/resources/i18n/commons.properties index 5bcadfd747..533c38a467 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons.properties @@ -541,3 +541,5 @@ current_user_can_not_operation_api_key=当前用户无操作该ApiKey的权限 #全局请求头 global_request_header=全局请求头 + +url_format_error=请检查Swagger URL是否输入正确! diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties b/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties index 60add6a40c..e1cff4ca04 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties @@ -578,4 +578,6 @@ api_key_not_exist=API key does not exist current_user_can_not_operation_api_key=The current user cannot operate the API key #全局请求头 -global_request_header=Global request header \ No newline at end of file +global_request_header=Global request header + +url_format_error=Please check if the Swagger URL is entered correctly! diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties b/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties index 7c9e7eb9ba..e19d30d4f9 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties @@ -575,4 +575,6 @@ api_key_not_exist=ApiKey不存在 current_user_can_not_operation_api_key=当前用户无操作该ApiKey的权限 #全局请求头 -global_request_header=全局请求头 \ No newline at end of file +global_request_header=全局请求头 + +url_format_error=请检查Swagger URL是否输入正确! \ No newline at end of file diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties b/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties index 2852fc1006..6e032ada38 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties @@ -575,4 +575,6 @@ api_key_not_exist=ApiKey不存在 current_user_can_not_operation_api_key=當前用戶無法操作ApiKey #全局请求头 -global_request_header=全局請求頭 \ No newline at end of file +global_request_header=全局請求頭 + +url_format_error=请检查Swagger URL是否输入正确! \ No newline at end of file diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java index ea256e3e3f..191fda1d33 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java @@ -57,6 +57,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; +import java.net.HttpURLConnection; +import java.net.URI; import java.util.*; import java.util.stream.Collectors; @@ -112,6 +114,9 @@ public class ApiDefinitionImportUtilService { if (StringUtils.equals(request.getType(), "SCHEDULE")) { request.setProtocol(ModuleConstants.NODE_PROTOCOL_HTTP); } + if (StringUtils.equals(request.getPlatform(), ApiImportPlatform.Swagger3.name()) && StringUtils.isNotBlank(request.getSwaggerUrl())) { + testUrlTimeout(request.getSwaggerUrl(), 30000); + } try { apiImport = (ApiDefinitionImport) Objects.requireNonNull(runService).parse(file == null ? null : file.getInputStream(), request); //TODO 处理mock数据 @@ -128,6 +133,23 @@ public class ApiDefinitionImportUtilService { } } + public static void testUrlTimeout(String address, int timeOutMillSeconds) { + HttpURLConnection connection = null; + try { + URI uriObj = new URI(address); + connection = (HttpURLConnection) uriObj.toURL().openConnection(); + connection.setUseCaches(false); + connection.setConnectTimeout(timeOutMillSeconds); // 设置超时时间 + connection.connect(); // 建立连接 + } catch (Exception e) { + LogUtils.error(e); + throw new MSException(Translator.get("url_format_error")); + } finally { + if (connection != null) { + connection.disconnect(); // 关闭连接 + } + } + } public void checkFileSuffixName(ImportRequest request, String suffixName) { if (FILE_JMX.equalsIgnoreCase(suffixName)) {