Skip to content

Commit

Permalink
feat: use UseSwaggerUi & SwaggerUiTheme options in configuer UseSwagg…
Browse files Browse the repository at this point in the history
…erUI
  • Loading branch information
davidkallesen committed Aug 6, 2024
1 parent ff26429 commit 8d0a560
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
8 changes: 8 additions & 0 deletions docs/CodeDoc/Atc.Rest.Extended/Atc.Rest.Extended.Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
### Properties
#### SwaggerUiTheme
>```csharp
>SwaggerUiTheme
>```
#### UseApiVersioning
>```csharp
>UseApiVersioning
Expand All @@ -61,4 +65,8 @@
>```csharp
>UseOpenApiSpec
>```
#### UseSwaggerUi
>```csharp
>UseSwaggerUi
>```
<hr /><div style='text-align: right'><i>Generated by MarkdownCodeDoc version 1.2</i></div>
4 changes: 3 additions & 1 deletion docs/CodeDoc/Atc.Rest.Extended/IndexExtended.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
- PostConfigure(string name, JwtBearerOptions options)
- [RestApiExtendedOptions](Atc.Rest.Extended.Options.md#restapiextendedoptions)
- Properties
- SwaggerUiTheme
- UseApiVersioning
- UseFluentValidation
- UseOpenApiSpec
- UseSwaggerUi

## [Atc.Rest.Extended.Versioning](Atc.Rest.Extended.Versioning.md)

Expand All @@ -64,7 +66,7 @@
- [OpenApiBuilderExtensions](Microsoft.AspNetCore.Builder.md#openapibuilderextensions)
- Static Methods
- UseOpenApiSpec(this IApplicationBuilder app, IWebHostEnvironment env)
- UseOpenApiSpec(this IApplicationBuilder app, IWebHostEnvironment env, RestApiExtendedOptions restApiOptions)
- UseOpenApiSpec(this IApplicationBuilder app, IWebHostEnvironment env, RestApiExtendedOptions restApiOptions, SwaggerUIOptions swaggerUiOption = null)
- [RestApiExtendedBuilderExtensions](Microsoft.AspNetCore.Builder.md#restapiextendedbuilderextensions)
- Static Methods
- ConfigureRestApi(this IApplicationBuilder app, IWebHostEnvironment env)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
>```
#### UseOpenApiSpec
>```csharp
>IApplicationBuilder UseOpenApiSpec(this IApplicationBuilder app, IWebHostEnvironment env, RestApiExtendedOptions restApiOptions)
>IApplicationBuilder UseOpenApiSpec(this IApplicationBuilder app, IWebHostEnvironment env, RestApiExtendedOptions restApiOptions, SwaggerUIOptions swaggerUiOption = null)
>```
<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public static IApplicationBuilder UseOpenApiSpec(
public static IApplicationBuilder UseOpenApiSpec(
this IApplicationBuilder app,
IWebHostEnvironment env,
RestApiExtendedOptions restApiOptions)
RestApiExtendedOptions restApiOptions,
SwaggerUIOptions? swaggerUiOption = null)
{
ArgumentNullException.ThrowIfNull(env);
ArgumentNullException.ThrowIfNull(restApiOptions);
Expand All @@ -23,9 +24,16 @@ public static IApplicationBuilder UseOpenApiSpec(

app.UseSwagger();

if (env.IsDevelopment())
if (restApiOptions.UseSwaggerUi || env.IsDevelopment())
{
app.UseSwaggerUI();
if (swaggerUiOption is null)
{
app.UseSwaggerUI();
}
else
{
app.UseSwaggerUI(swaggerUiOption);
}
}

return app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,35 @@ public static IApplicationBuilder ConfigureRestApi(
// Cast to base-restApiOptions to force to use ConfigureRestApi in RestApiBuilderExtensions
app.ConfigureRestApi(env, restApiOptions as RestApiOptions, setupAction);

if (restApiOptions.UseOpenApiSpec)
if (!restApiOptions.UseOpenApiSpec)
{
return app;
}

if ((env.IsDevelopment() || restApiOptions.UseSwaggerUi) &&
restApiOptions.SwaggerUiTheme is not null)
{
var cssFile = string.Empty;
if (restApiOptions.SwaggerUiTheme.EndsWith(".css", StringComparison.Ordinal) &&
restApiOptions.SwaggerUiTheme.Contains("Light", StringComparison.OrdinalIgnoreCase))
{
cssFile = "SwaggerLight.css";
}
else if (restApiOptions.SwaggerUiTheme.EndsWith(".css", StringComparison.Ordinal) &&
restApiOptions.SwaggerUiTheme.Contains("Dark", StringComparison.OrdinalIgnoreCase))
{
cssFile = "SwaggerDark.css";
}

var options = new SwaggerUIOptions();
options.InjectStylesheet(restApiOptions.SwaggerUiTheme);
options.EnableTryItOutByDefault();
options.InjectStylesheet($"/swagger-ui/{cssFile}");
options.InjectJavascript("/swagger-ui/main.js");

app.UseOpenApiSpec(env, restApiOptions, options);
}
else
{
app.UseOpenApiSpec(env, restApiOptions);
}
Expand Down

0 comments on commit 8d0a560

Please sign in to comment.