Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better support for customizing SwaggerUI #240

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ app.UseSwaggerForOcelotUI(opt => {
})
```

You can optionally customize SwaggerUI:
```CSharp
app.UseSwaggerForOcelotUI(opt => {
// swaggerForOcelot options
}, uiOpt => {
//swaggerUI options
uiOpt.DocumentTitle = "Gateway documentation";
})
```

6. Show your microservices interactive documentation.

> `http://ocelotserviceurl/swagger`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace MMLib.SwaggerForOcelot.Configuration
/// Configuration for Swagger UI.
/// </summary>
/// <seealso cref="Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIOptions" />
public class SwaggerForOcelotUIOptions : SwaggerUIOptions
public class SwaggerForOcelotUIOptions
{
/// <summary>
/// The relative path to gateway swagger generator.
Expand Down
16 changes: 4 additions & 12 deletions src/MMLib.SwaggerForOcelot/Middleware/BuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ public static class BuilderExtensions
/// </summary>
/// <param name="app">The application builder.</param>
/// <param name="setupAction">Setup <see cref="SwaggerForOcelotUIOptions"/></param>
/// <param name="setupUiAction">Setup SwaggerUI</param>
/// <returns>
/// <see cref="IApplicationBuilder"/>.
/// </returns>
public static IApplicationBuilder UseSwaggerForOcelotUI(
this IApplicationBuilder app,
Action<SwaggerForOcelotUIOptions> setupAction = null)
Action<SwaggerForOcelotUIOptions> setupAction = null,
Action<SwaggerUIOptions> setupUiAction = null)
{
SwaggerForOcelotUIOptions options = app.ApplicationServices.GetService<IOptions<SwaggerForOcelotUIOptions>>().Value;
setupAction?.Invoke(options);
UseSwaggerForOcelot(app, options);

app.UseSwaggerUI(c =>
{
InitUIOption(c, options);
setupUiAction?.Invoke(c);
IReadOnlyList<SwaggerEndPointOptions> endPoints = app
.ApplicationServices.GetService<ISwaggerEndPointProvider>().GetAll();

Expand Down Expand Up @@ -95,15 +97,5 @@ static string GetDescription(SwaggerEndPointConfig config)
}
}
}

private static void InitUIOption(SwaggerUIOptions c, SwaggerForOcelotUIOptions options)
{
c.ConfigObject = options.ConfigObject;
c.DocumentTitle = options.DocumentTitle;
c.HeadContent = options.HeadContent;
c.IndexStream = options.IndexStream;
c.OAuthConfigObject = options.OAuthConfigObject;
c.RoutePrefix = options.RoutePrefix;
}
}
}