swagger add document and operation for a generic controller

断了今生、忘了曾经 提交于 2021-01-28 09:22:42

问题


i am working on a webapi with plugins. I want to update the swagger documentation when a plugin is added. I can handle this when i don't use version. All methods are added to the swagger documentation.

but when the api has apiversion turned on, the generation of the new version swagger document failed. It returns a 404.

do i need to so anything for versioning to work and pick up the dynamic controller functions...

 private string AddSwaggerVersionDocument(PluginMetadata metadata)
        {

            var version = metadata.Version.ToApiVersion();

            if (SwaggerElements.GeneratorOptions.SwaggerGeneratorOptions.SwaggerDocs.ContainsKey(version) == false)
            {
                SwaggerElements.GeneratorOptions.SwaggerDoc(version, new Info
                {

                    Title = "webapi API",
                    Version = $"{version}",
                    Description = "Web API demo",
                    TermsOfService = "None",
                    Contact = new Contact
                    {
                        Name = "Frans van Ek",
                        Email = string.Empty,
                        Url = "https://fransvanek.nl"
                    },
                    License = new License
                    {
                        Name = "Use under LICX",
                        Url = "https://fransvanek.nl"
                    }
                });

                 SwaggerElements.UIOptions.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"My API : {version}");
            }
            return version;
        } 

回答1:


I see now. You're using your own versioning mechanism and generating Swagger documents on-demand. Swashbuckle expects everything to be defined upfront. This is reasonable as the supported versions should be deterministic at the start of the application. If your application is completely dynamic, then your current solution will work, but can vary between invocations. If plug-ins are discovered and loaded ahead of time, then you can register an IConfigureOptions<SwaggerGenOptions> that configures Swashbuckle with your plugin information. Something like:

public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
{
    readonly IApiDescriptionGroupCollectionProvider provider;

    public ConfigureSwaggerOptions(
        IApiDescriptionGroupCollectionProvider  provider ) => this.provider = provider;

    public void Configure( SwaggerGenOptions options )
    {
        // TODO: configure swashbuckler with plug-in information
    }
}

And then register it in the service container with:

services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();

I hope that helps



来源:https://stackoverflow.com/questions/53762697/swagger-add-document-and-operation-for-a-generic-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!