Types and paremeters
Classes as schemas
One of the most powerful feature of this library is the ability to use classes as schemas.
When using the @ApiProperty
decorator it will automatically add the property to the generated schema and try to infer the type from your Typescript type.
Advanced properties options
The @ApiProperty
decorator also accepts the same properties as the original OpenAPI Schema object, allowing you to extend the generated schema.
Optional properties
Due to the lack of informations provided by the Typescript metadata it is not possible to infer if a property is optional. For this you can either use the required
option or the @ApiOptionalProperty
decorator.
Explicit type
Sometimes the type of your property is too complex to be automatically inferred or might be serialized in a specific way. When it is the case, you should see a warning in the console. To resolve this, you simply have to define explicitly the type using the type
option.
You can create custom type loaders to automatically generate schemas for inferred custom types, more information on the openapi-metadata
documentation.
Circular dependencies
You may have circular dependencies in your schema, for example a Post
can have a Post
as a parent. In this situation, you must explicitly define the type using a Thunk
:
Primitives
Primitives can be defined by using their name as a string or its constructor.
Arrays
You can configure a type to be an array by simply wrapping your base type in an array.
Raw schema
Sometimes you may want to define a raw OpenAPI schema. For this you can use the schema
option.
Enums
An enum type can be defined by providing an array of the available items or a Typescript enum.