From 915456f5fbefb1e6881779b487e9c64473468df4 Mon Sep 17 00:00:00 2001 From: KaKa Date: Tue, 15 Jun 2021 16:42:38 +0800 Subject: [PATCH] fix: fastify issue 3129 (#194) --- index.js | 24 +++++++++++++++++----- test/fastify-issue-3129.test.js | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 test/fastify-issue-3129.test.js diff --git a/index.js b/index.js index 74478ec5..641f8623 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 @@ -539,7 +539,7 @@ 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) { @@ -547,11 +547,25 @@ Router.prototype._getWildcardNode = function (node, path, len) { ? 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 } } diff --git a/test/fastify-issue-3129.test.js b/test/fastify-issue-3129.test.js new file mode 100644 index 00000000..e632074c --- /dev/null +++ b/test/fastify-issue-3129.test.js @@ -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 + ) +})