From 591b9baaa3097064e355103ead37729610d76f81 Mon Sep 17 00:00:00 2001 From: Vadim Ogievetsky Date: Wed, 28 Feb 2024 14:37:24 -0800 Subject: [PATCH] fix crash when parsing data in data loader that can not be parsed (#15983) --- web-console/src/utils/object-change.spec.ts | 21 ++++++++++++++++++++- web-console/src/utils/object-change.ts | 5 +++-- web-console/src/utils/sampler.ts | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/web-console/src/utils/object-change.spec.ts b/web-console/src/utils/object-change.spec.ts index bdfecca7ec1e..547d88159268 100644 --- a/web-console/src/utils/object-change.spec.ts +++ b/web-console/src/utils/object-change.spec.ts @@ -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', () => { @@ -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: { diff --git a/web-console/src/utils/object-change.ts b/web-console/src/utils/object-change.ts index 9b29cda5dbf4..9eeb8a4b6e99 100644 --- a/web-console/src/utils/object-change.ts +++ b/web-console/src/utils/object-change.ts @@ -160,9 +160,10 @@ export function deepExtend>(target: T, diff: Recor } export function allowKeys(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]; } } diff --git a/web-console/src/utils/sampler.ts b/web-console/src/utils/sampler.ts index 866b195d4573..6d274b80d0a5 100644 --- a/web-console/src/utils/sampler.ts +++ b/web-console/src/utils/sampler.ts @@ -133,7 +133,7 @@ export interface SampleEntry { export function getCacheRowsFromSampleResponse(sampleResponse: SampleResponse): CacheRows { return filterMap(sampleResponse.data, d => ({ ...d.input, - ...allowKeys(d.parsed, ALL_POSSIBLE_SYSTEM_FIELDS), + ...allowKeys(d.parsed || {}, ALL_POSSIBLE_SYSTEM_FIELDS), })).slice(0, 20); }