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") });

结束

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

相关推荐
Nonullpoint.20 天前
《Spring Boot 集成 Swagger:打造高效接口文档与开发体验》
java·spring boot·后端·swagger
小谭の努力23 天前
Swagger的增强knife4j
spring boot·swagger·knife4j
万雅虎1 个月前
使用Kiota工具生成WebApi的代理类,以及接口调用的简单体验
webapi·aspnetcore·kiota
yc_12241 个月前
配置更加美观的 Swagger UI
swagger·webapi·useknife4ui
营赢盈英1 个月前
Azure OpenAI Swagger Validation Failure with APIM
ai·azure·swagger·azure api·azure manage
万雅虎2 个月前
使用 Alba 对 AspnetCore项目进行测试
单元测试·aspnetcore
wn5312 个月前
【协作提效 Go - gin ! swagger】
开发语言·后端·golang·gin·swagger
枫叶_v2 个月前
【SpringBoot】5 Swagger
java·spring boot·后端·swagger
冯浩(grow up)2 个月前
springboot2.6x 中使用Swagger3
java·swagger
软泡芙2 个月前
【框架】ABP(ASP.NET Boilerplate Project)
后端·asp.net·abp