Skip to content

Commit

Permalink
fix: support express@5 router
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd committed Feb 27, 2024
1 parent f2e054e commit 0265a80
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
13 changes: 12 additions & 1 deletion lib/generate-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ module.exports = function generateDocument (baseDocument, router, basePath) {

function iterateStack (path, routeLayer, layer, cb) {
cb(path, routeLayer, layer)

if (layer.name === 'router') {
layer.handle.stack.forEach(l => {
path = path || ''
Expand Down Expand Up @@ -113,6 +112,16 @@ function processComplexMatch (thing, keys) {

// https://github.com/expressjs/express/issues/3308#issuecomment-300957572
function split (thing, keys) {
// In express v5 the router layers regexp ([email protected])
// has some additional handling for end of lines, remove those
//
// layer.regexp
// v4 ^\\/sub-route\\/?(?=\\/|$)
// v5 ^\\/sub-route(?:\\/(?=$))?(?=\\/|$)
//
// l.regexp
// v4 ^\\/endpoint\\/?$
// v5 ^\\/endpoint(?:\\/)?$
if (typeof thing === 'string') {
return thing.split('/')
} else if (thing.fast_slash) {
Expand All @@ -122,6 +131,8 @@ function split (thing, keys) {
.toString()
.replace('\\/?', '')
.replace('(?=\\/|$)', '$')
// Added this line to catch the express v5 case after the v4 part is stripped off
.replace('(?:\\/(?=$))?$', '$')
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\/]|[^.*+?^${}()|[\]\\/])*)\$\//)
return match
? match[1].replace(/\\(.)/g, '$1').split('/')
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
},
"scripts": {
"test": "standard && mocha",
"prepublishOnly": "npm t",
"prepublishOnly": "EXPRESS_MAJOR=4 mocha && EXPRESS_MAJOR=5 mocha",
"postpublish": "git push origin && git push origin --tags"
},
"devDependencies": {
"express": "^4.18.2",
"express4": "github:expressjs/express#4.18.3-staging",
"express5": "npm:express@^5.0.0-beta.1",
"mocha": "^10.2.0",
"standard": "^17.1.0",
"supertest": "^6.3.3"
Expand Down
11 changes: 10 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ const { suite, test } = require('mocha')
const assert = require('assert')
const util = require('util')
const supertest = require('supertest')
const express = require('express')
const SwaggerParser = require('swagger-parser')
const openapi = require('../')
const { name } = require('../package.json')
const YAML = require('yaml')

// We support testing with different major versions of express
let spec = 'express'
if (process.env.EXPRESS_MAJOR) {
if (!['4', '5'].includes(process.env.EXPRESS_MAJOR)) {
throw new Error('EXPRESS_MAJOR contained an invalid value')
}
spec += process.env.EXPRESS_MAJOR
}
const express = require(spec)

function logDocument (doc) {
console.log(util.inspect(doc, { depth: null }))
}
Expand Down

0 comments on commit 0265a80

Please sign in to comment.