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

fix: support express@5 router #59

Merged
merged 2 commits into from
Feb 27, 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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
node-version: [16.x, 18.x, 20.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
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
Loading