Skip to content

Commit

Permalink
feat!: add support for routes passed in as arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Megapixel99 committed Jan 17, 2024
1 parent 12d91c2 commit 5543cea
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/generate-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ function iterateStack (path, routeLayer, layer, cb) {
if (!layer.route) {
return
}
if (Array.isArray(layer.route.path)) {
const r = layer.regexp.toString()
layer.route.path.forEach((p, i) => iterateStack(path + p, layer, {
...layer,
// Chacking if p is a string here since p may be a regex expression
keys: layer.keys.filter((k) => typeof p === 'string' ? p.includes(`/:${k.name}`) : false),
// There may be an issue here if the regex has a '|', but that seems to only be the case with user defined regex
regexp: new RegExp(`(${r.substring(2, r.length - 3).split('|')[i]})`),
route: { ...layer.route, path: '' }
}, cb))
return
}
layer.route.stack.forEach((l) => iterateStack(path + layer.route.path, layer, l, cb))
}

Expand Down
31 changes: 31 additions & 0 deletions test/_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,36 @@ module.exports = function () {
done()
})
})

test('serve routes in an array as different routes', function (done) {
const app = express()

const oapi = openapi()
app.use(oapi)
app.get(['/route/:a', '/route/b', '/routeC'], oapi.path({
summary: 'Test route.',
responses: {
200: {
content: {
'application/json': {
schema: {
type: 'string'
}
}
}
}
}
}))

supertest(app)
.get(`${openapi.defaultRoutePrefix}.json`)
.expect(200, (err, res) => {
assert(!err, err)
assert.strictEqual(Object.keys((res.body.paths))[0], '/route/{a}')
assert.strictEqual(Object.keys((res.body.paths))[1], '/route/b')
assert.strictEqual(Object.keys((res.body.paths))[2], '/routeC')
done()
})
})
})
}

0 comments on commit 5543cea

Please sign in to comment.