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

[BUGFIX] avoid tampering queryParam argument in RouterService#isActive #17571

Merged
merged 1 commit into from
May 27, 2020
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
3 changes: 3 additions & 0 deletions packages/@ember/-internals/routing/lib/services/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Evented } from '@ember/-internals/runtime';
import { assert } from '@ember/debug';
import { readOnly } from '@ember/object/computed';
import { assign } from '@ember/polyfills';
import Service from '@ember/service';
import { DEBUG } from '@glimmer/env';
import { Transition } from 'router_js';
Expand Down Expand Up @@ -319,6 +320,7 @@ export default class RouterService extends Service {
let hasQueryParams = Object.keys(queryParams).length > 0;

if (hasQueryParams) {
queryParams = assign({}, queryParams);
this._router._prepareQueryParams(
// UNSAFE: casting `routeName as string` here encodes the existing
// assumption but may be wrong: `extractRouteArgs` correctly returns it
Expand All @@ -334,6 +336,7 @@ export default class RouterService extends Service {
queryParams as QueryParam,
true /* fromRouterService */
);

return shallowEqual(queryParams, routerMicrolib.state!.queryParams);
}

Expand Down
25 changes: 25 additions & 0 deletions packages/ember/tests/routing/router_service_test/isActive_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,30 @@ moduleFor(
);
});
}

['@test RouterService#isActive does not alter query params hash'](assert) {
assert.expect(3);

this.add(
'controller:parent.child',
Controller.extend({
queryParams: ['sort', 'page'],
sort: 'ASC',
page: 1,
})
);

let qp = this.buildQueryParams({ sort: 'ascending' });

return this.visit('/')
.then(() => {
return this.routerService.transitionTo('parent.child', qp);
})
.then(() => {
assert.ok(this.routerService.isActive('parent.child', qp));
assert.ok(this.routerService.isActive('parent.child', qp)); // using same qp second time should not fail
assert.deepEqual(qp.queryParams, { sort: 'ascending' });
});
}
}
);