Skip to content

Commit

Permalink
fix crash when parsing data in data loader that can not be parsed (#1…
Browse files Browse the repository at this point in the history
…5983) (#16059)

Co-authored-by: Vadim Ogievetsky <vadim@ogievetsky.com>
  • Loading branch information
cryptoe and vogievetsky authored Mar 8, 2024
1 parent b1aff95 commit daf2c48
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
21 changes: 20 additions & 1 deletion web-console/src/utils/object-change.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@

import * as JSONBig from 'json-bigint-native';

import { deepDelete, deepExtend, deepGet, deepSet, makePath, parsePath } from './object-change';
import {
allowKeys,
deepDelete,
deepExtend,
deepGet,
deepSet,
makePath,
parsePath,
} from './object-change';

describe('object-change', () => {
describe('parsePath', () => {
Expand All @@ -36,6 +44,17 @@ describe('object-change', () => {
});
});

describe('allowKeys', () => {
it('works with bad objects', () => {
expect(allowKeys(null, ['a', 'b', 'c'] as any)).toEqual(null);
expect(allowKeys(undefined as any, ['a', 'b', 'c'] as any)).toEqual(undefined);
});

it('works in a normal case', () => {
expect(allowKeys({ a: 1, z: 4 }, ['a', 'b', 'c'] as any)).toEqual({ a: 1 });
});
});

describe('deepGet', () => {
const thing = {
hello: {
Expand Down
5 changes: 3 additions & 2 deletions web-console/src/utils/object-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ export function deepExtend<T extends Record<string, any>>(target: T, diff: Recor
}

export function allowKeys<T>(obj: T, keys: (keyof T)[]): T {
const newObj: T = {} as any;
if (!obj || typeof obj !== 'object') return obj;
const newObj = {} as T;
for (const key of keys) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (Object.hasOwn(obj, key)) {
newObj[key] = obj[key];
}
}
Expand Down
2 changes: 1 addition & 1 deletion web-console/src/utils/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export interface SampleEntry {
export function getCacheRowsFromSampleResponse(sampleResponse: SampleResponse): CacheRows {
return filterMap(sampleResponse.data, d => ({
...d.input,
...allowKeys<any>(d.parsed, ALL_POSSIBLE_SYSTEM_FIELDS),
...allowKeys<any>(d.parsed || {}, ALL_POSSIBLE_SYSTEM_FIELDS),
})).slice(0, 20);
}

Expand Down

0 comments on commit daf2c48

Please sign in to comment.