Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add support for routes passed in as arrays #58

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})
})
}