Skip to content

Swagger / OpenAPI

GrydSwagger provides centralized Swagger/OpenAPI configuration with automatic JWT token management for the Gryd.IO ecosystem.

Features

FeatureDescription
Bearer JWT SecuritySecuritySchemeType.Http with Bearer scheme
Per-endpoint Lock IconsLock icon only on [Authorize] endpoints
Auto Token CaptureAutomatically captures JWT from login response
Token PersistencePreserves token across page refreshes
XML CommentsAuto-discovers XML documentation from assemblies

Quick Setup

csharp
// Program.cs
using Gryd.API.Swagger;

// Register services
builder.Services.AddGrydSwagger();

// Configure middleware
if (app.Environment.IsDevelopment())
{
    app.UseGrydSwagger();
}

That's it! With two lines you get a fully configured Swagger UI with automatic token management.

How Auto Token Capture Works

Configuration Options

Basic Configuration

csharp
builder.Services.AddGrydSwagger(options =>
{
    options.Title = "My API";
    options.Version = "v1";
    options.Description = "My application API documentation";
});

Full Configuration

csharp
builder.Services.AddGrydSwagger(options =>
{
    // API Documentation
    options.Title = "My Company API";
    options.Version = "v2";
    options.Description = "Complete API documentation";
    
    // Authentication
    options.IncludeAuthentication = true;   // Enable Bearer JWT (default: true)
    options.AutoCaptureToken = true;        // Auto-capture from login (default: true)
    options.PersistAuthorization = true;    // Keep token on refresh (default: true)
    
    // Custom login endpoint (relative path, version prefix added automatically)
    options.LoginEndpointPath = "auth/login";           // default
    options.TokenJsonPath = "data.token";               // default
    
    // XML Documentation
    options.IncludeXmlComments = true;     // default: true
    
    // Contact Info
    options.ContactName = "Dev Team";
    options.ContactEmail = "dev@example.com";
    options.ContactUrl = "https://example.com";
    
    // License
    options.LicenseName = "MIT";
    options.LicenseUrl = "https://opensource.org/licenses/MIT";
});

All Options

OptionTypeDefaultDescription
Titlestring"Gryd.IO API"API documentation title
Versionstring"v1"API version
Descriptionstring?nullAPI description
IncludeAuthenticationbooltrueEnable Bearer JWT security scheme
PersistAuthorizationbooltrueKeep token across page refreshes
AutoCaptureTokenbooltrueAuto-capture token from login
LoginEndpointPathstring"auth/login"Login endpoint path
TokenJsonPathstring"data.token"JSON path to token in response
IncludeXmlCommentsbooltrueInclude XML doc comments
ContactNamestring?nullContact name
ContactEmailstring?nullContact email
ContactUrlstring?nullContact URL
LicenseNamestring?nullLicense name
LicenseUrlstring?nullLicense URL

Per-Endpoint Security (AuthorizeOperationFilter)

GrydSwagger replaces the global AddSecurityRequirement with an IOperationFilter that inspects each endpoint individually.

Behavior

AttributeLock Icon401/403 Response
[Authorize]✅ Yes✅ Added
[Authorize(Roles = "Admin")]✅ Yes✅ Added
[AllowAnonymous]❌ No❌ Not added
No attribute❌ No❌ Not added

Before vs After

Before (global security):

csharp
// ❌ All endpoints show lock icon, even login
c.AddSecurityRequirement(new OpenApiSecurityRequirement { ... });

After (per-endpoint):

csharp
// ✅ Only [Authorize] endpoints show lock
builder.Services.AddGrydSwagger(); // Uses AuthorizeOperationFilter internally

Example

csharp
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    [HttpGet]                    // ❌ No lock — accessible by anyone
    public IActionResult List() => Ok();

    [Authorize]
    [HttpPost]                   // 🔒 Lock icon — requires Bearer token
    public IActionResult Create() => Ok();

    [Authorize(Roles = "Admin")]
    [HttpDelete("{id}")]         // 🔒 Lock icon — requires Bearer token
    public IActionResult Delete(Guid id) => Ok();
}

Custom Token Path

If your login response has a different structure, configure TokenJsonPath:

csharp
// Response: { "result": { "accessToken": "eyJ..." } }
builder.Services.AddGrydSwagger(options =>
{
    options.TokenJsonPath = "result.accessToken";
});

// Response: { "token": "eyJ..." }
builder.Services.AddGrydSwagger(options =>
{
    options.TokenJsonPath = "token";
});

Without Authentication

For APIs that don't use authentication:

csharp
builder.Services.AddGrydSwagger(options =>
{
    options.IncludeAuthentication = false;
    options.Title = "Public API";
});

Architecture

Files

FileDescription
GrydSwaggerOptionsConfiguration POCO with all options
AuthorizeOperationFilterIOperationFilter for per-endpoint Bearer security
GrydSwaggerExtensionsAddGrydSwagger() / UseGrydSwagger() extension methods

Extension Methods

MethodTargetDescription
AddGrydSwagger()IServiceCollectionRegisters Swagger services + Bearer scheme
AddGrydSwagger(Action<GrydSwaggerOptions>)IServiceCollectionSame with custom options
UseGrydSwagger()IApplicationBuilderConfigures Swagger UI middleware
UseGrydSwagger(IHostEnvironment)IApplicationBuilderSame, only in Development

Released under the MIT License.