export const initializeSwaggerDoc = (app: INestApplication) => {
const contextPath = process.env.CONTEXT_PATH || 'v1';
app.setGlobalPrefix(contextPath);
const config = new DocumentBuilder()
.setTitle('Test API')
.setDescription('Draft version')
.setVersion('1.0')
// .addServer(process.env.BACKEND_ENV)
.setBasePath(contextPath)
.build();
const options: SwaggerDocumentOptions = {
include: [AppModule],
deepScanRoutes: true,
operationIdFactory: (controllerKey: string, methodKey: string) => methodKey,
};
const documentFactory = SwaggerModule.createDocument(app, config, options);
// The ui will be generated on /v1/open-api
// The open api spec will be generated on /v1/open-api-json
SwaggerModule.setup(`${contextPath}/open-api`, app, documentFactory, {
swaggerOptions: {
tagsSorter: 'alpha',
operationsSorter: 'alpha',
},
});
};
main.ts
initializeSwaggerDoc(app);
The SwaggerModule searches for all @Body(), @Query(), and @Param() decorators in route handlers to generate the API document
In order to make the class properties visible to the SwaggerModule, we have to either annotate them with the @ApiProperty() decorator or use the CLI plugin (read more in the Plugin section) which will do it automatically
Api Property
To override the attributes of object, it can be declared through decorator