在Swagger中自动生成对SwaggerDoc的调用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Swagger中自动生成对SwaggerDoc的调用相关的知识,希望对你有一定的参考价值。

    public void ConfigureServices(IServiceCollection services)
    
        services.AddControllers(); //this replaces these services.AddMvcCore().AddApiExplorer();
        ...
        services.AddSwaggerGen(options =>
        
            // make this automatic ???
            options.SwaggerDoc("v1", new Info  Version = "v1", Title = "v1 API", Description = "v1 API Description" );
            options.SwaggerDoc("v2", new Info  Version = "v2", Title = "v2 API", Description = "v2 API Description" );
            ...
            options.DocInclusionPredicate((version, desc) =>
            
                var versions = desc.CustomAttributes().OfType<ApiVersionAttribute>().SelectMany(attr => attr.Versions).ToArray();
                var maps = desc.CustomAttributes().OfType<MapToApiVersionAttribute>().SelectMany(attr => attr.Versions).ToArray();
                return versions.Any(v => $"vv.ToString()" == version) && (!maps.Any() || maps.Any(v => $"vv.ToString()" == version));
            );
        );
    

此代码可以正常工作。但是,为了使代码更加通用,可以自动调用SwaggerDoc吗?在DocInclusionPredicate参数的desc中,可以收集版本。

答案

[当您使用ApiVersionAttribute时,我假设您正在使用Microsoft.AspNetCore.Mvc.Versioning nuget包。程序包提供名为IApiVersionDescriptionProvider的服务。该服务提供所有检测到的API版本的枚举。然后,您可以将它们自动添加为swagger-doc。

services.AddSwaggerGen(options =>

    // you can use the IApiVersionDescriptionProvider
    var provider = services.BuildServiceProvider()
        .GetRequiredService<IApiVersionDescriptionProvider>();

    foreach (var description in provider.ApiVersionDescriptions)
    
        var info = new Info
        
            Title = $"My API description.ApiVersion",
            Version = description.ApiVersion.ToString(),
            Contact = new Contact
            
                Email = "info@mydomain.com",
                Name = "Foo Bar",
                Url = "https://thecatapi.com/"
            
        ;

        options.SwaggerDoc(description.GroupName, info);
    

    // instead of manually adding your versions
    //options.SwaggerDoc("v1", new Info  Version = "v1", Title = "v1 API", Description = "v1 API Description" );
    //options.SwaggerDoc("v2", new Info  Version = "v2", Title = "v2 API", Description = "v2 API Description" );
    options.DocInclusionPredicate((version, desc) =>
    
        var versions = desc.CustomAttributes().OfType<ApiVersionAttribute>().SelectMany(attr => attr.Versions).ToArray();
        var maps = desc.CustomAttributes().OfType<MapToApiVersionAttribute>().SelectMany(attr => attr.Versions).ToArray();
        return versions.Any(v => $"vv.ToString()" == version) && (!maps.Any() || maps.Any(v => $"vv.ToString()" == version));
    );
);

以上是关于在Swagger中自动生成对SwaggerDoc的调用的主要内容,如果未能解决你的问题,请参考以下文章

Swagger使用--在一个Controller中使用相同(类似)参数的方法

Swagger

当试图打开Swagger-UI时只获得一个json Response

django-rest-swagger对API接口注释

在 PHP 中自动生成 openAPI(swagger) 规范文件

重新整理 .net core 实践篇—————路由和终结点[二十三]