C
C#•3y ago
BokoGanga

Two SwaggerUI for one Solution

Hey guys, I have been looking around for a way to create two SwaggerUI for one project. I want to create a separate document just for 3 Endpoints to be displayed while the other contains all the endpoints. Is this doable? If anyone has any leads, I'd really appreciate it.
8 Replies
Angius
Angius•3y ago
Ooof, this awoke some bad memories lol I was fighting with the very same issue not long ago Lemme dig up the code I used
services.AddOpenApiDocument(settings =>
{
settings.DocumentName = "public";
settings.OperationProcessors.Insert(0, new ExcludeRssProcessor());
settings.OperationProcessors.Insert(1, new ExcludeInternalApisProcessor());
settings.SchemaNameGenerator = new NSwagNestedNameGenerator();
});
services.AddOpenApiDocument(settings =>
{
settings.DocumentName = "internal";
settings.OperationProcessors.Insert(0, new ExcludeRssProcessor());
settings.OperationProcessors.Insert(1, new IncludeInternalApisProcessor());
settings.SchemaNameGenerator = new NSwagNestedNameGenerator();
});
services.AddOpenApiDocument(settings =>
{
settings.DocumentName = "public";
settings.OperationProcessors.Insert(0, new ExcludeRssProcessor());
settings.OperationProcessors.Insert(1, new ExcludeInternalApisProcessor());
settings.SchemaNameGenerator = new NSwagNestedNameGenerator();
});
services.AddOpenApiDocument(settings =>
{
settings.DocumentName = "internal";
settings.OperationProcessors.Insert(0, new ExcludeRssProcessor());
settings.OperationProcessors.Insert(1, new IncludeInternalApisProcessor());
settings.SchemaNameGenerator = new NSwagNestedNameGenerator();
});
this is inside of the Startup.cs And it uses processors like this:
public class ExcludeInternalApisProcessor : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
=> !(context.ControllerType.FullName?.ToUpper().Contains("ADMIN") ?? false);
}
public class ExcludeInternalApisProcessor : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
=> !(context.ControllerType.FullName?.ToUpper().Contains("ADMIN") ?? false);
}
public class IncludeInternalApisProcessor : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
=> context.ControllerType.FullName?.ToUpper().Contains("ADMIN") ?? false;
}
public class IncludeInternalApisProcessor : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
=> context.ControllerType.FullName?.ToUpper().Contains("ADMIN") ?? false;
}
public class ExcludeRssProcessor : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
=> !(context.ControllerType.FullName?.Contains("rss", StringComparison.InvariantCultureIgnoreCase) ?? false);
}
public class ExcludeRssProcessor : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
=> !(context.ControllerType.FullName?.Contains("rss", StringComparison.InvariantCultureIgnoreCase) ?? false);
}
IIRC, it kinda-sorta uses the API versioning mechanism of SwaggerUI, so instead of switching between V1/V2/V3 and so on, you switch between public/internal/whatever Haven't found a way that would be without this tradeoff
BokoGanga
BokoGangaOP•3y ago
Omg! Thanks a lotttt. I will test this out and see what it produces. @Angius one question if you don't mind, in what namespace is "NSwagNestedNameGenerator" available? or is it a different package than NSwag?
Angius
Angius•3y ago
Ah, that's also my own class I think you can just not set it
BokoGanga
BokoGangaOP•3y ago
Ah alright. It threw me off a bit. I will test it now and see. @Angius thanks a lot. It worked like a charm. Life saver. 🙇
Angius
Angius•3y ago
Nice
BokoGanga
BokoGangaOP•3y ago
anywhere to change the title of the document?
Angius
Angius•3y ago
settings.DocumentName = "public"; should be it If not, not sure There probably is some way
BokoGanga
BokoGangaOP•3y ago
yeah I figured it out. it has a property to add a title
Want results from more Discord servers?
Add your server