简体   繁体   English

有没有办法为最新版本 api 创建 swagger 文档?

[英]Is there way to create a swagger doc for latest version api?

I use dotnet tool called "swagger" to create a swagger json file that contains API spec for specific version.我使用名为“swagger”的 dotnet 工具创建 swagger json 文件,其中包含特定版本的 API 规范。 Command be like: swagger tofile --serializeasv2 --output rest-api.json C:\Service.dll v1命令类似于: swagger tofile --serializeasv2 --output rest-api.json C:\Service.dll v1

As you can see I have to pass swagger doc name v1 to export file.如您所见,我必须通过 swagger 文档名称v1才能导出文件。 The name is version of API.名称为 API 版本。 Is there a way to export the latest version?有没有办法导出最新版本?

I tried to create a swagger doc with name "latest" that contains the latest version of API but I couldn't.我试图创建一个名为“latest”的 swagger 文档,其中包含最新版本的 API,但我做不到。 Do anyone know how to create a swagger doc that display the latest version of API?有谁知道如何创建显示最新版本 API 的 swagger 文档?

v1 is the default name given by the code example from the Swashbuckle project : v1Swashbuckle 项目中的代码示例给出的默认名称:

services.AddMvc();

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});

It has no particular meaning as a version number beyond this - it's just what the code author has chosen to call it.除此之外,它作为版本号没有特别的意义——它只是代码作者选择的名称。 The 'latest version' is whatever the code currently specifies. “最新版本”是代码当前指定的任何内容。 Calling c.SwaggerDoc multiple times with different names would expose different Swagger documents and you could choose to make their name indicate a version - this is down to the project's Swashbuckle configuration.使用不同的名称多次调用c.SwaggerDoc会暴露不同的 Swagger 文档,您可以选择让它们的名称表示版本 - 这取决于项目的 Swashbuckle 配置。

Thanks every one for help.感谢每一个人的帮助。 I have found a solution.我找到了解决方案。

The main goal is to avoid changing the console command when new version API appears.主要目标是避免在出现新版本 API 时更改控制台命令。 I am talking about the last part v1 .我说的是最后一部分v1 If v2 doc appears, I have to pass it to the command.如果出现 v2 doc,我必须将它传递给命令。

Now I can pass latest value to the command and get rest-api.json that contains the latest version of API.现在我可以将latest值传递给命令并获取包含最新版本 API 的 rest-api.json。

swagger tofile --serializeasv2 --output rest-api.json C:\Service.dll latest

What I did:我做了什么:

In my project I have implemented a decorator for the service that implements ISwaggerProvider在我的项目中,我为实现ISwaggerProvider的服务实现了一个装饰器

public class SwaggerGeneratorDecorator: ISwaggerProvider
{
    private readonly ISwaggerProvider _decorated;
    private readonly IApiDescriptionGroupCollectionProvider _provider;

    public SwaggerGeneratorDecorator(ISwaggerProvider decorated, IApiDescriptionGroupCollectionProvider provider)
    {
        _decorated = decorated;
        _provider = provider;
    }
        
    public OpenApiDocument GetSwagger(string documentName, string host = null, string basePath = null)
    {
        if (documentName == "latest")
        {
            var group = _provider.ApiDescriptionGroups.Items.MaxBy(group => group.GroupName);
            documentName = group?.GroupName ?? documentName;
        }

        return _decorated.GetSwagger(documentName, host, basePath);
    }
}

Then register it in service collection.然后在服务集合中注册它。

services.Decorate<ISwaggerProvider, SwaggerGeneratorDecorator>();

Now, when I run the command with 'latest' parameter, I get generated file with the latest API version.现在,当我使用 'latest' 参数运行命令时,我会使用最新的 API 版本生成文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM