I'm trying to find a solution to have multiple swaggerUi documentation with a single express app.
I'm using :
"typescript": "^2.5.2",
"swagger-tools": "^0.10.1",
"express": "^4.15.3",
"express-openapi": "^1.0.1",
My swagger doc file is partly generated with the project file architecture.
How can I do that ?
EDIT ---
For now I'm initializing swaggerUi like this :
const openapi = Openapi.initialize({
paths: openApiPaths,
expressApp,
swaggerApiDoc,
});
const openApiSpec: any = openapi.apiDoc;
app.use(swaggerUI(openApiSpec));
Where openApiPaths
contain the paths:{}
of swagger doc
A year late, 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);
With 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);
}
});
};
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