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 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 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 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);
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'
}
the swagger-ui-express module only use the last defined document as if the server keeps reference to that document...
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);
});
and for the Y group of controllers:
// 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With