[英]Swagger-ui-express module, instantiates only the last defined document
我有一個Typescripted nodejs服務器,我正在嘗試為分離的控制器定義不同的swagger路徑,但swagger-ui-express模塊似乎只顯示特定路由中最后定義的doc。
用於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));
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對所有控制器組進行分組
'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的例子
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模塊僅使用最后定義的文檔,就好像服務器保持對該文檔的引用一樣......
通過直接為每個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); });
對於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-express/issues/65
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.