From fb58115897c54c6c07340c4f5d75c929d066c1c1 Mon Sep 17 00:00:00 2001 From: Tom Kerkhove Date: Wed, 15 Jan 2020 23:11:26 +0100 Subject: [PATCH 1/3] Provide capability to ignore existing SwaggerOptions/SwaggerUiOptions --- .../DependencyInjection/SwaggerBuilderExtensions.cs | 12 ++++++++++-- .../SwaggerUIBuilderExtensions.cs | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs b/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs index 0d0db19dad..6a5a8cd4bf 100644 --- a/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs +++ b/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs @@ -9,9 +9,17 @@ public static class SwaggerBuilderExtensions { public static IApplicationBuilder UseSwagger( this IApplicationBuilder app, - Action setupAction = null) + Action setupAction = null, + bool ignoreExistingOptions = false) { - var options = app.ApplicationServices.GetService>()?.Value ?? new SwaggerOptions(); + SwaggerOptions options = new SwaggerOptions(); + + var existingSwaggerOptions = app.ApplicationServices.GetService>(); + if (ignoreExistingOptions == false && existingSwaggerOptions?.Value != null) + { + options = existingSwaggerOptions.Value; + } + setupAction?.Invoke(options); app.UseMiddleware(options); diff --git a/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs index 5b4fafe7a1..c66eaed475 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs @@ -9,9 +9,17 @@ public static class SwaggerUIBuilderExtensions { public static IApplicationBuilder UseSwaggerUI( this IApplicationBuilder app, - Action setupAction = null) + Action setupAction = null, + bool ignoreExistingOptions = false) { - var options = app.ApplicationServices.GetService>()?.Value ?? new SwaggerUIOptions(); + SwaggerUIOptions options = new SwaggerUIOptions(); + + var existingSwaggerOptions = app.ApplicationServices.GetService>(); + if (ignoreExistingOptions == false && existingSwaggerOptions?.Value != null) + { + options = existingSwaggerOptions.Value; + } + setupAction?.Invoke(options); app.UseMiddleware(options); From f9fd0772bbe5993d45b81e1db2bb5371e27c5770 Mon Sep 17 00:00:00 2001 From: Tom Kerkhove Date: Mon, 16 Mar 2020 21:59:00 +0100 Subject: [PATCH 2/3] Support for ReDoc Signed-off-by: Tom Kerkhove --- .../ReDocBuilderExtensions.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.ReDoc/ReDocBuilderExtensions.cs b/src/Swashbuckle.AspNetCore.ReDoc/ReDocBuilderExtensions.cs index 896526f31f..a3c80d9027 100644 --- a/src/Swashbuckle.AspNetCore.ReDoc/ReDocBuilderExtensions.cs +++ b/src/Swashbuckle.AspNetCore.ReDoc/ReDocBuilderExtensions.cs @@ -11,8 +11,16 @@ public static IApplicationBuilder UseReDoc( this IApplicationBuilder app, Action setupAction = null) { - var options = app.ApplicationServices.GetService>()?.Value ?? new ReDocOptions(); - setupAction?.Invoke(options); + var options = new ReDocOptions(); + if (setupAction != null) + { + setupAction(options); + } + else + { + options = app.ApplicationServices.GetRequiredService>().Value; + } + app.UseMiddleware(options); return app; From 2f89e260b0fc62a9808563d355d664bf94af20b8 Mon Sep 17 00:00:00 2001 From: Tom Kerkhove Date: Mon, 16 Mar 2020 21:59:35 +0100 Subject: [PATCH 3/3] Change to suggestion Signed-off-by: Tom Kerkhove --- .../SwaggerBuilderExtensions.cs | 14 +++++++------- .../SwaggerUIBuilderExtensions.cs | 18 +++++++++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs b/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs index 6a5a8cd4bf..416accbb4e 100644 --- a/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs +++ b/src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerBuilderExtensions.cs @@ -9,18 +9,18 @@ public static class SwaggerBuilderExtensions { public static IApplicationBuilder UseSwagger( this IApplicationBuilder app, - Action setupAction = null, - bool ignoreExistingOptions = false) + Action setupAction = null) { SwaggerOptions options = new SwaggerOptions(); - - var existingSwaggerOptions = app.ApplicationServices.GetService>(); - if (ignoreExistingOptions == false && existingSwaggerOptions?.Value != null) + if (setupAction != null) + { + setupAction(options); + } + else { - options = existingSwaggerOptions.Value; + options = app.ApplicationServices.GetRequiredService>().Value; } - setupAction?.Invoke(options); app.UseMiddleware(options); return app; diff --git a/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs index c66eaed475..971e24f21c 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIBuilderExtensions.cs @@ -9,18 +9,18 @@ public static class SwaggerUIBuilderExtensions { public static IApplicationBuilder UseSwaggerUI( this IApplicationBuilder app, - Action setupAction = null, - bool ignoreExistingOptions = false) + Action setupAction = null) { - SwaggerUIOptions options = new SwaggerUIOptions(); - - var existingSwaggerOptions = app.ApplicationServices.GetService>(); - if (ignoreExistingOptions == false && existingSwaggerOptions?.Value != null) + var options = new SwaggerUIOptions(); + if (setupAction != null) + { + setupAction(options); + } + else { - options = existingSwaggerOptions.Value; + options = app.ApplicationServices.GetRequiredService>().Value; } - - setupAction?.Invoke(options); + app.UseMiddleware(options); return app;