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: fastify issue 3129 #194

Merged
merged 1 commit into from
Jun 15, 2021
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
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
Copy link
Contributor Author

@climba03003 climba03003 Jun 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the same pattern at https://github.com/delvedor/find-my-way/blob/master/index.js#L380 for consistency. And only unconstrainedHandler contains what we need to compute paramsObj.


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
)
})