Skip to content

Commit

Permalink
[ML] conditional rison encoding for query params
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Jan 30, 2020
1 parent a8622cf commit 72b4507
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('useUrlState', () => {

expect(mockHistoryPush).toHaveBeenCalledWith({
search:
'_a=%28mlExplorerFilter%3A%28%29%2CmlExplorerSwimlane%3A%28viewByFieldName%3Aaction%29%2Cquery%3A%28%29%29&_g=%28ml%3A%28jobIds%3A%21%28dec-2%29%29%2CrefreshInterval%3A%28display%3AOff%2Cpause%3A%21f%2Cvalue%3A0%29%2Ctime%3A%28from%3A%272019-01-01T00%3A03%3A40.000Z%27%2Cmode%3Aabsolute%2Cto%3A%272019-08-30T11%3A55%3A07.000Z%27%29%29&savedSearchId=%27571aaf70-4c88-11e8-b3d7-01146121b73d%27',
'_a=%28mlExplorerFilter%3A%28%29%2CmlExplorerSwimlane%3A%28viewByFieldName%3Aaction%29%2Cquery%3A%28%29%29&_g=%28ml%3A%28jobIds%3A%21%28dec-2%29%29%2CrefreshInterval%3A%28display%3AOff%2Cpause%3A%21f%2Cvalue%3A0%29%2Ctime%3A%28from%3A%272019-01-01T00%3A03%3A40.000Z%27%2Cmode%3Aabsolute%2Cto%3A%272019-08-30T11%3A55%3A07.000Z%27%29%29&savedSearchId=571aaf70-4c88-11e8-b3d7-01146121b73d',
});
});
});
22 changes: 19 additions & 3 deletions x-pack/legacy/plugins/ml/public/application/util/url_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@ import { getNestedProperty } from './object_utils';
export type SetUrlState = (attribute: string | Dictionary<any>, value?: any) => void;
export type UrlState = [Dictionary<any>, SetUrlState];

const decodedParams = new Set(['_a', '_g']);
/**
* Set of URL query parameters that require the rison serialization.
*/
const risonSerializedParams = new Set(['_a', '_g']);

/**
* Checks if the URL query parameter requires rison serialization.
* @param queryParam
*/
function isRisonSerializationRequired(queryParam: string): boolean {
return risonSerializedParams.has(queryParam);
}

export function getUrlState(search: string): Dictionary<any> {
const urlState: Dictionary<any> = {};
const parsedQueryString = queryString.parse(search);

try {
Object.keys(parsedQueryString).forEach(a => {
if (decodedParams.has(a)) {
if (isRisonSerializationRequired(a)) {
urlState[a] = decode(parsedQueryString[a]) as Dictionary<any>;
} else {
urlState[a] = parsedQueryString[a];
Expand Down Expand Up @@ -75,7 +87,11 @@ export const useUrlState = (accessor: string): UrlState => {
const oldLocationSearch = queryString.stringify(parsedQueryString, { encode: false });

Object.keys(urlState).forEach(a => {
parsedQueryString[a] = encode(urlState[a]);
if (isRisonSerializationRequired(a)) {
parsedQueryString[a] = encode(urlState[a]);
} else {
parsedQueryString[a] = urlState[a];
}
});
const newLocationSearch = queryString.stringify(parsedQueryString, { encode: false });

Expand Down

0 comments on commit 72b4507

Please sign in to comment.