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 release] Don't serialize default QPs on RouterService #19971

Merged
merged 1 commit into from
Feb 18, 2022
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
27 changes: 18 additions & 9 deletions packages/@ember/-internals/routing/lib/services/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ class RouterService<R extends Route> extends Service.extend(Evented) {
let { routeName, models, queryParams } = extractRouteArgs(args);

let transition = this._router._doTransition(routeName, models, queryParams, true);
transition['_keepDefaultQueryParamValues'] = true;

return transition;
}
Expand Down Expand Up @@ -324,21 +323,31 @@ class RouterService<R extends Route> extends Service.extend(Evented) {
let hasQueryParams = Object.keys(queryParams).length > 0;

if (hasQueryParams) {
// UNSAFE: casting `routeName as string` here encodes the existing
// assumption but may be wrong: `extractRouteArgs` correctly returns it
// as `string | undefined`. There may be bugs if `_prepareQueryParams`
// does not correctly account for `undefined` values for `routeName`.
// Spoilers: under the hood this currently uses router.js APIs which
// *do not* account for this being `undefined`.
let targetRouteName = routeName as string;

queryParams = Object.assign({}, queryParams);
this._router._prepareQueryParams(
// UNSAFE: casting `routeName as string` here encodes the existing
// assumption but may be wrong: `extractRouteArgs` correctly returns it
// as `string | undefined`. There may be bugs if `_prepareQueryParams`
// does not correctly account for `undefined` values for `routeName`.
// Spoilers: under the hood this currently uses router.js APIs which
// *do not* account for this being `undefined`.
routeName as string,
targetRouteName,
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens when targetRouteName here is undefined?

should we remove the as and instead narrow the type with an assert?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don’t know. It was like this before and I just moved things around. I do want to review all as casts someone soon.

models,
queryParams,
true /* fromRouterService */
);

return shallowEqual(queryParams, routerMicrolib.state!.queryParams);
let currentQueryParams = Object.assign({}, routerMicrolib.state!.queryParams);
this._router._prepareQueryParams(
targetRouteName,
models,
currentQueryParams,
true /* fromRouterService */
);

return shallowEqual(queryParams, currentQueryParams);
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/-internals/routing/lib/system/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2522,7 +2522,7 @@ Route.reopen({
qp.serializedValue = svalue;

let thisQueryParamHasDefaultValue = qp.serializedDefaultValue === svalue;
if (!thisQueryParamHasDefaultValue || (transition as any)._keepDefaultQueryParamValues) {
if (!thisQueryParamHasDefaultValue) {
finalParams.push({
value: svalue,
visible: true,
Expand Down
9 changes: 2 additions & 7 deletions packages/@ember/-internals/routing/lib/system/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ class EmberRouter<R extends Route = Route> extends EmberObject.extend(Evented) i
_targetRouteName: string | undefined,
models: ModelFor<R>[],
_queryParams: Record<string, unknown>,
_keepDefaultQueryParamValues?: boolean
_fromRouterService?: boolean
): Transition {
let targetRouteName = _targetRouteName || getActiveTargetName(this._routerMicrolib);
assert(
Expand All @@ -1069,12 +1069,7 @@ class EmberRouter<R extends Route = Route> extends EmberObject.extend(Evented) i

Object.assign(queryParams, _queryParams);

this._prepareQueryParams(
targetRouteName,
models,
queryParams,
Boolean(_keepDefaultQueryParamValues)
);
this._prepareQueryParams(targetRouteName, models, queryParams, Boolean(_fromRouterService));

let transition = this._routerMicrolib.transitionTo(targetRouteName, ...models, { queryParams });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ moduleFor(
return this.routerService.transitionTo('parent.child', queryParams);
})
.then(() => {
assert.ok(this.routerService.isActive('parent.child', queryParams));
assert.ok(this.routerService.isActive('parent.child', queryParams), 'route is active');
assert.notOk(
this.routerService.isActive('parent.child', this.buildQueryParams({ sort: 'DESC' }))
this.routerService.isActive('parent.child', this.buildQueryParams({ sort: 'DESC' })),
'route with QPs is not active'
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ moduleFor(
});
}

['@test RouterService#replaceWith with basic query params does not remove query param defaults'](
['@test RouterService#replaceWith with basic query params removes query param defaults'](
chriskrycho marked this conversation as resolved.
Show resolved Hide resolved
assert
) {
assert.expect(1);
Expand All @@ -133,7 +133,7 @@ moduleFor(
return this.routerService.replaceWith('parent.child', queryParams);
})
.then(() => {
assert.deepEqual(this.state, ['/', '/child?sort=ASC']);
assert.deepEqual(this.state, ['/', '/child']);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ moduleFor(
this.assertText('much dynamicism');
}

['@test RouterService#transitionTo with basic query params does not remove query param defaults'](
['@test RouterService#transitionTo with basic query params removes query param defaults'](
assert
) {
assert.expect(1);
Expand All @@ -258,7 +258,7 @@ moduleFor(
return this.routerService.transitionTo('parent.child', queryParams);
})
.then(() => {
assert.equal(this.routerService.get('currentURL'), '/child?sort=ASC');
assert.equal(this.routerService.get('currentURL'), '/child');
});
}

Expand Down Expand Up @@ -302,14 +302,14 @@ moduleFor(
})
);

let queryParams = this.buildQueryParams({ sort: 'ASC' });
let queryParams = this.buildQueryParams({ sort: 'DESC' });

return this.visit('/')
.then(() => {
return this.routerService.transitionTo('parent.child', queryParams);
})
.then(() => {
assert.equal(this.routerService.get('currentURL'), '/child?sort=ASC');
assert.equal(this.routerService.get('currentURL'), '/child?sort=DESC');
});
}

Expand All @@ -328,14 +328,14 @@ moduleFor(
})
);

let queryParams = this.buildQueryParams({ url_sort: 'ASC' });
let queryParams = this.buildQueryParams({ url_sort: 'DESC' });

return this.visit('/')
.then(() => {
return this.routerService.transitionTo('parent.child', queryParams);
})
.then(() => {
assert.equal(this.routerService.get('currentURL'), '/child?url_sort=ASC');
assert.equal(this.routerService.get('currentURL'), '/child?url_sort=DESC');
});
}

Expand Down