nestrs-openapi generates an OpenAPI 3.1 specification from the routes registered in RouteRegistry and serves a Swagger UI at a configurable path. Enable it with a single call to NestApplication::enable_openapi() or enable_openapi_with_options(options).
Quick start
GET /openapi.json— the generated OpenAPI 3.1 JSON specGET /docs— Swagger UI (served via CDN fromunpkg.com/swagger-ui-dist)
OpenApiOptions
Configures titles, paths, servers, security, and more.
The
info.title field in the generated spec.The
info.version field. Defaults to the value of CARGO_PKG_VERSION at compile time.The path where the OpenAPI JSON spec is served. Defaults to
"/openapi.json".The path where Swagger UI is served. Defaults to
"/docs".OpenAPI Server Objects. Pass a JSON array.
OpenAPI Components Object for security schemes, schemas, and other reusable definitions.
Root-level Security Requirement Objects applied to all operations by default.
When
true, routes with #[roles] metadata get an operation-level security entry referencing roles_security_scheme. Requires that scheme to be declared in components.securitySchemes.enable_openapi vs enable_openapi_with_options
#[openapi] macro
Annotate individual route handlers to override the auto-generated summary, set a tag, and declare response status codes.
Human-readable summary for the operation. Defaults to a humanized version of the handler function name (e.g.,
find_one → "Find one").Groups the operation in Swagger UI. Defaults to the first meaningful path segment after an optional version segment (e.g.,
/v1/users/1 → "users").Declares response status codes and their descriptions. Defaults to
200 OK only when not specified.What is generated automatically
For each route registered inRouteRegistry, nestrs-openapi generates:
operationId— the full Rust module path + handler name (e.g.,my_app::controllers::UsersController::find_one)summary— humanized handler name or the#[openapi(summary = ...)]overridetags— inferred from the URL path or the#[openapi(tag = ...)]overrideresponses—200 OKby default, or the list from#[openapi(responses = ...)]
Security scheme documentation
Add abearerAuth scheme to components.securitySchemes and enable infer_route_security_from_roles to show the Swagger lock icon on routes protected with #[roles]:
/api/private operation will include security: [{ "bearerAuth": [] }] in the generated spec, rendering the lock icon in Swagger UI.
Accessing the generated spec
The spec is served as JSON at thejson_path (default: /openapi.json) and is not mounted under set_global_prefix or enable_uri_versioning. You can fetch it directly:
What nestrs-openapi does not do
nestrs-openapi does not generate JSON schemas from Rust types. Unlike @nestjs/swagger with @ApiProperty, there is no runtime type reflection. To add schemas to components.schemas, either hand-author them in OpenApiOptions::components or use a separate schema generation crate such as utoipa or okapi and merge the output.NestJS / @nestjs/swagger | nestrs-openapi |
|---|---|
@ApiTags | Inferred from URL path or #[openapi(tag = "...")] |
@ApiOperation | Auto summary or #[openapi(summary = "...")] |
@ApiResponse | Default 200 OK or #[openapi(responses = ...)] |
| DTO schema generation | Not built-in — hand-author components.schemas |
@ApiBearerAuth | components.securitySchemes + infer_route_security_from_roles |
| Swagger UI | Yes — bundled at docs_path |
| Plugins (NestJS CLI) | No |