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] Prevent multiple qp serialization on activeTransition #19236

Merged
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
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 @@ -1935,7 +1935,7 @@ interface PartialRenderOptions {
model?: {};
}

function getFullQueryParams(router: EmberRouter, state: TransitionState<Route>) {
export function getFullQueryParams(router: EmberRouter, state: TransitionState<Route>) {
if (state['fullQueryParams']) {
return state['fullQueryParams'];
}
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 @@ -14,6 +14,7 @@ import { calculateCacheKey, extractRouteArgs, getActiveTargetName, resemblesURL
import DSL from './dsl';
import Route, {
defaultSerialize,
getFullQueryParams,
hasDefaultSerialize,
RenderOptions,
ROUTE_CONNECTIONS,
Expand All @@ -28,7 +29,6 @@ import { MatchCallback } from 'route-recognizer';
import Router, {
InternalRouteInfo,
logAbort,
QUERY_PARAMS_SYMBOL,
STATE_SYMBOL,
Transition,
TransitionError,
Expand Down Expand Up @@ -861,7 +861,7 @@ class EmberRouter extends EmberObject {

let unchangedQPs = {};
let qpUpdates = this._qpUpdates;
let params = this._routerMicrolib.activeTransition[QUERY_PARAMS_SYMBOL];
let params = getFullQueryParams(this, this._routerMicrolib.activeTransition[STATE_SYMBOL]);
for (let key in params) {
if (!qpUpdates.has(key)) {
unchangedQPs[key] = params[key];
Expand Down
49 changes: 49 additions & 0 deletions packages/ember/tests/routing/query_params_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,55 @@ moduleFor(
});
}

['@test Calling transitionTo does not serialize query params already serialized on the activeTransition'](
assert
) {
assert.expect(3);

this.router.map(function () {
this.route('parent', function () {
this.route('child');
this.route('sibling');
});
});

this.add(
'route:parent.child',
Route.extend({
afterModel() {
this.transitionTo('parent.sibling');
},
})
);

this.add(
'controller:parent',
Controller.extend({
queryParams: ['array', 'string'],
array: [],
string: '',
})
);

// `/parent/child?array=["one",2]&string=hello`
Comment on lines +140 to +170
Copy link
Contributor Author

@rreckonerr rreckonerr Oct 31, 2020

Choose a reason for hiding this comment

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

@chancancode @rwjblue

This test case fails with not really helpful error:

Promise rejected during " Calling transitionTo does not serialize multiple times query params already on the activeTransition": Assertion Failed: Cannot call `meta` on string
Error message image

Which comes from emberA trying to convert a string to array.

emberA(JSON.parse(value as string)`

_deserializeQueryParam(value: unknown, defaultType: string) {
if (value === null || value === undefined) {
return value;
} else if (defaultType === 'boolean') {
return value === 'true';
} else if (defaultType === 'number') {
return Number(value).valueOf();
} else if (defaultType === 'array') {
return emberA(JSON.parse(value as string));
}
return value;
}

But this error is not thrown in an ember application.

Wrapping emberA(JSON.parse(value as string) in a try-catch block results in a readable failing test assertion, like that the expected route qp's are different from the actual ones.

Should we do something to improve error reporting in _deserializeQueryParam? Like, check if JSON.parse(value) actually returns an array?

Copy link
Member

Choose a reason for hiding this comment

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

Should we do something to improve error reporting in _deserializeQueryParam? Like, check if JSON.parse(value) actually returns an array?

Yes, I think this seems like the best path.

return this.visit('/parent/child?array=%5B%22one%22%2C2%5D&string=hello').then(() => {
this.assertCurrentPath(
'/parent/sibling?array=%5B%22one%22%2C2%5D&string=hello',
'redirected to the sibling route, instead of child route'
);
assert.equal(
this.getController('parent').get('string'),
'hello',
'controller has value from the active transition'
);
assert.deepEqual(
this.getController('parent').get('array'),
['one', 2],
'controller has value from the active transition'
);
});
}

async ['@test Single query params can be set on the controller and reflected in the url'](
assert
) {
Expand Down