Skip to content
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

Programmatic Endpoint configuration not used in UseSwaggerForOcelotUI #104

Closed
rklfss opened this issue Jun 3, 2020 · 0 comments · Fixed by #105
Closed

Programmatic Endpoint configuration not used in UseSwaggerForOcelotUI #104

rklfss opened this issue Jun 3, 2020 · 0 comments · Fixed by #105
Assignees

Comments

@rklfss
Copy link
Contributor

rklfss commented Jun 3, 2020

Describe the bug
If you configure Swagger Endpoints dynamically in ConfigureServices(IServiceCollection services), those are not used when calling UseSwaggerForOcelotUI and you are getting the error "SwaggerEndPoints configuration section is missing or empty".

services.Configure<List<SwaggerEndPointOptions>>(cfg =>
{
    cfg ??= new List<SwaggerEndPointOptions>();

    foreach (var (name, svc) in _appSettings.Services)
    {
        cfg.Add(new SwaggerEndPointOptions
        {
            Key = name,
            Config = {
                new SwaggerEndpointConfig
                {
                    Name = svc.Title,
                    Version = "v1",
                    Url = $"{svc.Url}/swagger/v1/swagger.json",
                },
            },
        });
    }
});

Expected behavior
The programmatic configuration is used.

Possible fix

Do not re-read the configuration section in UseSwaggerForOcelotUI and read from configured options instead.

public static IApplicationBuilder UseSwaggerForOcelotUI(
    this IApplicationBuilder app,
    // no need to use a configuration section here
    Action<SwaggerForOcelotUIOptions> setupAction)
{
    var options = app.ApplicationServices.GetService<IOptions<SwaggerForOcelotUIOptions>>().Value;
    setupAction?.Invoke(options);
    UseSwaggerForOcelot(app, options);

    app.UseSwaggerUI(c =>
    {
        InitUIOption(c, options);
        IEnumerable<SwaggerEndPointOptions> endPoints = app.ApplicationServices.GetService<IOptions<List<SwaggerEndPointOptions>>>().Value;
        AddSwaggerEndPoints(c, endPoints, options.DownstreamSwaggerEndPointBasePath);
    });

    return app;
}

This also works for file based configuration as the options are already configured in AddSwaggerForOcelot().

rklfss pushed a commit to rklfss/MMLib.SwaggerForOcelot that referenced this issue Jun 3, 2020
Use configured options in UseSwaggerForOcelotUI to allow programmatic configuration of services.

Overloads of UseSwaggerForOcelotUI with IConfiguration parameter become deprecated.

Fixes Burgyn#104
@ghost ghost added the Pending PR label Jun 3, 2020
Burgyn pushed a commit that referenced this issue Jun 4, 2020
* Allow programmatic configuration of SwaggerEndpoints

Use configured options in UseSwaggerForOcelotUI to allow programmatic configuration of services.

Overloads of UseSwaggerForOcelotUI with IConfiguration parameter become deprecated.

Fixes #104

* Updated Readme to use new UseSwaggerForOcelotUI

Updated Readme to use UseSwaggerForOcelotUI without configuration parameter

Co-authored-by: Robin Kaulfuß <[email protected]>
@ghost ghost removed the Pending PR label Jun 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants