[英]Swagger-ui-express module, instantiates only the last defined document
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. 我有一个Typescripted nodejs服务器,我正在尝试为分离的控制器定义不同的swagger路径,但swagger-ui-express模块似乎只显示特定路由中最后定义的doc。
index.ts for X group of controllers 用于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));
index.ts for Y group of controllers 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;
api.ts grouping all groups of controllers 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;
server.ts server.ts
/**
* Primary app routes.
*/
app.use('/api', apiRoutes);
example of one of the swaggerDoc's 其中一个swaggerDoc的例子
export const botSwaggerDoc = {
'swagger': '2.0',
'info': {
'version': '1.0.0',
'title': 'Cupo embed chat bot API',
'license': {
'name': 'Internal use only'
}
the swagger-ui-express module only use the last defined document as if the server keeps reference to that document... swagger-ui-express模块仅使用最后定义的文档,就好像服务器保持对该文档的引用一样......
I was able to get around this by serving up the HTML directly for each individual api. 通过直接为每个api提供HTML,我能够解决这个问题。 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); });
and for the Y group of controllers: 对于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); });
Sources: https://github.com/scottie1984/swagger-ui-express/issues/65 来源: https : //github.com/scottie1984/swagger-ui-express/issues/65
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.