Gathering detailed insights and metrics for @benjd90/routing-controllers-openapi
Gathering detailed insights and metrics for @benjd90/routing-controllers-openapi
Gathering detailed insights and metrics for @benjd90/routing-controllers-openapi
Gathering detailed insights and metrics for @benjd90/routing-controllers-openapi
Runtime OpenAPI v3 schema generation for routing-controllers.
npm install @benjd90/routing-controllers-openapi
Typescript
Module System
Node Version
NPM Version
TypeScript (99.04%)
JavaScript (0.96%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
75 Commits
1 Branches
1 Contributors
Updated on Oct 30, 2023
Latest Version
1.7.0
Package Id
@benjd90/routing-controllers-openapi@1.7.0
Size
82.58 kB
NPM Version
6.4.1
Node Version
10.15.1
Published on
Jul 09, 2019
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Runtime OpenAPI v3 schema generation for @flyacts/routing-controllers.
yarn add @benjd90/routing-controllers-openapi
1import { getMetadataArgsStorage } from '@flyacts/routing-controllers' 2import { routingControllersToSpec } from '@benjd90/routing-controllers-openapi' 3 4// Define your controllers as usual: 5 6@JsonController('/users') 7class UsersController { 8 @Get('/:userId') 9 getUser(@Param('userId') userId: string) { 10 // ... 11 } 12 13 @HttpCode(201) 14 @Post('/') 15 createUser(@Body() body: CreateUserBody) { 16 // ... 17 } 18} 19 20// Generate a schema: 21 22const storage = getMetadataArgsStorage() 23const spec = routingControllersToSpec(storage) 24console.log(spec)
prints out the following specification:
1{ 2 "components": { 3 "schemas": {} 4 }, 5 "info": { 6 "title": "", 7 "version": "1.0.0" 8 }, 9 "openapi": "3.0.0", 10 "paths": { 11 "/users/{userId}": { 12 "get": { 13 "operationId": "UsersController.getUser", 14 "parameters": [ 15 { 16 "in": "path", 17 "name": "userId", 18 "required": true, 19 "schema": { 20 "type": "string" 21 } 22 } 23 ], 24 "responses": { 25 "200": { 26 "content": { 27 "application/json": {} 28 }, 29 "description": "Successful response" 30 } 31 }, 32 "summary": "List users", 33 "tags": [ 34 "Users" 35 ] 36 } 37 }, 38 "/users/": { 39 "post": { 40 "operationId": "UsersController.createUser", 41 "requestBody": { 42 "content": { 43 "application/json": { 44 "schema": { 45 "$ref": "#/components/schemas/CreateUserBody" 46 } 47 } 48 }, 49 "description": "CreateUserBody", 50 "required": false 51 }, 52 "responses": { 53 "201": { 54 "content": { 55 "application/json": {} 56 }, 57 "description": "Successful response" 58 } 59 }, 60 "summary": "Create user", 61 "tags": [ 62 "Users" 63 ] 64 } 65 } 66 } 67}
Check /sample
for a complete sample application.
routingControllersToSpec
has the following type signature:
1export function routingControllersToSpec( 2 storage: MetadataArgsStorage, 3 routingControllerOptions: RoutingControllersOptions = {}, 4 additionalProperties: Partial<OpenAPIObject> = {} 5): OpenAPIObject
routingControllerOptions
refers to the options object used to configure routing-controllers. Pass in the same options here to have your routePrefix
and defaults
options reflected in the resulting OpenAPI spec.
additionalProperties
is a partial OpenAPI object that gets merged into the result spec. You can for example set your own info
or components
keywords here.
Use class-validator-jsonschema to convert your validation classes into OpenAPI-compatible schemas:
1import { validationMetadatasToSchemas } from 'class-validator-jsonschema' 2 3// ... 4 5const schemas = validationMetadatasToSchemas(metadatas, { 6 refPointerPrefix: '#/components/schemas' 7}) 8 9const spec = routingControllersToSpec(storage, routingControllerOptions, { 10 components: { schemas }, 11 info: { title: 'My app', version: '1.2.0' } 12})
Use the @OpenAPI
decorator to supply your actions with additional keywords:
1import { OpenAPI } from '@benjd90/routing-controllers-openapi' 2 3@JsonController('/users') 4export class UsersController { 5 6 @Get('/') 7 @OpenAPI({ 8 description: 'List all available users', 9 responses: { 10 '400': { 11 description: 'Bad request' 12 } 13 } 14 }) 15 listUsers() { 16 // ... 17 } 18}
The parameter object consists of any number of properties from the Operation object. These properties are then merged into the spec, overwriting any existing values.
Alternatively you can call @OpenAPI
with a function of type (source: OperationObject, route: IRoute) => OperationObject
, i.e. a function receiving the existing spec as well as the target route, spitting out an updated spec. This function parameter can be used to implement for example your own merging logic or custom decorators.
@OpenAPI
decoratorsA single handler can be decorated with multiple @OpenAPI
s. Note though that since decorators are applied top-down, any possible duplicate keys are overwritten by subsequent decorators:
1 @OpenAPI({ 2 summary: 'This value will be overwritten!', 3 description: 'This value will remain' 4 }) 5 @OpenAPI({ 6 summary: 'This value will remain' 7 }) 8 listUsers() { 9 // ... 10 }
Multiple @OpenAPI
s are merged together with lodash/merge
which has a few interesting properties to keep in mind when it comes to arrays. Use the function parameter described above when strict control over merging logic is required.
@OpenAPI
decoratorUsing @OpenAPI
on the controller class effectively applies given spec to each class method. Method-level @OpenAPI
s are merged into class specs, with the former having precedence:
1@OpenAPI({ 2 security: [{ basicAuth: [] }] // Applied to each method 3}) 4@JsonController('/users') 5export class UsersController { 6 // ... 7}
Extracting response types automatically in runtime isn't currently allowed by Typescript's reflection system. Specifically the problem is that @benjd90/routing-controllers-openapi
can't unwrap generic types like Promise@ResponseSchema
decorator to supply the response body schema:
1import { ResponseSchema } from '@benjd90/routing-controllers-openapi' 2 3@JsonController('/users') 4export class UsersController { 5 6 @Get('/:id') 7 @ResponseSchema(User) 8 getUser() { 9 // ... 10 } 11}
@ResponseSchema
takes as an argument either a class-validator class or a plain string schema name. You can also supply an optional secondary options
argument:
1 @Post('/') 2 @ResponseSchema(User, { 3 contentType: 'text/csv', 4 description: 'A list of created user objects', 5 isArray: true 6 statusCode: '201'}) 7 createUsers() { 8 // ... 9 }
contentType
and statusCode
default to routing-controller's @ContentType
and @HttpCode
values. To specify a response schema of an array, set options.isArray
as true
. You can also annotate a single handler with multiple ResponseSchema
s to specify responses with different status codes.
Note that when using @ResponseSchema
together with @JSONSchema
, the outer decorator will overwrite keys of inner decorators. So in the following example, information from @ResponseSchema
would be overwritten by @JSONSchema
:
1@JSONSchema({responses: { 2 '200': { 3 'content': { 4 'application/json': { 5 schema: { 6 '$ref': '#/components/schemas/Pet' 7 } 8 } 9 ] 10 } 11}}) 12@ResponseSchema(SomeResponseObject) 13handler() { ... }
@Controller
/@JsonController
base route and default content-typeoptions.routePrefix
@Get
, @Post
and other action decorators@Param
decorator
/users/:id(\d+)/:type?
) are also supported@QueryParam
and @QueryParams
@HeaderParam
and @HeaderParams
@Body
and @BodyParam
@HttpCode
and @ContentType
valuesoptions.defaults.paramOptions.required
option and local override with {required: true}
in decorator paramssummary
, operationId
and tags
keywords from controller/method namesFeel free to submit a PR!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
project is archived
Details
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
93 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More