Decorators
Complete reference for all OpenAPI decorators available in AdonisJS. Covers operation, controller, and model decorators.
This page provides a comprehensive reference for all available OpenAPI decorators. Decorators are organized by their usage context: operations (methods), controllers, and models.
Operation Decorators
These decorators are used on controller methods to describe API operations.
@ApiOperation()
Configures the operation details such as summary, description, and operation ID. This decorator is optional as operations are automatically loaded from the Adonis Router.
Usage: Method
import { } from '@foadonis/openapi/decorators'
export default class {
@({
: 'List all Posts',
: 'Retrieves a list of all posts in the system',
: 'listPosts',
})
() {
// ...your logic
}
}Options:
summary?: string- A short summary of what the operation doesdescription?: string- A verbose explanation of the operation behavioroperationId?: string- Unique string used to identify the operationdeprecated?: boolean- Declares this operation to be deprecatedtags?: string[]- A list of tags for API documentation control
@ApiResponse()
Describes a possible response from an operation, including status code, description, and schema. You can stack multiple @ApiResponse decorators on the same operation for different status codes. Can also be used on the controller to apply to all its operations.
Usage: Method / Controller
import from '#models/post'
import { } from '@foadonis/openapi/decorators'
export default class {
@({
: 200,
: 'List of posts',
: [],
})
@({
: 404,
: 'No posts found',
})
() {
// ...your logic
}
@({
: 201,
: 'The post has been successfully created.',
: ,
})
() {}
}Options:
status?: number- HTTP status code (default: 200)description?: string- A description of the responsetype?: Type | Type[]- The response type or array of typesmediaType?: string- The media type of the response (e.g., 'application/json', 'text/html')schema?: SchemaObject- Raw OpenAPI schema object
@ApiBody()
Describes the request body of an operation, including its schema and examples.
Usage: Method
import { } from '#validators/post'
import { } from '@foadonis/openapi/decorators'
export default class {
@({
: () => ,
: 'Post creation data',
})
() {}
}Options:
type?: Type | (() => Type)- The request body type (use a function for circular dependencies)description?: string- A description of the request bodymediaType?: string- The media type of the request body (e.g., 'application/json', 'multipart/form-data')required?: boolean- Whether the request body is requiredschema?: SchemaObject- Raw OpenAPI schema object
@ApiParam()
Describes a single path parameter for an operation, including its name, location, and schema.
Usage: Method
import { } from '@foadonis/openapi/decorators'
export default class {
@({
: 'id',
: 'The post identifier',
: ,
: true,
})
() {}
}Options:
name: string- The name of the parameterdescription?: string- A description of the parametertype?: Type- The parameter typerequired?: boolean- Whether the parameter is required (default: true for path parameters)schema?: SchemaObject- Raw OpenAPI schema objectexample?: any- Example value for the parameter
@ApiQuery()
Describes a query parameter for an operation.
Usage: Method
import { } from '@foadonis/openapi/decorators'
export default class {
@({
: 'page',
: 'Page number',
: ,
: false,
})
@({
: 'limit',
: 'Number of items per page',
: ,
: false,
})
@({
: 'status',
: ['draft', 'published', 'archived'],
: false,
})
() {}
}Options:
name: string- The name of the query parameterdescription?: string- A description of the parametertype?: Type- The parameter typerequired?: boolean- Whether the parameter is required (default: false)enum?: string[] | Enum- Array of allowed values or TypeScript enumschema?: SchemaObject- Raw OpenAPI schema objectexample?: any- Example value for the parameter
@ApiHeader()
Describes a header parameter for an operation. Can be applied to specific operations or directly on the controller.
Usage: Method / Controller
import { } from '@foadonis/openapi/decorators'
@({
: 'X-Language',
: 'The currently defined language',
: false,
})
export default class {
@({
: 'X-Store',
: 'Store identifier',
: true,
})
() {}
}Options:
name: string- The name of the headerdescription?: string- A description of the headerrequired?: boolean- Whether the header is required (default: false)type?: Type- The header typeschema?: SchemaObject- Raw OpenAPI schema objectexample?: any- Example value for the header
@ApiExcludeEndpoint()
Excludes an endpoint from the OpenAPI documentation.
Usage: Method
import { } from '@foadonis/openapi/decorators'
export default class {
@()
() {
// This endpoint will not appear in the OpenAPI documentation
}
}@ApiExtension()
Adds custom extensions to the OpenAPI documentation. Extensions are prefixed with x- in the OpenAPI specification.
Usage: Method
import { } from '@foadonis/openapi/decorators'
export default class {
@('x-code-samples', [
{
: 'JavaScript',
: 'fetch("/posts")',
},
])
() {}
}Options:
name: string- The extension name (without thex-prefix)value: any- The extension value
Controller Decorators
These decorators can be applied to controllers or methods to configure behavior across multiple operations.
@ApiTags()
Assigns tags to operations for grouping and categorization in the documentation. Can be applied to specific operations or on the controller.
Usage: Method / Controller
import { } from '@foadonis/openapi/decorators'
@('Users')
export default class {
@('Public', 'Demo')
() {}
}Options:
...tags: string[]- One or more tag names
@ApiProduces()
Specifies the MIME types that an operation can produce. Can be applied to specific operations or on the controller.
Usage: Method / Controller
import { } from '@foadonis/openapi/decorators'
@('application/json')
export default class {
@('image/png', 'image/jpeg')
() {}
}Options:
...mimeTypes: string[]- One or more MIME types (e.g., 'application/json', 'text/html')
@ApiConsumes()
Specifies the MIME types that an operation can consume. Can be applied to specific operations or on the controller.
Usage: Method / Controller
import { } from '@foadonis/openapi/decorators'
@('application/json')
export default class {
@('multipart/form-data')
() {}
}Options:
...mimeTypes: string[]- One or more MIME types (e.g., 'application/json', 'multipart/form-data')
@ApiSecurity()
Defines security requirements for operations. The security mechanisms must be defined in your OpenAPI configuration.
Usage: Method / Controller
import { } from '@foadonis/openapi/decorators'
@('bearer')
export default class {
@('basic')
() {}
}Options:
name: string- The name of the security scheme (must be defined inconfig/openapi.ts)
@ApiBearerAuth()
Indicates that the operation uses Bearer Token authentication. This is a convenience decorator for @ApiSecurity('bearer').
Usage: Method / Controller
import { } from '@foadonis/openapi/decorators'
@()
export default class {}Options:
name?: string- Optional name of the bearer auth scheme (default: 'bearer')
@ApiBasicAuth()
Indicates that the operation uses Basic authentication. This is a convenience decorator for @ApiSecurity('basic').
Usage: Method / Controller
import { } from '@foadonis/openapi/decorators'
@()
export default class {}Options:
name?: string- Optional name of the basic auth scheme (default: 'basic')
@ApiOAuth2()
Indicates that the operation uses OAuth2 authentication. This is a convenience decorator for @ApiSecurity('oauth2').
Usage: Method / Controller
import { } from '@foadonis/openapi/decorators'
@('users:read', 'global:read')
export default class {}Options:
...scopes: string[]- One or more OAuth2 scopes required for the operation
@ApiExtraModels()
Registers additional models that are not directly referenced in the operation but are used in the API. This is useful for union types, discriminated unions, or models referenced only in error responses.
Usage: Method / Controller
import { , } from '@foadonis/openapi/decorators'
import from '#schemas/error_response'
@()
export default class {
@({
: 400,
: ,
})
() {}
}Options:
...models: Type[]- One or more model classes to register
Model Decorators
These decorators are used on model classes and their properties to define schemas.
@ApiProperty()
Defines a property in a model, specifying its schema and description. The type is automatically inferred from the TypeScript type when possible.
Usage: Property
import { } from '@foadonis/openapi/decorators'
export default class {
@()
declare : number
@({
: 6,
: 255,
: 'How to create REST APIs with Adonis',
})
declare : string
}Options:
type?: Type | (() => Type)- Explicit type (use a function for circular dependencies)description?: string- A description of the propertyrequired?: boolean- Whether the property is required (default: true)example?: any- Example valueenum?: string[] | Enum- Array of allowed values or TypeScript enumschema?: SchemaObject- Raw OpenAPI schema object- All standard OpenAPI Schema properties (e.g.,
minLength,maxLength,minimum,maximum,format, etc.)
@ApiPropertyOptional()
Defines an optional property in a model. This is a convenience decorator equivalent to @ApiProperty({ required: false }).
Usage: Property
import { , } from '@foadonis/openapi/decorators'
export default class {
@()
declare : number
@()
declare : string
@({ : })
declare ?: string
}Options:
- Same as
@ApiProperty(), butrequireddefaults tofalse
@ApiHideProperty()
Hides a property from the OpenAPI documentation. Useful for internal properties that should not be exposed in the API schema.
Usage: Property
import { , } from '@foadonis/openapi/decorators'
export default class {
@()
declare : number
@()
declare : string
}Decorator Reference Table
| Decorator | Usage Level | Description |
|---|---|---|
@ApiOperation() | Method | Configures operation details |
@ApiResponse() | Method / Controller | Describes possible responses |
@ApiBody() | Method | Describes request body |
@ApiParam() | Method | Describes path parameters |
@ApiQuery() | Method | Describes query parameters |
@ApiHeader() | Method / Controller | Describes header parameters |
@ApiExcludeEndpoint() | Method | Excludes endpoint from documentation |
@ApiExtension() | Method | Adds custom extensions |
@ApiTags() | Method / Controller | Assigns tags for categorization |
@ApiProduces() | Method / Controller | Specifies MIME types produced |
@ApiConsumes() | Method / Controller | Specifies MIME types consumed |
@ApiSecurity() | Method / Controller | Defines security requirements |
@ApiBearerAuth() | Method / Controller | Bearer token authentication |
@ApiBasicAuth() | Method / Controller | Basic authentication |
@ApiOAuth2() | Method / Controller | OAuth2 authentication |
@ApiExtraModels() | Method / Controller | Registers additional models |
@ApiProperty() | Property | Defines a model property |
@ApiPropertyOptional() | Property | Defines an optional model property |
@ApiHideProperty() | Property | Hides a property from documentation |