Skip to content

Commit

Permalink
[BUGFIX] Don't serialize default QPs on RouterService
Browse files Browse the repository at this point in the history
Fixes #19493
  • Loading branch information
wagenet committed Feb 15, 2022
1 parent 8455848 commit 05f2a12
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
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,
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
4 changes: 2 additions & 2 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 @@ -1073,7 +1073,7 @@ class EmberRouter<R extends Route = Route> extends EmberObject.extend(Evented) i
targetRouteName,
models,
queryParams,
Boolean(_keepDefaultQueryParamValues)
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'](
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

0 comments on commit 05f2a12

Please sign in to comment.