First, your problem sounds like you didn't properly separate your APIs and that in reality it should be two apis (two applications services in microservices terminology).
As such, you should treat it as two separate APIs too.
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("public", new OpenApiInfo { Title = "My Public API", Version = "v1" });
c.SwaggerDoc("private", new OpenApiInfo { Title = "My Private API", Version = "v1" });
});
This will generate two different OpenAPI (Swagger) specs. /api-docs/public/swagger.json
and /api-docs/private/swagger.json
, which can be hosted in two different UI applications (one protected other publically available)
// Public Docs Api
app.UseSwaggerUI(c =>
{
// we use absolute uri here, since the swagger.json is outside of this application
c.SwaggerEndpoint("http://example.com/api-docs/public/swagger.json", "Public API");
});
// Private Docs App
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("http://example.com/api-docs/private/swagger.json", "Private API");
});
An alternative approach is to use a build-pipeline/continuous integration system. Swashbuckle.AspNetCore provides an cli extensions in the 5.x version of the library, which can be executed as part of a build script to generate the swagger.json
files during a build process.
For example
dotnet swagger tofile --output ../swagger/myapi/private.json MyCompany.MyApplication.Mvc private
dotnet swagger tofile --output ../swagger/myapi/public.json MyCompany.MyApplication.Mvc public
and have one docs App like this
// Private Docs App
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("http://example.com/api-docs/swagger/myapp/private.json", "Private API");
c.SwaggerEndpoint("http://example.com/api-docs/swagger/myapp/public.json", "Public API");
});
where you protect "private.json" with Webserver means (nginx, apache, iis), i.e. allowing private.json access only in the internal network or only after authentication etc.
Alternative to the above, is to host both files in the same application, but secure the private file with a middleware, see this GitHub issue for some inspirations.