Skip to content

url: fix format when query is not an object #6005

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ Url.prototype.format = function() {
var pathname = this.pathname || '';
var hash = this.hash || '';
var host = false;
var query = '';
var query;

if (this.host) {
host = auth + this.host;
Expand All @@ -563,6 +563,8 @@ Url.prototype.format = function() {

if (this.query !== null && typeof this.query === 'object')
query = querystring.stringify(this.query);
else
Copy link
Member

Choose a reason for hiding this comment

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

Please add a typeof check to ensure that a string (and not something else) is passed.

Copy link
Member

Choose a reason for hiding this comment

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

rather than that, coercing to a string with '' + this.query may be ok.

Copy link
Member

Choose a reason for hiding this comment

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

er.. scratch that...

else if (typeof this.query !== 'undefined')
  query = '' + this.query;

query = this.query;

var search = this.search || (query && ('?' + query)) || '';

Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1586,3 +1586,6 @@ for (let i = 0; i < throws.length; i++) {
}
assert(url.format('') === '');
assert(url.format({}) === '');

// https://github.com/nodejs/node/issues/6004
assert.equal(url.format({pathname:'/foo', query: 'bar=baz'}), '/foo?bar=baz');
Copy link
Contributor

Choose a reason for hiding this comment

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

minor nit: there should be a space after pathname: