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 846b035
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/generate-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ 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 = routeLayer.keys[i - 1]
if (!Number.isNaN(k.name) && prev?.offset + prev?.name?.length - 2 === 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 +49,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 +60,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
98 changes: 98 additions & 0 deletions test/_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,103 @@ module.exports = function () {
done()
})
})

test('serve routes with a * wildcard', function (done) {
const app = express()

const oapi = openapi()
app.use(oapi)
app.get(['/route/:param*'], 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/{param}')
assert.strictEqual(res.body.paths[Object.keys((res.body.paths))[0]].get.parameters.length, 1)
assert.strictEqual(res.body.paths[Object.keys((res.body.paths))[0]].get.parameters[0].in, 'path')
assert.strictEqual(res.body.paths[Object.keys((res.body.paths))[0]].get.parameters[0].name, 'param')
done()
})
})

test('serve routes with a * wildcard in parentheses', function (done) {
const app = express()

const oapi = openapi()
app.use(oapi)
app.get(['/route/:param(*)'], 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/{param}')
assert.strictEqual(res.body.paths[Object.keys((res.body.paths))[0]].get.parameters.length, 1)
assert.strictEqual(res.body.paths[Object.keys((res.body.paths))[0]].get.parameters[0].in, 'path')
assert.strictEqual(res.body.paths[Object.keys((res.body.paths))[0]].get.parameters[0].name, 'param')
done()
})
})

test('serve routes in an array as different routes when one route has a * wildcard', function (done) {
const app = express()

const oapi = openapi()
app.use(oapi)
app.get(['/route/:param*', '/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/{param}')
assert.strictEqual(res.body.paths[Object.keys((res.body.paths))[0]].get.parameters.length, 1)
assert.strictEqual(res.body.paths[Object.keys((res.body.paths))[0]].get.parameters[0].in, 'path')
assert.strictEqual(res.body.paths[Object.keys((res.body.paths))[0]].get.parameters[0].name, 'param')
assert.strictEqual(Object.keys((res.body.paths))[1], '/route/b')
assert.strictEqual(Object.keys((res.body.paths))[2], '/routeC')
done()
})
})
})
}

0 comments on commit 846b035

Please sign in to comment.