MMLib.SwaggerForOcelot
8.3.2
dotnet add package MMLib.SwaggerForOcelot --version 8.3.2
NuGet\Install-Package MMLib.SwaggerForOcelot -Version 8.3.2
<PackageReference Include="MMLib.SwaggerForOcelot" Version="8.3.2" />
paket add MMLib.SwaggerForOcelot --version 8.3.2
#r "nuget: MMLib.SwaggerForOcelot, 8.3.2"
// Install MMLib.SwaggerForOcelot as a Cake Addin #addin nuget:?package=MMLib.SwaggerForOcelot&version=8.3.2 // Install MMLib.SwaggerForOcelot as a Cake Tool #tool nuget:?package=MMLib.SwaggerForOcelot&version=8.3.2
<img src="./assets/logo.png" alt="logo" width="300"/>
SwaggerForOcelot combines two amazing projects Swashbuckle.AspNetCore and Ocelot. Allows you to view and use swagger documentation for downstream services directly through the Ocelot project.
Direct via http://ocelotprojecturl:port/swagger
provides documentation for downstream services configured in ocelot.json
. Additionally, the addresses are modified to match the UpstreamPathTemplate
from the configuration.
Did this project help you? You can now buy me a coffee ☕️.
<a href="https://www.buymeacoffee.com/minomartiniak" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-violet.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>
Get Started
- Configure SwaggerGen in your downstream services.
Follow the SwashbuckleAspNetCore documentation.
- Install Nuget package into yout ASP.NET Core Ocelot project.
dotnet add package MMLib.SwaggerForOcelot
- Configure SwaggerForOcelot in
ocelot.json
.
{
"Routes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5100
}
],
"UpstreamPathTemplate": "/api/contacts/{everything}",
"UpstreamHttpMethod": [ "Get" ],
"SwaggerKey": "contacts"
},
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5200
}
],
"UpstreamPathTemplate": "/api/orders/{everything}",
"UpstreamHttpMethod": [ "Get" ],
"SwaggerKey": "orders"
}
],
"SwaggerEndPoints": [
{
"Key": "contacts",
"Config": [
{
"Name": "Contacts API",
"Version": "v1",
"Url": "http://localhost:5100/swagger/v1/swagger.json"
}
]
},
{
"Key": "orders",
"Config": [
{
"Name": "Orders API",
"Version": "v0.9",
"Url": "http://localhost:5200/swagger/v0.9/swagger.json"
},
{
"Name": "Orders API",
"Version": "v1",
"Url": "http://localhost:5200/swagger/v1/swagger.json"
},
{
"Name": "Orders API",
"Version": "v2",
"Url": "http://localhost:5200/swagger/v2/swagger.json"
},
{
"Name": "Orders API",
"Version": "v3",
"Url": "http://localhost:5200/swagger/v3/swagger.json"
}
]
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost"
}
}
SwaggerEndPoint
is configuration for downstream service swagger generator endpoint. PropertyKey
is used to pair with the Route configuration.Name
is displayed in the combobox.Url
is downstream service swagger generator endpoint.
- In the
ConfigureServices
method ofStartup.cs
, register the SwaggerForOcelot generator.
services.AddSwaggerForOcelot(Configuration);
- In
Configure
method, insert theSwaggerForOcelot
middleware to expose interactive documentation.
app.UseSwaggerForOcelotUI(opt => {
opt.PathToSwaggerGenerator = "/swagger/docs";
})
You can optionally include headers that your Ocelot Gateway will send when requesting a swagger endpoint. This can be especially useful if your downstream microservices require contents from a header to authenticate.
app.UseSwaggerForOcelotUI(opt => {
opt.DownstreamSwaggerHeaders = new[]
{
new KeyValuePair<string, string>("Auth-Key", "AuthValue"),
};
})
After swagger for ocelot transforms the downstream swagger to the upstream swagger, you have the ability to alter the upstream swagger if you need to by setting the ReConfigureUpstreamSwaggerJson
option or ReConfigureUpstreamSwaggerJsonAsync
option for async methods.
public string AlterUpstreamSwaggerJson(HttpContext context, string swaggerJson)
{
var swagger = JObject.Parse(swaggerJson);
// ... alter upstream json
return swagger.ToString(Formatting.Indented);
}
app.UseSwaggerForOcelotUI(opt => {
opt.ReConfigureUpstreamSwaggerJson = AlterUpstreamSwaggerJson;
})
You can optionally customize the swagger server prior to calling the endpoints of the microservices as follows:
app.UseSwaggerForOcelotUI(opt => {
opt.ReConfigureUpstreamSwaggerJson = AlterUpstreamSwaggerJson;
opt.ServerOcelot = "/siteName/apigateway" ;
})
You can optionally customize SwaggerUI:
app.UseSwaggerForOcelotUI(opt => {
// swaggerForOcelot options
}, uiOpt => {
//swaggerUI options
uiOpt.DocumentTitle = "Gateway documentation";
})
Show your microservices interactive documentation.
http://ocelotserviceurl/swagger
Open API Servers
If you have multiple servers defined in the downstream service Open API documentation, or you use server templating and you want to use it on the gateway side as well, then you must explicitly enable it on the Swagger endpoint definition by setting property TakeServersFromDownstreamService
to true
.
"SwaggerEndPoints": [
{
"Key": "users",
"TakeServersFromDownstreamService": true,
"Config": [
{
"Name": "Users API",
"Version": "v1",
"Service": {
"Name": "users",
"Path": "/swagger/v1/swagger.json"
}
}
]
}
]
⚠ If you set
TakeServersFromDownstreamService
totrue
, then the server path is not used to transform the paths of individual endpoints.
Virtual directory
If you have a downstream service
hosted in the virtual directory, you probably have a DownstreamPathTemplate
starting with the name of this virtual directory /virtualdirectory/api/{everything}
. In order to properly replace the paths, it is necessary to set the property route "Virtualdirectory":"/virtualdirectory"
.
Example:
{
"DownstreamPathTemplate": "/project/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5100
}
],
"UpstreamPathTemplate": "/api/project/{everything}",
"UpstreamHttpMethod": [ "Get" ],
"SwaggerKey": "project",
"VirtualDirectory":"/project"
}
Service discovery
If you use Ocelot Service Discovery Provider to find the host and port for the downstream service, then you can use the same service name for swagger configuration.
"Routes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"ServiceName": "projects",
"UpstreamPathTemplate": "/api/project/{everything}",
"SwaggerKey": "projects",
}
],
"SwaggerEndPoints": [
{
"Key": "projects",
"Config": [
{
"Name": "Projects API",
"Version": "v1",
"Service": {
"Name": "projects",
"Path": "/swagger/v1/swagger.json"
}
}
]
}
],
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
"Type": "AppConfiguration",
"PollingInterval": 1000
}
}
The Gateway documentation itself
There are several real scenarios when you need to have a controller directly in your gateway. For example: specific aggregation of results from multiple services / legacy part of your system / ...
If you need to, you can also add documentation.
- Allow
GenerateDocsForGatewayItSelf
option in configuration section.
services.AddSwaggerForOcelot(Configuration,
(o) =>
{
o.GenerateDocsForGatewayItSelf = true;
});
or you can provide more options for gateway itself documentation
services.AddSwaggerForOcelot(Configuration,
(o) =>
{
o.GenerateDocsDocsForGatewayItSelf(opt =>
{
opt.FilePathsForXmlComments = { "MyAPI.xml" };
opt.GatewayDocsTitle = "My Gateway";
opt.GatewayDocsOpenApiInfo = new()
{
Title = "My Gateway",
Version = "v1",
};
opt.DocumentFilter<MyDocumentFilter>();
opt.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Description = @"JWT Authorization header using the Bearer scheme. Enter 'Bearer' [space] and then your token in the text input below. Example: 'Bearer 12345abcdef'",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
opt.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
});
});
- Use Swagger generator in
Configure
section.
app.UseSwagger();
Documentation of Ocelot Aggregates
You are probably familiar with Ocelot great feature Request Aggregation. Request Aggregation allows you to easily add a new endpoint to the gateway that will aggregate the result from other existing endpoints. If you use these aggregations, you would probably want to have these endpoints in the api documentation as well.
📢 From version 3.0.0
you can use this package for generating documentation for Ocelot aggregates.
In ConfigureServices
allow GenerateDocsForAggregates
option.
services.AddSwaggerForOcelot(Configuration,
(o) =>
{
o.GenerateDocsForAggregates = true;
});
Documentations of your aggregates will be available on custom page Aggregates.
The current implementation may not cover all scenarios (I hope most of them), but there are several ways you can change the final documentation.
Custom description
By default, this package generate description from downstream documentation. If you want add custom description for your aggregate route, you can add description to ocelot.json
.
"Aggregates": [
{
"RouteKeys": [
"user",
"basket"
],
"Description": "Custom description for this aggregate route.",
"Aggregator": "BasketAggregator",
"UpstreamPathTemplate": "/gateway/api/basketwithuser/{id}"
}
]
Different parameter names
It is likely that you will have different parameter names in the downstream services that you are aggregating. For example, in the User service you will have the {Id}
parameter, but in the Basket service the same parameter will be called {BuyerId}
. In order for Ocelot aggregations to work, you must have parameters named the same in Ocelot configurations, but this will make it impossible to find the correct documentation.
Therefore, you can help the configuration by setting parameter name map.
{
"DownstreamPathTemplate": "/api/basket/{id}",
"UpstreamPathTemplate": "/gateway/api/basket/{id}",
"ParametersMap": {
"id": "buyerId"
},
"ServiceName": "basket",
"SwaggerKey": "basket",
"Key": "basket"
}
Property ParametersMap
is map, where key
(first parameter) is the name of parameter in Ocelot configuration and value
(second parameter) is the name of parameter in downstream service.
Custom aggregator
The response documentation is generated according to the rules that Ocelot uses to compose the response from the aggregate. If you use your custom IDefinedAggregator
, your result may be different. In this case you can use AggregateResponseAttibute
.
[AggregateResponse("Basket with buyer and busket items.", typeof(CustomResponse))]
public class BasketAggregator : IDefinedAggregator
{
public async Task<DownstreamResponse> Aggregate(List<HttpContext> responses)
{
...
}
}
Modifying the generated documentation
If you do not like the final documentation, you can modify it by defining your custom postprocessor.
services.AddSwaggerForOcelot(Configuration,
(o) =>
{
o.GenerateDocsForAggregates = true;
o.AggregateDocsGeneratorPostProcess = (aggregateRoute, routesDocs, pathItemDoc, documentation) =>
{
if (aggregateRoute.UpstreamPathTemplate == "/gateway/api/basketwithuser/{id}")
{
pathItemDoc.Operations[OperationType.Get].Parameters.Add(new OpenApiParameter()
{
Name = "customParameter",
Schema = new OpenApiSchema() { Type = "string"},
In = ParameterLocation.Header
});
}
};
});
If none of this is enough
🙏 Feel free to provide a PR with implementation of your scenario. You will probably help many others.
Merging configuration files
Optionally you can use the Ocelot feature Merging configuration files to load the apigateway configuration from multiple configuration files named as follows: ocelot.exampleName.json
. To activate this feature you need to use the following extension:
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddOcelotWithSwaggerSupport();
})
.UseStartup<Startup>();
Using this extension the swagger path settings must be in a file called: ocelot.SwaggerEndPoints.json
. If instead you want to use another name for this file you could set the name as follows (without the .json extension):
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddOcelotWithSwaggerSupport((o) => {
o.FileOfSwaggerEndPoints = "ocelot.swagger";
})
})
.UseStartup<Startup>();
Optionally you can put the configuration files in a folder, and for that you have to set the extension as follows:
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddOcelotWithSwaggerSupport((o) => {
o.Folder = "Configuration";
});
})
.UseStartup<Startup>();
Optionally you can also add configuration files with the format ocelot.exampleName.json
per environment, to use this functionality you must configure the extension as follows:
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddOcelotWithSwaggerSupport((o) => {
o.Folder = "Configuration";
o.Environment = hostingContext.HostingEnvironment;
});
})
.UseStartup<Startup>();
To save the primary Ocelot config file under a name other than `ocelot.json then use the following:
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddOcelotWithSwaggerSupport((o) => {
o.PrimaryOcelotConfigFile = "myOcelot.json";
});
})
.UseStartup<Startup>();
Control downstream to swagger api
With the ISwaggerDownstreamInterceptor
interface you are able to inject your own logic to control the downstream.
- In the ConfigureServices method of Startup.cs, register your downstream interceptor along with your other dependencies.
services.AddSingleton<ISwaggerDownstreamInterceptor, PublishedDownstreamInterceptor>();
services.AddSingleton<ISwaggerEndpointConfigurationRepository, DummySwaggerEndpointRepository>();
- In your downstream interceptor add your custom logic to control if the downstream should be done.
public class PublishedDownstreamInterceptor : ISwaggerDownstreamInterceptor
{
private readonly ISwaggerEndpointConfigurationRepository _endpointConfigurationRepository;
public PublishedDownstreamInterceptor(ISwaggerEndpointConfigurationRepository endpointConfigurationRepository)
{
_endpointConfigurationRepository = endpointConfigurationRepository;
}
public bool DoDownstreamSwaggerEndpoint(HttpContext httpContext, string version, SwaggerEndPointOptions endPoint)
{
var myEndpointConfiguration = _endpointConfigurationRepository.GetSwaggerEndpoint(endPoint, version);
if (!myEndpointConfiguration.IsPublished)
{
httpContext.Response.StatusCode = 404;
httpContext.Response.WriteAsync("This enpoint is under development, please come back later.");
}
return myEndpointConfiguration.IsPublished;
}
}
Note, the service is still visible in the swagger ui the response is only visible in the request to the downstream url. If you want to control the visibility of the endpoints as well you have to implement a custom swagger ui.
Security definition generation
It is possible to generate security definitions for the enpoints based on Ocelot configuration
- Add
AuthenticationOptions
to your route definition
"Routes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"ServiceName": "projects",
"UpstreamPathTemplate": "/api/project/{everything}",
"SwaggerKey": "projects",
"AuthenticationOptions": {
"AuthenticationProviderKey": "Bearer",
"AllowedScopes": [ "scope" ]
},
}
]
- Provide a mapping in Startup between
AuthenticationProviderKey
and it's correspondingsecurityDefintion
services.AddSwaggerForOcelot(Configuration,
(o) =>
{
o.AddAuthenticationProviderKeyMapping("Bearer", "appAuth");
});
- Now you should have security definitions on your swagger documents
{
"paths": {
"/api/project": {
"get": {
...
"security": [
{
"appAuth": [ "scope" ]
}
]
}
}
}
}
Note, this does not affect nor checks the swagger document's securityDefinitions
property.
Downstream Documentation Caching
If your downstream documentation is too large, the response time may be slow.
To address this issue, you can enable caching of transformed documentation by setting the
DownstreamDocsCacheExpire
parameter. If this parameter is not provided, the documentation won't be cached.
If there is any change in the downstream documentation, the cache will be refreshed.
services.AddSwaggerForOcelot(Configuration,
setup =>
{
setup.DownstreamDocsCacheExpire = TimeSpan.FromMinutes(10);
});
Limitation
- Now, this library support only
{everything}
as a wildcard in routing definition. #68 - This package unfortunately does not support parameter translating between upstream and downstream path template. #59
- If your downstream documentation is too large (usually more than 10 MB), the response may be too slow. You can turn off the removal of an unused schema component from downstream documentation by using
RemoveUnusedComponentsFromScheme: false
.
"SwaggerEndPoints": [
{
"Key": "projects",
"RemoveUnusedComponentsFromScheme": false,
"Config": [
{
"Name": "Projects API",
"Version": "v1",
"Service": {
"Name": "projects",
"Path": "/swagger/v1/swagger.json"
}
}
]
}
]
Version 6.0.0
⚠️ Breaking change #240 - new way to modify swagger UI configuration.
Version 2.0.0
This version is breaking change. Because support Ocelot 16.0.0, which rename ReRoutes
to Routes
. See Ocelot v16.0.0.
If you have read this readme to the end, please let me know by clicking this link.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Kros.Utils (>= 2.1.0)
- Microsoft.Extensions.Http (>= 7.0.0)
- Ocelot (>= 18.0.0)
- Swashbuckle.AspNetCore.SwaggerGen (>= 6.5.0)
- Swashbuckle.AspNetCore.SwaggerUI (>= 6.5.0)
-
net7.0
- Kros.Utils (>= 2.1.0)
- Microsoft.Extensions.Http (>= 7.0.0)
- Ocelot (>= 19.0.4)
- Swashbuckle.AspNetCore.SwaggerGen (>= 6.5.0)
- Swashbuckle.AspNetCore.SwaggerUI (>= 6.5.0)
-
net8.0
- Kros.Utils (>= 2.1.0)
- Microsoft.Extensions.Http (>= 7.0.0)
- Ocelot (>= 20.0.0)
- Swashbuckle.AspNetCore.SwaggerGen (>= 6.5.0)
- Swashbuckle.AspNetCore.SwaggerUI (>= 6.5.0)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on MMLib.SwaggerForOcelot:
Package | Downloads |
---|---|
Fsel.Core
Fsel Core Package |
|
Garcia.Infrastructure.Ocelot
Package Description |
|
AviOcelotExtension
Package Description |
|
Ars.Common.Ocelot
Ocelot Extensions |
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on MMLib.SwaggerForOcelot:
Repository | Stars |
---|---|
madslundt/NetCoreMicroservicesSample
Sample using micro services in .NET Core 3.1 Focusing on clean code
|
|
hamed-shirbandi/TaskoMask
Task management system based on .NET 8 with Microservices, DDD, CQRS, Event Sourcing and Testing Concepts
|
Version | Downloads | Last updated |
---|---|---|
8.3.2 | 21,269 | 10/4/2024 |
8.3.1 | 171 | 10/4/2024 |
8.3.0 | 41,543 | 7/15/2024 |
8.2.0 | 120,516 | 3/27/2024 |
8.1.0 | 141,950 | 12/4/2023 |
8.0.0 | 24,400 | 11/21/2023 |
7.0.1 | 67,601 | 9/1/2023 |
7.0.0 | 21,405 | 7/13/2023 |
6.3.2 | 52,297 | 7/11/2023 |
6.3.1 | 52,601 | 6/7/2023 |
6.3.0 | 32,447 | 5/10/2023 |
6.2.0 | 206,713 | 1/27/2023 |
6.1.0 | 28,815 | 12/27/2022 |
6.0.0 | 97,703 | 10/19/2022 |
5.2.0 | 226,825 | 5/18/2022 |
5.1.0 | 45,623 | 4/18/2022 |
5.0.3 | 22,584 | 3/25/2022 |
4.9.0 | 38,998 | 3/23/2022 |
4.8.0 | 121,392 | 1/24/2022 |
4.7.0 | 34,566 | 12/3/2021 |
4.6.0 | 48,452 | 11/20/2021 |
4.5.0 | 11,958 | 11/3/2021 |
4.4.3 | 36,170 | 9/8/2021 |
4.4.2 | 6,617 | 8/24/2021 |
4.4.1 | 117,167 | 3/9/2021 |
4.4.0 | 14,117 | 2/18/2021 |
4.3.0 | 1,316 | 2/16/2021 |
4.2.0 | 1,553 | 2/15/2021 |
4.1.0 | 1,186 | 2/15/2021 |
4.0.2 | 9,441 | 2/8/2021 |
4.0.1 | 12,859 | 2/1/2021 |
4.0.0 | 17,466 | 12/31/2020 |
3.2.1 | 94,636 | 2/5/2021 |
3.2.0 | 65,263 | 12/21/2020 |
3.1.1 | 2,458 | 12/31/2020 |
3.1.0 | 4,203 | 12/18/2020 |
3.0.0 | 4,854 | 12/10/2020 |
2.6.1 | 43,734 | 10/30/2020 |
2.6.0 | 29,232 | 9/26/2020 |
2.5.1 | 18,578 | 9/6/2020 |
2.5.0 | 84,368 | 6/18/2020 |
2.4.0 | 1,716 | 6/18/2020 |
2.3.0 | 3,383 | 6/9/2020 |
2.2.0 | 2,937 | 6/5/2020 |
2.0.3 | 20,899 | 6/1/2020 |
2.0.2 | 3,160 | 5/28/2020 |
2.0.1 | 6,186 | 5/27/2020 |
2.0.0 | 1,525 | 5/27/2020 |
2.0.0-alpha.4 | 776 | 5/19/2020 |
2.0.0-alpha.3 | 14,934 | 4/25/2020 |
2.0.0-alpha.2 | 2,717 | 3/6/2020 |
2.0.0-alpha.1 | 502 | 2/28/2020 |
1.10.5 | 52,422 | 2/9/2020 |
1.10.3 | 7,188 | 1/23/2020 |
1.10.2 | 1,405 | 1/18/2020 |
1.10.1 | 2,837 | 1/16/2020 |
1.10.0 | 12,209 | 11/28/2019 |
1.9.0 | 31,374 | 10/12/2019 |
1.8.0 | 4,246 | 10/9/2019 |
1.7.0 | 15,607 | 9/10/2019 |
1.6.0 | 97,599 | 8/15/2019 |
1.5.1 | 1,493 | 8/13/2019 |
1.5.0 | 2,955 | 7/30/2019 |
1.4.1 | 2,390 | 7/22/2019 |
1.4.0 | 3,017 | 6/14/2019 |
1.3.1 | 1,523 | 6/11/2019 |
1.3.0 | 3,955 | 6/6/2019 |
1.2.1 | 5,308 | 5/23/2019 |
1.2.0 | 2,252 | 4/27/2019 |
1.1.0 | 8,504 | 2/25/2019 |
1.0.1 | 3,927 | 2/5/2019 |
1.0.0 | 4,593 | 1/17/2019 |
0.4.0 | 1,992 | 1/1/2019 |
0.3.0 | 1,679 | 12/19/2018 |
0.2.0 | 1,873 | 12/4/2018 |
0.1.0 | 3,805 | 12/2/2018 |