Skip to content

Commit

Permalink
Add SwaggerRoutesFactory to SwaggerUi3Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico Suter committed Aug 15, 2023
1 parent 8d120cd commit b819344
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Owin;
using NSwag.Generation;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/NSwag.AspNetCore/ApiverseUiSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//-----------------------------------------------------------------------

using Microsoft.AspNetCore.Http;
using System.Threading;
using System.Threading.Tasks;

namespace NSwag.AspNetCore
{
Expand All @@ -31,9 +33,9 @@ public ApimundoUiSettings()
/// </summary>
public string ApimundoUrl { get; set; } = "https://apimundo.com";

internal override string TransformHtml(string html, HttpRequest request)
internal override Task<string> TransformHtmlAsync(string html, HttpRequest request, CancellationToken cancellationToken)
{
return html;
return Task.FromResult(html);
}
}
}
6 changes: 5 additions & 1 deletion src/NSwag.AspNetCore/Middlewares/SwaggerUiIndexMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/NSwag.AspNetCore/ReDocSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

using NSwag.Generation;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
#if AspNetOwin
using Microsoft.Owin;

Expand All @@ -30,15 +32,15 @@ public class ReDocSettings : SwaggerUiSettingsBase
public IDictionary<string, object> AdditionalSettings { get; } = new Dictionary<string, object>();

#if AspNetOwin
internal override string TransformHtml(string html, IOwinRequest request)
internal override Task<string> TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken)
#else
internal override string TransformHtml(string html, HttpRequest request)
internal override Task<string> 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);
}
}
}
21 changes: 17 additions & 4 deletions src/NSwag.AspNetCore/SwaggerUi3Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using Newtonsoft.Json;
using NSwag.Generation;
using NJsonSchema;
using System.Threading;
using System.Threading.Tasks;

#if AspNetOwin
using Microsoft.Owin;
Expand Down Expand Up @@ -121,12 +123,19 @@ public bool WithCredentials
/// <summary>Gets or sets the Swagger URL routes (must start with '/', hides SwaggerRoute).</summary>
public ICollection<SwaggerUi3Route> SwaggerRoutes { get; } = new List<SwaggerUi3Route>();

/// <summary>Gets or sets the Swagger URL routes factory (SwaggerRoutes is ignored when set).</summary>
#if AspNetOwin
public Func<IOwinRequest, CancellationToken, Task<IEnumerable<SwaggerUi3Route>>> SwaggerRoutesFactory { get; set; }
#else
public Func<HttpRequest, CancellationToken, Task<IEnumerable<SwaggerUi3Route>>> 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<string> TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken)
#else
internal override string TransformHtml(string html, HttpRequest request)
internal override async Task<string> TransformHtmlAsync(string html, HttpRequest request, CancellationToken cancellationToken)
#endif
{
var htmlBuilder = new StringBuilder(html);
Expand All @@ -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
));
Expand Down
8 changes: 5 additions & 3 deletions src/NSwag.AspNetCore/SwaggerUiSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string> TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken)
#else
internal override string TransformHtml(string html, HttpRequest request)
internal override Task<string> TransformHtmlAsync(string html, HttpRequest request, CancellationToken cancellationToken)
#endif
{
var htmlBuilder = new StringBuilder(html);
Expand All @@ -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());
}
}
}
6 changes: 4 additions & 2 deletions src/NSwag.AspNetCore/SwaggerUiSettingsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,11 +64,11 @@ public SwaggerUiSettingsBase()
#if AspNetOwin
public Func<string, IOwinRequest, string> TransformToExternalPath { get; set; }

internal abstract string TransformHtml(string html, IOwinRequest request);
internal abstract Task<string> TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken);
#else
public Func<string, HttpRequest, string> TransformToExternalPath { get; set; }

internal abstract string TransformHtml(string html, HttpRequest request);
internal abstract Task<string> TransformHtmlAsync(string html, HttpRequest request, CancellationToken cancellationToken);
#endif

/// <summary>
Expand Down

0 comments on commit b819344

Please sign in to comment.