【问题标题】:How to include only specific module in nestjs swagger如何在nestjs swagger中仅包含特定模块
【发布时间】:2021-12-31 12:22:44
【问题描述】:
const app = await NestFactory.create(AppModule);
app.useGlobalFilters(new AllExceptionsFilter());
app.enableCors();
app.useGlobalPipes(new ValidationPipe());

const config = new DocumentBuilder()
               .setTitle('API')
               .setDescription('Node Api to connec')
               .setVersion('1.0.0')
               .build();
const document = `SwaggerModule.createDocument`(app, config);
SwaggerModule.setup('api', app, document);

await app.listen(8000);

我有一些模块,如用户模块(UserModule)、驱动模块(DriverModule)。 我只希望驱动模块的 api 以 swagger 显示。

我知道SwaggerModule.createDocument 有 SwaggerOption 的第三个参数。

const option:SwaggerDocumentOptions = {
 include: [() => DriversModule],
 deepScanRoutes: true
}
const document = SwaggerModule.createDocument(app, config, option);

但是在这样写之后,Swagger 并没有显示任何 api。我从 UI 中的招摇中收到 No operations defined in spec! 消息。 我无法弄清楚我做错了什么。

【问题讨论】:

    标签: nestjs nestjs-swagger


    【解决方案1】:
    const option:SwaggerDocumentOptions = {
      include: [() => DriversModule],
      deepScanRoutes: true
    }
    const document = SwaggerModule.createDocument(app, config, option);
    

    问题在于包含属性。我给出了一个返回模块的函数。当我直接编写模块时,它就可以工作了。

    const option:SwaggerDocumentOptions = {
      include: [DriversModule],
      deepScanRoutes: true
    }
    const document = SwaggerModule.createDocument(app, config, option);
    

    这会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-22
      • 2021-09-18
      • 1970-01-01
      • 2014-03-18
      • 2013-10-28
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      相关资源
      最近更新 更多