Skip to content

Commit

Permalink
fix: fastify issue 3129 (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 authored Jun 15, 2021
1 parent 59a954d commit 915456f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
24 changes: 19 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ Router.prototype.find = function find (method, path, derivedConstraints) {
if (node === null) {
node = currentNode.parametricBrother
if (node === null) {
return this._getWildcardNode(wildcardNode, originalPath, pathLenWildcard)
return this._getWildcardNode(wildcardNode, originalPath, pathLenWildcard, derivedConstraints, params)
}

var goBack = previousPath.charCodeAt(0) === 47 ? previousPath : '/' + previousPath
Expand Down Expand Up @@ -447,7 +447,7 @@ Router.prototype.find = function find (method, path, derivedConstraints) {
}

if (len !== prefixLen) {
return this._getWildcardNode(wildcardNode, originalPath, pathLenWildcard)
return this._getWildcardNode(wildcardNode, originalPath, pathLenWildcard, derivedConstraints, params)
}

// if exist, save the wildcard child
Expand Down Expand Up @@ -539,19 +539,33 @@ Router.prototype.find = function find (method, path, derivedConstraints) {
}
}

Router.prototype._getWildcardNode = function (node, path, len) {
Router.prototype._getWildcardNode = function (node, path, len, derivedConstraints, params) {
if (node === null) return null
var decoded = fastDecode(path.slice(-len))
if (decoded === null) {
return this.onBadUrl !== null
? this._onBadUrl(path.slice(-len))
: null
}
var handle = node.handlers[0]

var handle = derivedConstraints !== undefined ? node.getMatchingHandler(derivedConstraints) : node.unconstrainedHandler

if (handle !== null && handle !== undefined) {
var paramsObj = {}
if (handle.paramsLength > 0 && params !== null) {
var paramNames = handle.params

for (i = 0; i < handle.paramsLength; i++) {
paramsObj[paramNames[i]] = params[i]
}
}

// we must override params[*] to decoded
paramsObj['*'] = decoded

return {
handler: handle.handler,
params: { '*': decoded },
params: paramsObj,
store: handle.store
}
}
Expand Down
35 changes: 35 additions & 0 deletions test/fastify-issue-3129.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const t = require('tap')
const test = t.test
const FindMyWay = require('../')

test('contain param and wildcard together', t => {
t.plan(4)

const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})

findMyWay.on('GET', '/:lang/item/:id', (req, res, params) => {
t.same(params.lang, 'fr')
t.same(params.id, '12345')
})

findMyWay.on('GET', '/:lang/item/*', (req, res, params) => {
t.same(params.lang, 'fr')
t.same(params['*'], '12345/edit')
})

findMyWay.lookup(
{ method: 'GET', url: '/fr/item/12345', headers: {} },
null
)

findMyWay.lookup(
{ method: 'GET', url: '/fr/item/12345/edit', headers: {} },
null
)
})

0 comments on commit 915456f

Please sign in to comment.