Swagger-ui-express 模块,仅实例化最后定义的文档

Dan*_*zer 5 singleton node.js swagger typescript swagger-ui

我有一个 Typescripted nodejs 服务器,我正在尝试为单独的控制器定义不同的 swagger 路径,但是 swagger-ui-express 模块似乎只显示特定路径中最后定义的文档。

X 组控制器的 index.ts

import express from 'express';
import passport from 'passport';
const router = express.Router();
// import all bot routes
import { authRoute } from './auth';
import { botCrudRoute } from './bot-crud';
import { aiRoutes } from './ai';
import { categoryCrudRoute } from './categories-crud';

const swaggerUi = require('swagger-ui-express');
import { botSwaggerDoc } from './swagger';

const swaggerDoc = botSwaggerDoc;

const swaggerUiOpts = {
    explorer: false
};

// Swagger setup
router.use('/api-docs', swaggerUi.serve);
router.get('/api-docs', swaggerUi.setup(swaggerDoc, swaggerUiOpts));
Run Code Online (Sandbox Code Playgroud)

用于 Y 组控制器的 index.ts

import express from 'express';
const router = express.Router();
// import all bot routes

const swaggerUi = require('swagger-ui-express');
import { adminSwaggerDoc } from './swagger';

const swaggerDoc = adminSwaggerDoc;

const swaggerUiOpts = {
    explorer: false
};

// Swagger setup
router.use('/api-docs', swaggerUi.serve);
router.get('/api-docs', swaggerUi.setup(swaggerDoc, swaggerUiOpts));

export const adminRoutes = router;
Run Code Online (Sandbox Code Playgroud)

api.ts 对所有控制器组进行分组

'use strict';

import express from 'express';
import { Response, Request, NextFunction } from 'express';
import { adminRoutes } from './admin';
import { botRoutes } from './bot';
// import { onboardRoutes } from './onboard';

const router = express.Router();

// router.use('/onboard', onboardRoutes);
router.use('/bot', botRoutes);
router.use('/admin', adminRoutes);

export const apiRoutes = router;
Run Code Online (Sandbox Code Playgroud)

服务器.ts

/**
 * Primary app routes.
 */
app.use('/api', apiRoutes);
Run Code Online (Sandbox Code Playgroud)

swaggerDoc 之一的示例

export const botSwaggerDoc = {
    'swagger': '2.0',
    'info': {
        'version': '1.0.0',
        'title': 'Cupo embed chat bot API',
        'license': {
            'name': 'Internal use only'
        }
Run Code Online (Sandbox Code Playgroud)

swagger-ui-express 模块只使用最后定义的文档,就好像服务器保持对该文档的引用......

Joh*_*ynh 6

我能够通过直接为每个单独的 api 提供 HTML 来解决这个问题。见下文:

// index.ts for X group of controllers

  const apiV1Html = swaggerUi.generateHTML(
    v1SwaggerDocument,
  );
  router.use(
    '/docs',
    swaggerUi.serveFiles(v1SwaggerDocument),
  );
  router.get('/docs', (req: any, res: any) => {
    res.send(apiV1Html);
  });
  
  
Run Code Online (Sandbox Code Playgroud)

对于 Y 组控制器:

// index.ts for y group of controllers

  const apiV2Html = swaggerUi.generateHTML(
    v2SwaggerDocument,
  );
  router.use(
    '/docs',
    swaggerUi.serveFiles(v2SwaggerDocument),
  );
  router.get('/docs', (req: any, res: any) => {
    res.send(apiV2Html);
  });
  
  
Run Code Online (Sandbox Code Playgroud)

来源:https : //github.com/scottie1984/swagger-ui-express/issues/65