Skip to content

Update API Versions auto-discovery sample #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cd21h opened this issue Dec 31, 2018 · 1 comment
Closed

Update API Versions auto-discovery sample #422

cd21h opened this issue Dec 31, 2018 · 1 comment

Comments

@cd21h
Copy link

cd21h commented Dec 31, 2018

Swashbuckle 4 uses IOptions pattern, so creating temporary container
is not required anymore.

This can be done using IConfigureOptions

    public interface ISwaggerVersionInfoProvider
    {
        Info GetVersionInfo(ApiVersionDescription apiDescription);
    }

    public class SwaggerVersionAutoDiscovery : IConfigureOptions<SwaggerGenOptions>
    {
        private readonly IApiVersionDescriptionProvider _versionDescriptionProvider;
        private readonly ISwaggerVersionInfoProvider _swaggerVersionInfoProvider;

        public SwaggerVersionAutoDiscovery(IApiVersionDescriptionProvider versionDescriptionProvider, 
            ISwaggerVersionInfoProvider swaggerVersionInfoProvider)
        {
            _versionDescriptionProvider = versionDescriptionProvider ?? throw new ArgumentNullException(nameof(versionDescriptionProvider));
            _swaggerVersionInfoProvider = swaggerVersionInfoProvider ?? throw new ArgumentNullException(nameof(swaggerVersionInfoProvider));
        }

        public void Configure(SwaggerGenOptions options)
        {
            foreach (var description in _versionDescriptionProvider.ApiVersionDescriptions)
            {
                options.SwaggerDoc(description.GroupName, _swaggerVersionInfoProvider.GetVersionInfo(description));
            }
        }
    }

Startup.cs code

// todo: register your ISwaggerVersionInfoProvider implementation
// services.Add(...);
// Add SwaggerGenOptions configurator
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, SwaggerVersionAutoDiscovery>();

@commonsensesoftware
Copy link
Collaborator

Ah … thanks for the tip. I've updated the examples. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants