Skip to content

Commit 466298c

Browse files
committed
Fix issue #78: Separate OpenAPI specs for multiple middleware instances
- Store owned routes directly on middleware instance using Set - Filter routes to exclude those owned by other OpenAPI middlewares - Ensures each middleware only includes its own routes while preserving sub-router compatibility
1 parent b56f6ee commit 466298c

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = function ExpressOpenApi (_routePrefix, _doc, _opts) {
3939
// Where the magic happens
4040
const middleware = function OpenApiMiddleware (req, res, next) {
4141
if (isFirstRequest) {
42-
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath)
42+
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath, middleware)
4343
isFirstRequest = false
4444
}
4545

@@ -48,8 +48,8 @@ module.exports = function ExpressOpenApi (_routePrefix, _doc, _opts) {
4848

4949
// Expose the current document and prefix
5050
middleware.routePrefix = routePrefix
51-
middleware.document = generateDocument(doc, undefined, opts.basePath)
52-
middleware.generateDocument = generateDocument
51+
middleware.document = generateDocument(doc, undefined, opts.basePath, middleware)
52+
middleware.generateDocument = (doc, router, basePath) => generateDocument(doc, router, basePath, middleware)
5353
middleware.options = opts
5454

5555
// Add a path schema to the document
@@ -59,6 +59,7 @@ module.exports = function ExpressOpenApi (_routePrefix, _doc, _opts) {
5959
}
6060

6161
setSchema(schemaMiddleware, schema)
62+
schemaMiddleware._ownerMiddleware = middleware
6263
return schemaMiddleware
6364
}
6465

@@ -73,6 +74,7 @@ module.exports = function ExpressOpenApi (_routePrefix, _doc, _opts) {
7374
}
7475

7576
setSchema(validSchemaMiddleware, schema)
77+
validSchemaMiddleware._ownerMiddleware = middleware
7678
return validSchemaMiddleware
7779
}
7880

@@ -131,13 +133,13 @@ module.exports = function ExpressOpenApi (_routePrefix, _doc, _opts) {
131133

132134
// OpenAPI document as json
133135
router.get(`${routePrefix}.json`, (req, res) => {
134-
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath)
136+
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath, middleware)
135137
res.json(middleware.document)
136138
})
137139

138140
// OpenAPI document as yaml
139141
router.get([`${routePrefix}.yaml`, `${routePrefix}.yml`], (req, res) => {
140-
const jsonSpec = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath)
142+
const jsonSpec = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath, middleware)
141143
const yamlSpec = YAML.stringify(jsonSpec)
142144

143145
res.type('yaml')
@@ -146,7 +148,7 @@ module.exports = function ExpressOpenApi (_routePrefix, _doc, _opts) {
146148

147149
router.get(`${routePrefix}/components/:type/:name.json`, (req, res, next) => {
148150
const { type, name } = req.params
149-
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath)
151+
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath, middleware)
150152

151153
// No component by that identifer
152154
if (!middleware.document.components[type] || !middleware.document.components[type][name]) {
@@ -159,7 +161,7 @@ module.exports = function ExpressOpenApi (_routePrefix, _doc, _opts) {
159161

160162
// Validate full open api document
161163
router.get(`${routePrefix}/validate`, (req, res) => {
162-
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath)
164+
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath, middleware)
163165
SwaggerParser.validate(middleware.document, (err, api) => {
164166
if (err) {
165167
return res.json({

lib/generate-doc.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const pathToRegexp = require('path-to-regexp')
33
const minimumViableDocument = require('./minimum-doc')
44
const { get: getSchema, set: setSchema } = require('./layer-schema')
55

6-
module.exports = function generateDocument (baseDocument, router, basePath) {
6+
module.exports = function generateDocument (baseDocument, router, basePath, ownerMiddleware) {
77
// Merge document with select minimum defaults
88
const doc = Object.assign({
99
openapi: minimumViableDocument.openapi
@@ -23,6 +23,11 @@ module.exports = function generateDocument (baseDocument, router, basePath) {
2323
return
2424
}
2525

26+
// If ownerMiddleware is provided, only exclude routes owned by other OpenAPI middlewares
27+
if (ownerMiddleware && layer.handle._ownerMiddleware && layer.handle._ownerMiddleware !== ownerMiddleware) {
28+
return
29+
}
30+
2631
const operation = Object.assign({}, schema)
2732

2833
// Add route params to schema

0 commit comments

Comments
 (0)