Swagger-ui-express 模块,仅实例化最后定义的文档 [英] Swagger-ui-express module, instantiates only the last defined document

查看:48
本文介绍了Swagger-ui-express 模块,仅实例化最后定义的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Typescripted nodejs server and i'm trying to define diffrent swagger paths for seperated controllers, but the swagger-ui-express module seems to only show the last defined doc in the specific route.

index.ts 用于 X 组控制器

index.ts for X group of controllers

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));

index.ts 用于 Y 组控制器

index.ts for Y group of controllers

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;

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

api.ts grouping all groups of controllers

'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;

server.ts

/**
 * Primary app routes.
 */
app.use('/api', apiRoutes);

swaggerDoc 之一的示例

example of one of the swaggerDoc's

export const botSwaggerDoc = {
    'swagger': '2.0',
    'info': {
        'version': '1.0.0',
        'title': 'Cupo embed chat bot API',
        'license': {
            'name': 'Internal use only'
        }

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

the swagger-ui-express module only use the last defined document as if the server keeps reference to that document...

推荐答案

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

I was able to get around this by serving up the HTML directly for each individual api. See below:

// 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);
  });
  
  

对于 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);
  });
  
  

来源:https://github.com/scottie1984/swagger-ui-快递/问题/65

这篇关于Swagger-ui-express 模块,仅实例化最后定义的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆