diff --git a/src/NSwag.AspNet.Owin/Middlewares/SwaggerUiIndexMiddleware.cs b/src/NSwag.AspNet.Owin/Middlewares/SwaggerUiIndexMiddleware.cs index 6dfaa50c64..7ac6f233c9 100644 --- a/src/NSwag.AspNet.Owin/Middlewares/SwaggerUiIndexMiddleware.cs +++ b/src/NSwag.AspNet.Owin/Middlewares/SwaggerUiIndexMiddleware.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading; using System.Threading.Tasks; using Microsoft.Owin; using NSwag.Generation; @@ -30,7 +31,7 @@ public override async Task Invoke(IOwinContext context) { context.Response.Headers["Content-Type"] = "text/html; charset=utf-8"; context.Response.StatusCode = 200; - context.Response.Write(_settings.TransformHtml(reader.ReadToEnd(), context.Request)); + context.Response.Write(await _settings.TransformHtmlAsync(reader.ReadToEnd(), context.Request, CancellationToken.None)); } } else diff --git a/src/NSwag.AspNetCore/ApiverseUiSettings.cs b/src/NSwag.AspNetCore/ApiverseUiSettings.cs index ec546dc26b..548efafbeb 100644 --- a/src/NSwag.AspNetCore/ApiverseUiSettings.cs +++ b/src/NSwag.AspNetCore/ApiverseUiSettings.cs @@ -7,6 +7,8 @@ //----------------------------------------------------------------------- using Microsoft.AspNetCore.Http; +using System.Threading; +using System.Threading.Tasks; namespace NSwag.AspNetCore { @@ -31,9 +33,9 @@ public ApimundoUiSettings() /// public string ApimundoUrl { get; set; } = "https://apimundo.com"; - internal override string TransformHtml(string html, HttpRequest request) + internal override Task TransformHtmlAsync(string html, HttpRequest request, CancellationToken cancellationToken) { - return html; + return Task.FromResult(html); } } } diff --git a/src/NSwag.AspNetCore/Middlewares/SwaggerUiIndexMiddleware.cs b/src/NSwag.AspNetCore/Middlewares/SwaggerUiIndexMiddleware.cs index 6a2fc82d6f..2889168a43 100644 --- a/src/NSwag.AspNetCore/Middlewares/SwaggerUiIndexMiddleware.cs +++ b/src/NSwag.AspNetCore/Middlewares/SwaggerUiIndexMiddleware.cs @@ -30,7 +30,11 @@ public async Task Invoke(HttpContext context) { context.Response.Headers["Content-Type"] = "text/html; charset=utf-8"; context.Response.StatusCode = 200; - await context.Response.WriteAsync(_settings.TransformHtml(await reader.ReadToEndAsync(), context.Request)); + await context.Response.WriteAsync( + await _settings.TransformHtmlAsync( + await reader.ReadToEndAsync(), + context.Request, + context.RequestAborted)); } } else diff --git a/src/NSwag.AspNetCore/ReDocSettings.cs b/src/NSwag.AspNetCore/ReDocSettings.cs index fad349d2ed..938be6e972 100644 --- a/src/NSwag.AspNetCore/ReDocSettings.cs +++ b/src/NSwag.AspNetCore/ReDocSettings.cs @@ -8,6 +8,8 @@ using NSwag.Generation; using System.Collections.Generic; +using System.Threading.Tasks; +using System.Threading; #if AspNetOwin using Microsoft.Owin; @@ -30,15 +32,15 @@ public class ReDocSettings : SwaggerUiSettingsBase public IDictionary AdditionalSettings { get; } = new Dictionary(); #if AspNetOwin - internal override string TransformHtml(string html, IOwinRequest request) + internal override Task TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken) #else - internal override string TransformHtml(string html, HttpRequest request) + internal override Task TransformHtmlAsync(string html, HttpRequest request, CancellationToken cancellationToken) #endif { html = html.Replace("{AdditionalSettings}", GenerateAdditionalSettings(AdditionalSettings)); html = html.Replace("{CustomStyle}", GetCustomStyleHtml(request)); html = html.Replace("{CustomScript}", GetCustomScriptHtml(request)); - return html; + return Task.FromResult(html); } } } \ No newline at end of file diff --git a/src/NSwag.AspNetCore/SwaggerUi3Settings.cs b/src/NSwag.AspNetCore/SwaggerUi3Settings.cs index 88099b0ff4..6855bf969b 100644 --- a/src/NSwag.AspNetCore/SwaggerUi3Settings.cs +++ b/src/NSwag.AspNetCore/SwaggerUi3Settings.cs @@ -15,6 +15,8 @@ using Newtonsoft.Json; using NSwag.Generation; using NJsonSchema; +using System.Threading; +using System.Threading.Tasks; #if AspNetOwin using Microsoft.Owin; @@ -121,12 +123,19 @@ public bool WithCredentials /// Gets or sets the Swagger URL routes (must start with '/', hides SwaggerRoute). public ICollection SwaggerRoutes { get; } = new List(); + /// Gets or sets the Swagger URL routes factory (SwaggerRoutes is ignored when set). +#if AspNetOwin + public Func>> SwaggerRoutesFactory { get; set; } +#else + public Func>> SwaggerRoutesFactory { get; set; } +#endif + internal override string ActualSwaggerDocumentPath => SwaggerRoutes.Any() ? "" : base.ActualSwaggerDocumentPath; #if AspNetOwin - internal override string TransformHtml(string html, IOwinRequest request) + internal override async Task TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken) #else - internal override string TransformHtml(string html, HttpRequest request) + internal override async Task TransformHtmlAsync(string html, HttpRequest request, CancellationToken cancellationToken) #endif { var htmlBuilder = new StringBuilder(html); @@ -148,11 +157,15 @@ internal override string TransformHtml(string html, HttpRequest request) } } - htmlBuilder.Replace("{Urls}", !SwaggerRoutes.Any() + var swaggerRoutes = SwaggerRoutesFactory != null ? + (await SwaggerRoutesFactory(request, cancellationToken)).ToList() : + SwaggerRoutes; + + htmlBuilder.Replace("{Urls}", !swaggerRoutes.Any() ? "undefined" : JsonConvert.SerializeObject( #pragma warning disable 618 - SwaggerRoutes.Select(r => new SwaggerUi3Route(r.Name, + swaggerRoutes.Select(r => new SwaggerUi3Route(r.Name, TransformToExternalPath(r.Url.Substring(MiddlewareBasePath?.Length ?? 0), request))) #pragma warning restore 618 )); diff --git a/src/NSwag.AspNetCore/SwaggerUiSettings.cs b/src/NSwag.AspNetCore/SwaggerUiSettings.cs index deef42c06f..430b82efb9 100644 --- a/src/NSwag.AspNetCore/SwaggerUiSettings.cs +++ b/src/NSwag.AspNetCore/SwaggerUiSettings.cs @@ -11,6 +11,8 @@ using System.Text; using Newtonsoft.Json; using NSwag.Generation; +using System.Threading.Tasks; +using System.Threading; #if AspNetOwin using Microsoft.Owin; @@ -52,9 +54,9 @@ public class SwaggerUiSettings : SwaggerUiSettingsBase public bool ShowRequestHeaders { get; set; } = false; #if AspNetOwin - internal override string TransformHtml(string html, IOwinRequest request) + internal override Task TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken) #else - internal override string TransformHtml(string html, HttpRequest request) + internal override Task TransformHtmlAsync(string html, HttpRequest request, CancellationToken cancellationToken) #endif { var htmlBuilder = new StringBuilder(html); @@ -74,7 +76,7 @@ internal override string TransformHtml(string html, HttpRequest request) .Replace("{CustomStyle}", GetCustomStyleHtml(request)) .Replace("{CustomScript}", GetCustomScriptHtml(request)); - return htmlBuilder.ToString(); + return Task.FromResult(htmlBuilder.ToString()); } } } \ No newline at end of file diff --git a/src/NSwag.AspNetCore/SwaggerUiSettingsBase.cs b/src/NSwag.AspNetCore/SwaggerUiSettingsBase.cs index 1f058dcb37..ee5df125bd 100644 --- a/src/NSwag.AspNetCore/SwaggerUiSettingsBase.cs +++ b/src/NSwag.AspNetCore/SwaggerUiSettingsBase.cs @@ -14,6 +14,8 @@ using Newtonsoft.Json; using System.Linq; using System.Text; +using System.Threading.Tasks; +using System.Threading; #if AspNetOwin using Microsoft.Owin; @@ -62,11 +64,11 @@ public SwaggerUiSettingsBase() #if AspNetOwin public Func TransformToExternalPath { get; set; } - internal abstract string TransformHtml(string html, IOwinRequest request); + internal abstract Task TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken); #else public Func TransformToExternalPath { get; set; } - internal abstract string TransformHtml(string html, HttpRequest request); + internal abstract Task TransformHtmlAsync(string html, HttpRequest request, CancellationToken cancellationToken); #endif ///