ABP中关于Swagger的一些配置

Abp 集成 Swagger 官方文档, 请参考 Swagger Integration
AspNetCore 配置 Swagger, 请参考 Swashbuckle.AspNetCore
本文的项目环境是 AspNetCore 6.0 + Volo.Abp.Swashbuckle 6.0.2

Abp 中默认的基础配置如下:

C# 复制代码
public override void ConfigureServices(ServiceConfigurationContext context)
{
    var services = context.Services;
    services.AddAbpSwaggerGen(
        options =>
        {
            options.SwaggerDoc("v1", new OpenApiInfo { Title = "Test API", Version = "v1" });
            options.DocInclusionPredicate((docName, description) => true);
            options.CustomSchemaIds(type => type.FullName);
        }
    );
}

这样的配置,很难满足我们的需求,比如它默认显示了 Abp 相关的 endpoints 和 schema, 没有详细的接口注释等

隐藏 Abp 相关的 endpoints

Abp 官方文档 提及了这个操作,代码如下

C# 复制代码
services.AddAbpSwaggerGen(
    options =>
    {
        options.HideAbpEndpoints();
    }
);

隐藏 Abp 相关的 schemas

这个在官网中没有发现,搜索到可以实现自定义的 ISchemaFilter

参考: Hide Endpoints And Schemas from Swagger / OpenAPI

C# 复制代码
public class HideAbpSchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        context.SchemaRepository.Schemas.RemoveAll(item => item.Key.StartsWith("Volo."));
    }
}

//使用方法
services.AddAbpSwaggerGen(
    options =>
    {
        options.SchemaFilter<HideAbpSchemaFilter>();
    }
);

隐藏 Abp 默认生成的响应类型

Abp 默认生成了 400,401,403,404,500,501 相关的响应

  • 通过 AbpAspNetCoreMvcModule 这个模块的源码,我们看到了它的默认实现如下:
C# 复制代码
Configure<AbpRemoteServiceApiDescriptionProviderOptions>(options =>
{
    var statusCodes = new List<int>
    {
        (int) HttpStatusCode.Forbidden,
        (int) HttpStatusCode.Unauthorized,
        (int) HttpStatusCode.BadRequest,
        (int) HttpStatusCode.NotFound,
        (int) HttpStatusCode.NotImplemented,
        (int) HttpStatusCode.InternalServerError
    };

    options.SupportedResponseTypes.AddIfNotContains(statusCodes.Select(statusCode => new ApiResponseType
    {
        Type = typeof(RemoteServiceErrorResponse),
        StatusCode = statusCode
    }));
});

那就很好解决了,我们只要把它给清除就行了,代码如下

C# 复制代码
Configure<AbpRemoteServiceApiDescriptionProviderOptions>(options =>
{
    options.SupportedResponseTypes.Clear();
});

接口注释

这个简单,只要包含项目的 XML 文档注释就行

C# 复制代码
var xmlFilename1 = "EOA.User.WebApi.xml";
var xmlFilename2 = "EOA.User.Application.xml";
var xmlFilename3 = "EOA.User.Application.Contracts.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename1));
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename2));
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename3));

别忘了开启生成项目的文档注释(可以直接编辑.csproj 文件)

复制代码
<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

修改 Schema 默认的时间格式

直接全局修改 DateTime 类型的 Schema 配置即可,给个默认的 Example

C# 复制代码
options.MapType<DateTime>(() => new OpenApiSchema { Type = "string", Example = new Microsoft.OpenApi.Any.OpenApiString("2000-01-01 00:00:00") });

结束

本文也是实际记录我发现的一点小问题, 这么一顿操作下来是不是清爽多了

相关推荐
一念一花一世界5 小时前
接口管理工具选型:Postman、Swagger与PostIn的全面对比指南
测试工具·postman·swagger·接口管理工具
西部森林牧歌14 天前
Postman、swagger、PostIn接口管理工具详细纵评
postman·swagger·postin·接口管理工具
西部森林牧歌15 天前
接口管理工具选型指南:swagger与PostIn深度对比
swagger·postin·tiklab devops·接口管理工具
wangmengxxw17 天前
Swagger技术
java·swagger
猿与禅18 天前
SpringBoot 2.x 升级到 3.x 时 Swagger 迁移完整指南
swagger·springboot升级·springboot3.0·swagger3.0
東雪木22 天前
Spring Boot 2.x 集成 Knife4j (OpenAPI 3) 完整操作指南
java·spring boot·后端·swagger·knife4j·java异常处理
一念一花一世界23 天前
swagger和PostIn,开源免费接口管理工具选型指南
api·swagger·postin·接口管理工具
赵庆明老师1 个月前
ASP.NET Core 9 Web Api 启用 Swagger
swagger·dotnet
一念一花一世界1 个月前
Postman vs swagger vs PostIn,接口管理工具一文纵评
postman·swagger·postin·接口管理工具
咖啡Beans2 个月前
SpringBoot2.7集成Swagger3.0
java·swagger