From abd5752ad1fea9e6ecf80d4327f355aedf3c1765 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 6 Jan 2026 06:46:19 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=BC=E5=AE=B9swagger=E6=97=A7?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E5=B9=B6=E4=BF=AE=E5=A4=8D=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...napi-forlinux => build-adminapi-forlinux.sh} | 0 .../Swagger/SwaggerExtensions.cs | 17 ++++++++++++++++- .../App/Dictionary/DictionaryApiTests.cs | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) rename scripts/{build-adminapi-forlinux => build-adminapi-forlinux.sh} (100%) diff --git a/scripts/build-adminapi-forlinux b/scripts/build-adminapi-forlinux.sh similarity index 100% rename from scripts/build-adminapi-forlinux rename to scripts/build-adminapi-forlinux.sh diff --git a/src/Core/TakeoutSaaS.Shared.Web/Swagger/SwaggerExtensions.cs b/src/Core/TakeoutSaaS.Shared.Web/Swagger/SwaggerExtensions.cs index 48021b1..0cfe43b 100644 --- a/src/Core/TakeoutSaaS.Shared.Web/Swagger/SwaggerExtensions.cs +++ b/src/Core/TakeoutSaaS.Shared.Web/Swagger/SwaggerExtensions.cs @@ -52,8 +52,10 @@ public static class SwaggerExtensions var provider = app.ApplicationServices.GetRequiredService(); var settings = app.ApplicationServices.GetRequiredService(); const string routePrefix = "api/docs"; - // 1. 注册 Swagger 中间件 + const string legacyRoutePrefix = "swagger"; + // 1. 注册 Swagger 中间件(新旧入口同时支持) app.UseSwagger(options => { options.RouteTemplate = $"{routePrefix}/{{documentName}}/swagger.json"; }); + app.UseSwagger(options => { options.RouteTemplate = $"{legacyRoutePrefix}/{{documentName}}/swagger.json"; }); app.UseSwaggerUI(options => { options.RoutePrefix = routePrefix; @@ -67,6 +69,19 @@ public static class SwaggerExtensions // 2. 显示请求耗时 options.DisplayRequestDuration(); }); + app.UseSwaggerUI(options => + { + options.RoutePrefix = legacyRoutePrefix; + foreach (var description in provider.ApiVersionDescriptions) + { + // 3. 使用相对路径适配反向代理/网关前缀 + options.SwaggerEndpoint( + $"./{description.GroupName}/swagger.json", + $"{settings.Title} {description.ApiVersion}"); + } + // 2. 显示请求耗时 + options.DisplayRequestDuration(); + }); return app; } } diff --git a/tests/TakeoutSaaS.Integration.Tests/App/Dictionary/DictionaryApiTests.cs b/tests/TakeoutSaaS.Integration.Tests/App/Dictionary/DictionaryApiTests.cs index 176317a..dde9318 100644 --- a/tests/TakeoutSaaS.Integration.Tests/App/Dictionary/DictionaryApiTests.cs +++ b/tests/TakeoutSaaS.Integration.Tests/App/Dictionary/DictionaryApiTests.cs @@ -1,6 +1,7 @@ using System.Text; using System.Text.Json; using FluentAssertions; +using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging.Abstractions; using TakeoutSaaS.Application.Dictionary.Contracts; @@ -443,6 +444,7 @@ public sealed class DictionaryApiTests new DictionaryItemRepository(context), cache, tenantProvider, + new HttpContextAccessor { HttpContext = new DefaultHttpContext() }, NullLogger.Instance); } @@ -494,6 +496,7 @@ public sealed class DictionaryApiTests cache, tenantProvider, currentUser, + new HttpContextAccessor { HttpContext = new DefaultHttpContext() }, NullLogger.Instance); }