Skip to content

Commit ae6d155

Browse files
committed
fix: update NuGet tag pattern and version, enhance OpenAPI UI middleware configuration
1 parent 174aab0 commit ae6d155

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

.github/workflows/publish-nuget.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Publish NuGet Package
33
on:
44
push:
55
tags:
6-
- "v*.*.*"
6+
- "nuget/v*"
77
workflow_dispatch:
88

99
env:

src/c-sharp/JakubKozera.OpenApiUi/JakubKozera.OpenApiUi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
44
<PackageId>JakubKozera.OpenApiUi</PackageId>
5-
<Version>1.0.10</Version>
5+
<Version>1.0.11</Version>
66
<Authors>Jakub Kozera</Authors>
77
<Description>A .NET library serving OpenAPI UI with HTML/CSS</Description>
88
<PackageTags>openapi;ui;html;css;dotnet</PackageTags>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace JakubKozera.OpenApiUi
2+
{
3+
/// <summary>
4+
/// Configuration options for OpenAPI UI middleware.
5+
/// </summary>
6+
public class OpenApiUiConfiguration
7+
{
8+
/// <summary>
9+
/// Gets or sets the path to the OpenAPI specification JSON file.
10+
/// </summary>
11+
/// <value>The path to the OpenAPI specification. Defaults to "/swagger/v1/swagger.json".</value>
12+
public string OpenApiSpecPath { get; set; } = "/swagger/v1/swagger.json";
13+
14+
/// <summary>
15+
/// Gets or sets the path where the OpenAPI UI will be served.
16+
/// </summary>
17+
/// <value>The UI path. Defaults to "openapi-ui".</value>
18+
public string OpenApiUiPath { get; set; } = "openapi-ui";
19+
}
20+
}

src/c-sharp/JakubKozera.OpenApiUi/Lib/OpenApiUiMiddleware.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,31 @@ public static class OpenApiUiMiddlewareExtensions
1717
/// This middleware serves an embedded OpenAPI documentation interface.
1818
/// </summary>
1919
/// <param name="app">The application builder instance.</param>
20-
/// <param name="openApiSpecPath">The path to the OpenAPI specification JSON file. Defaults to "/swagger/v1/swagger.json".</param>
20+
/// <param name="configuration">The OpenAPI UI configuration options.</param>
2121
/// <returns>The application builder instance for method chaining.</returns>
22-
public static IApplicationBuilder UseOpenApiUi(this IApplicationBuilder app, string openApiSpecPath = "/swagger/v1/swagger.json")
22+
public static IApplicationBuilder UseOpenApiUi(this IApplicationBuilder app, OpenApiUiConfiguration configuration)
2323
{
24-
var assembly = Assembly.GetExecutingAssembly(); var embeddedProvider = new EmbeddedFileProvider(assembly, "JakubKozera.OpenApiUi.openapi-ui");
24+
if (configuration == null)
25+
throw new ArgumentNullException(nameof(configuration));
26+
27+
var assembly = Assembly.GetExecutingAssembly();
28+
var embeddedProvider = new EmbeddedFileProvider(assembly, "JakubKozera.OpenApiUi.openapi-ui");
29+
30+
// Use the configurable UI path for serving static files
31+
var requestPath = $"/{configuration.OpenApiUiPath.TrimStart('/')}";
2532

2633
app.UseStaticFiles(new StaticFileOptions
2734
{
2835
FileProvider = embeddedProvider,
29-
RequestPath = "/openapi-ui",
36+
RequestPath = requestPath,
3037
ServeUnknownFileTypes = true
3138
});
3239

3340
app.Use(async (context, next) =>
3441
{
35-
var requestPath = context.Request.Path.Value;
42+
var currentPath = context.Request.Path.Value;
3643

37-
if (context.Request.Path == "/openapi-ui" || context.Request.Path == "/openapi-ui/")
44+
if (context.Request.Path == requestPath || context.Request.Path == $"{requestPath}/")
3845
{
3946
var fileInfo = embeddedProvider.GetFileInfo("index.html");
4047

@@ -47,12 +54,13 @@ public static IApplicationBuilder UseOpenApiUi(this IApplicationBuilder app, str
4754
var content = await reader.ReadToEndAsync();
4855

4956
// Replace the placeholder with the actual OpenAPI spec path
50-
var originalContent = content;
51-
content = content.Replace("#swagger_path#", openApiSpecPath);
57+
content = content.Replace("#swagger_path#", configuration.OpenApiSpecPath);
5258

53-
content = content.Replace("bundle.css", "openapi-ui/bundle.css");
54-
content = content.Replace("bundle.js", "openapi-ui/bundle.js");
55-
content = content.Replace("openapi-ui.png", "openapi-ui/openapi-ui.png");
59+
// Update resource paths to use the configurable UI path
60+
var uiPath = configuration.OpenApiUiPath.TrimStart('/');
61+
content = content.Replace("bundle.css", $"{uiPath}/bundle.css");
62+
content = content.Replace("bundle.js", $"{uiPath}/bundle.js");
63+
content = content.Replace("openapi-ui.png", $"{uiPath}/openapi-ui.png");
5664

5765
context.Response.ContentType = "text/html";
5866
await context.Response.WriteAsync(content);
@@ -79,5 +87,14 @@ public static IApplicationBuilder UseOpenApiUi(this IApplicationBuilder app, str
7987

8088
return app;
8189
}
90+
91+
/// <summary>
92+
/// Adds OpenAPI UI middleware to the ASP.NET Core application pipeline with default configuration.
93+
/// This middleware serves an embedded OpenAPI documentation interface.
94+
/// </summary>
95+
/// <param name="app">The application builder instance.</param>
96+
/// <returns>The application builder instance for method chaining.</returns>
97+
public static IApplicationBuilder UseOpenApiUi(this IApplicationBuilder app)
98+
=> UseOpenApiUi(app, new OpenApiUiConfiguration());
8299
}
83100
}

0 commit comments

Comments
 (0)