Multiple swaggerUi with one express application

I am trying to find a solution to work with multiple swaggerUi documents with one express application.

I use:

"typescript": "^2.5.2", "swagger-tools": "^0.10.1", "express": "^4.15.3", "express-openapi": "^1.0.1", 

My dag file of the swagger file is partially generated with the file architecture of the project.

How can i do this?

EDIT ---

Now I initialize swaggerUi as follows:

 const openapi = Openapi.initialize({ paths: openApiPaths, expressApp, swaggerApiDoc, }); const openApiSpec: any = openapi.apiDoc; app.use(swaggerUI(openApiSpec)); 

Where openApiPaths contains paths:{} a swagger document

+6
source share
2 answers

A year later, but still valid:

 async function initialize (app) { const authenticationSpec = path.join(__dirname,'api/authentication.yaml'); const authenticationMiddleware = await useSwaggerMiddlewares(authenticationSpec, { router: routerOptions, validator: validatorOptions, }); // NOTE the metadata must be mounted at root level! app.use(authenticationMiddleware.metadata); app.use('/v2/authentication', authenticationMiddleware.validator); app.use('/v2/authentication', authenticationMiddleware.router); app.use('/v2/authentication', authenticationMiddleware.ui); const mainSpec = path.join(__dirname,'api/swagger.yaml'); const mainMiddleware = await useSwaggerMiddlewares(mainSpec, { router: routerOptions, validator: validatorOptions, }); app.use(mainMiddleware.metadata); app.use(mainMiddleware.validator); app.use(mainMiddleware.router); app.use(mainMiddleware.ui); // Start the server http.createServer(app).listen(serverPort, () => console.log('server lintening on port ${serverPort}'); } initialize(app); 

Using useSwaggerMiddlewares defined here

 module.exports = function useSwaggerMiddlewares (swaggerSpec, { router: routerOptions, validator: validatorOptions, }) { return new Promise((resolve, reject) => { try { // The Swagger document (require it, build it programmatically, fetch it from a URL, ...) const spec = fs.readFileSync(swaggerSpec, 'utf8'); const swaggerDoc = jsyaml.safeLoad(spec); // Initialize the Swagger middleware swaggerTools.initializeMiddleware(swaggerDoc, (middleware) => { try { return resolve({ // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain metadata: middleware.swaggerMetadata(), // Validate Swagger requests validator: middleware.swaggerValidator(validatorOptions), // Route validated requests to appropriate controller router: middleware.swaggerRouter(routerOptions), // Serve the Swagger documents and Swagger UI ui: middleware.swaggerUi() }); } catch (error) { console.error(error); return reject(error); } }); } catch (error) { console.error(error); return reject(error); } }); }; 
+1
source

You can host multiple apis on the same express server, you just need to be sure basePath does not overlap.

ref: https://github.com/apigee-127/swagger-tools/issues/530

 swaggerTools.initializeMiddleware(doc1, function (mw1) { // Do stuff with first middleware swaggerTools.initializeMiddleware(doc2, function (mw2) { // Do stuff with second middleware // Repeat... }); }); 

ref: https://github.com/apigee-127/swagger-tools/issues/513

0
source

Source: https://habr.com/ru/post/1274810/


All Articles