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

app/controllers/posts_controller.ts
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 does
  • description?: string - A verbose explanation of the operation behavior
  • operationId?: string - Unique string used to identify the operation
  • deprecated?: boolean - Declares this operation to be deprecated
  • tags?: 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

app/controllers/posts_controller.ts
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 response
  • type?: Type | Type[] - The response type or array of types
  • mediaType?: 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

app/controllers/posts_controller.ts
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 body
  • mediaType?: string - The media type of the request body (e.g., 'application/json', 'multipart/form-data')
  • required?: boolean - Whether the request body is required
  • schema?: SchemaObject - Raw OpenAPI schema object

@ApiParam()

Describes a single path parameter for an operation, including its name, location, and schema.

Usage: Method

app/controllers/posts_controller.ts
import {  } from '@foadonis/openapi/decorators'

export default class  {
  @({
    : 'id',
    : 'The post identifier',
    : ,
    : true,
  })
  () {}
}

Options:

  • name: string - The name of the parameter
  • description?: string - A description of the parameter
  • type?: Type - The parameter type
  • required?: boolean - Whether the parameter is required (default: true for path parameters)
  • schema?: SchemaObject - Raw OpenAPI schema object
  • example?: any - Example value for the parameter

@ApiQuery()

Describes a query parameter for an operation.

Usage: Method

app/controllers/posts_controller.ts
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 parameter
  • description?: string - A description of the parameter
  • type?: Type - The parameter type
  • required?: boolean - Whether the parameter is required (default: false)
  • enum?: string[] | Enum - Array of allowed values or TypeScript enum
  • schema?: SchemaObject - Raw OpenAPI schema object
  • example?: 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

app/controllers/users_controller.ts
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 header
  • description?: string - A description of the header
  • required?: boolean - Whether the header is required (default: false)
  • type?: Type - The header type
  • schema?: SchemaObject - Raw OpenAPI schema object
  • example?: any - Example value for the header

@ApiExcludeEndpoint()

Excludes an endpoint from the OpenAPI documentation.

Usage: Method

app/controllers/posts_controller.ts
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

app/controllers/posts_controller.ts
import {  } from '@foadonis/openapi/decorators'

export default class  {
  @('x-code-samples', [
    {
      : 'JavaScript',
      : 'fetch("/posts")',
    },
  ])
  () {}
}

Options:

  • name: string - The extension name (without the x- 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

app/controllers/users_controller.ts
import {  } from '@foadonis/openapi/decorators'

@('Users')
export default class  {
  @('Public', 'Demo')
  () {}
}

Options:

  • ...tags: string[] - One or more tag names
Tags will automatically be applied to your controllers using its name.

@ApiProduces()

Specifies the MIME types that an operation can produce. Can be applied to specific operations or on the controller.

Usage: Method / Controller

app/controllers/files_controller.ts
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

app/controllers/files_controller.ts
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

app/controllers/users_controller.ts
import {  } from '@foadonis/openapi/decorators'

@('bearer')
export default class  {
  @('basic')
  () {}
}

Options:

  • name: string - The name of the security scheme (must be defined in config/openapi.ts)

@ApiBearerAuth()

Indicates that the operation uses Bearer Token authentication. This is a convenience decorator for @ApiSecurity('bearer').

Usage: Method / Controller

app/controllers/users_controller.ts
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

app/controllers/users_controller.ts
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

app/controllers/users_controller.ts
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

app/controllers/posts_controller.ts
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

app/models/post.ts
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 property
  • required?: boolean - Whether the property is required (default: true)
  • example?: any - Example value
  • enum?: string[] | Enum - Array of allowed values or TypeScript enum
  • schema?: 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

app/models/post.ts
import { ,  } from '@foadonis/openapi/decorators'

export default class  {
  @()
  declare : number

  @()
  declare : string

  @({ :  })
  declare ?: string
}

Options:

  • Same as @ApiProperty(), but required defaults to false

@ApiHideProperty()

Hides a property from the OpenAPI documentation. Useful for internal properties that should not be exposed in the API schema.

Usage: Property

app/models/post.ts
import { ,  } from '@foadonis/openapi/decorators'

export default class  {
  @()
  declare : number

  @()
  declare : string
}

Decorator Reference Table

DecoratorUsage LevelDescription
@ApiOperation()MethodConfigures operation details
@ApiResponse()Method / ControllerDescribes possible responses
@ApiBody()MethodDescribes request body
@ApiParam()MethodDescribes path parameters
@ApiQuery()MethodDescribes query parameters
@ApiHeader()Method / ControllerDescribes header parameters
@ApiExcludeEndpoint()MethodExcludes endpoint from documentation
@ApiExtension()MethodAdds custom extensions
@ApiTags()Method / ControllerAssigns tags for categorization
@ApiProduces()Method / ControllerSpecifies MIME types produced
@ApiConsumes()Method / ControllerSpecifies MIME types consumed
@ApiSecurity()Method / ControllerDefines security requirements
@ApiBearerAuth()Method / ControllerBearer token authentication
@ApiBasicAuth()Method / ControllerBasic authentication
@ApiOAuth2()Method / ControllerOAuth2 authentication
@ApiExtraModels()Method / ControllerRegisters additional models
@ApiProperty()PropertyDefines a model property
@ApiPropertyOptional()PropertyDefines an optional model property
@ApiHideProperty()PropertyHides a property from documentation

On this page