I'm implementing a POST endpoint which takes such a DTO as an input:
export class GenericEventDto<D, A> {
@ApiProperty({ type: D }) // <- does not work
@Expose()
data: D
@ApiProperty({ type: A })
@Expose()
attributes: A // <- same
@ApiProperty()
@Expose()
messageId: string
}
In swagger docs data
and attributes
props appear to be empty object. Controller code looks like this:
export class Controller {
@ApiBody({ type: GenericEventDto<ConcreteData, ConcreteAttributes> })
async handleIncomingMessage(
@Body() event: GenericEventDto<ConcreteData, ConcreteAttributes>
): Promise<void> {
// do smth
}
}
Is it possible to have generic type in swagger spec? This GenericEventDto
will be reused later for other endpoints and I would like to avoid code duplication
The docs specifically mention about this
Since TypeScript does not store metadata about generics or interfaces, when you use them in your DTOs, SwaggerModule may not be able to properly generate model definitions at runtime
You'd need to make a mixin rather than a generic type. A mixin in this case is a function that returns a class definition