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

Versioned route matching throws TypeError when "maxV" is not set #1380

Closed
kusor opened this issue Jun 23, 2017 · 4 comments
Closed

Versioned route matching throws TypeError when "maxV" is not set #1380

kusor opened this issue Jun 23, 2017 · 4 comments

Comments

@kusor
Copy link
Contributor

kusor commented Jun 23, 2017

Bug Report

When we have two routes applicable to the same request, one typed and the other versioned and not typed, a TypeError exception is thrown.

Restify Version

Every one since v2.8.5, exactly since commit Route matching should only prefer later routes if version is greater

Node.js Version

Any.

Expected behaviour

Exceptions should not be thrown

Actual behaviour

TypeError: Invalid Version: undefined
    at new SemVer (/Users/pedropc/work/node_modules/restify/node_modules/semver/semver.js:281:11)
    at SemVer.compare (/Users/pedropc/work/node_modules/restify/node_modules/semver/semver.js:348:13)
    at compare (/Users/pedropc/work/node_modules/restify/node_modules/semver/semver.js:571:31)
    at Function.gt (/Users/pedropc/work/node_modules/restify/node_modules/semver/semver.js:600:10)
    at /Users/pedropc/work/node_modules/restify/lib/router.js:497:38
    at Array.forEach (native)
    at Router.find (/Users/pedropc/work/node_modules/restify/lib/router.js:493:24)
    at Server._route (/Users/pedropc/work/node_modules/restify/lib/server.js:753:21)
    at routeAndRun (/Users/pedropc/work/node_modules/restify/lib/server.js:711:14)
    at Server._handle (/Users/pedropc/work/node_modules/restify/lib/server.js:731:9)

Repro case

var restify = require('restify');

var server = restify.createServer({
    name: 'myapp',
    version: '1.0.0'
});
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());


server.post({
    path: '/:param_one/path/[0-f]+/:param_two/word',
    contentType: 'application/json'
}, function (req, res, next) {
    req.log.warn(req.headers);
    res.setHeader('Content-Length', '0');
    res.send(204);
    next();
});


server.post({
    path: '/:param_one/path/[0-f]+/:param_two/:param_three'
}, function (req, res, next) {
    req.log.warn(req.headers);
    res.setHeader('Content-Length', '0');
    res.send(204);
    next();
});

server.listen(8080, '127.0.0.1', function () {
    console.log('%s listening at %s', server.name, server.url);
});

var assert = require('assert');
var clients = require('restify-clients');

var client = clients.createJsonClient({
    url: 'http://localhost:8080',
    version: '~1.0'
});

client.post({
    path: '/foo/path/6/bar/word',
    accept: 'application/json',
    contentType: 'application/json'
}, function (err, req, res, obj) {
    assert.ifError(err);
    console.log('Server returned: %j', obj);
});

Cause

The bug problem is into version checking here: https://github.com/restify/node-restify/blob/5.x/lib/router.js#L506-L519.
The first time we hit semver.gt(v, maxV), the value for maxV is undefined.

Are you willing and able to fix this?

Yes

@retrohacker
Copy link
Member

Hey @kusor!

Thank you for opening a thorough bug report and an accompanying PR! I'm tagging this as an unverified bug until a maintainer (probably me 😉) runs the repro case. ❤️

@kusor
Copy link
Contributor Author

kusor commented Jun 23, 2017

@retrohacker thanks for taking a look!

@retrohacker
Copy link
Member

Reproduced locally

@retrohacker
Copy link
Member

Closed by #1381

trentm added a commit to TritonDataCenter/manta-muskie that referenced this issue Apr 15, 2020
…2999 (#62)

Upgrade to restify 6.x to get the fix for restify/node-restify#1380 which breaks restify API
versioning in restify 4.x.

Co-authored-by: Trent Mick <trentm@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants