fix: 兼容swagger旧路径并修复测试构建
This commit is contained in:
@@ -52,8 +52,10 @@ public static class SwaggerExtensions
|
|||||||
var provider = app.ApplicationServices.GetRequiredService<IApiVersionDescriptionProvider>();
|
var provider = app.ApplicationServices.GetRequiredService<IApiVersionDescriptionProvider>();
|
||||||
var settings = app.ApplicationServices.GetRequiredService<SwaggerDocumentSettings>();
|
var settings = app.ApplicationServices.GetRequiredService<SwaggerDocumentSettings>();
|
||||||
const string routePrefix = "api/docs";
|
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 = $"{routePrefix}/{{documentName}}/swagger.json"; });
|
||||||
|
app.UseSwagger(options => { options.RouteTemplate = $"{legacyRoutePrefix}/{{documentName}}/swagger.json"; });
|
||||||
app.UseSwaggerUI(options =>
|
app.UseSwaggerUI(options =>
|
||||||
{
|
{
|
||||||
options.RoutePrefix = routePrefix;
|
options.RoutePrefix = routePrefix;
|
||||||
@@ -67,6 +69,19 @@ public static class SwaggerExtensions
|
|||||||
// 2. 显示请求耗时
|
// 2. 显示请求耗时
|
||||||
options.DisplayRequestDuration();
|
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;
|
return app;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using TakeoutSaaS.Application.Dictionary.Contracts;
|
using TakeoutSaaS.Application.Dictionary.Contracts;
|
||||||
@@ -443,6 +444,7 @@ public sealed class DictionaryApiTests
|
|||||||
new DictionaryItemRepository(context),
|
new DictionaryItemRepository(context),
|
||||||
cache,
|
cache,
|
||||||
tenantProvider,
|
tenantProvider,
|
||||||
|
new HttpContextAccessor { HttpContext = new DefaultHttpContext() },
|
||||||
NullLogger<DictionaryCommandService>.Instance);
|
NullLogger<DictionaryCommandService>.Instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,6 +496,7 @@ public sealed class DictionaryApiTests
|
|||||||
cache,
|
cache,
|
||||||
tenantProvider,
|
tenantProvider,
|
||||||
currentUser,
|
currentUser,
|
||||||
|
new HttpContextAccessor { HttpContext = new DefaultHttpContext() },
|
||||||
NullLogger<DictionaryImportExportService>.Instance);
|
NullLogger<DictionaryImportExportService>.Instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user