Skip to content

Commit

Permalink
feat: Add support for the regex *
Browse files Browse the repository at this point in the history
  • Loading branch information
Megapixel99 committed Aug 29, 2024
1 parent e3b4d2f commit 2f31d25
Show file tree
Hide file tree
Showing 3 changed files with 406 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/generate-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ module.exports = function generateDocument (baseDocument, router, basePath) {
if (routeLayer && routeLayer.keys && routeLayer.keys.length) {
const keys = {}

const params = routeLayer.keys.map((k) => {
const params = routeLayer.keys.map((k, i) => {
const prev = i > 0 && routeLayer.keys[i - 1]
// do not count parameters without a name if they are next to a named parameter
if (typeof k.name === 'number' && prev && prev.offset + prev.name.length + 1 >= k.offset) {
return null
}
let param
if (schema.parameters) {
param = schema.parameters.find((p) => p.name === k.name && p.in === 'path')
Expand All @@ -45,6 +50,7 @@ module.exports = function generateDocument (baseDocument, router, basePath) {
schema: k.schema || { type: 'string' }
}, param || {})
})
.filter((e) => e)

if (schema.parameters) {
schema.parameters.forEach((p) => {
Expand All @@ -55,7 +61,7 @@ module.exports = function generateDocument (baseDocument, router, basePath) {
}

operation.parameters = params
path = pathToRegexp.compile(path)(keys, { encode: (value) => value })
path = pathToRegexp.compile(path.replace(/\*|\(\*\)/g, '(.*)'))(keys, { encode: (value) => value })
}

doc.paths[path] = doc.paths[path] || {}
Expand Down
Loading

0 comments on commit 2f31d25

Please sign in to comment.