简体   繁体   English

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

[英]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.

相关问题 swagger-ui-express 不显示响应 - swagger-ui-express not displaying response 在swagger-ui-express中如何解决CORS问题 - How to solve CORS issue in swagger-ui-express swagger-ui-express未在浏览器中呈现,但在Postma中具有响应正文 - swagger-ui-express is not rendering in browser but has response body in Postma swagger-ui-express 不同 API 文档的多个路由 - swagger-ui-express Multiple Routes for Different API Documentation Swagger-UI-Express 未在 Azure 应用服务中加载:404 Not Found - Swagger-UI-Express not loading in Azure App Service: 404 Not Found 如何使用 swagger-ui-express 加载 YAML 文件? - How to load a YAML file with swagger-ui-express? Swagger - 正文中没有发送数据(swagger-jsdocs,swagger-ui-express) - Swagger - No data sent in body (swagger-jsdocs, swagger-ui-express) 如何在使用 swagger-ui-express 和 swagger-jsdoc 时正确使用 swagger 文件中的 $ref - How to use $ref in swagger file properly while working with swagger-ui-express and swagger-jsdoc 如何(自动)列出 Swagger UI 的所有路由(swagger-ui-express) - How to (auto) list all route to Swagger UI (swagger-ui-express) 托管在 aws beanstalk 上的 Nodejs express api swagger-ui-express API 文档上的 Cors 问题 - Cors issues on Nodejs express api swagger-ui-express API documentation hosted on aws beanstalk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM