fix crash when parsing data in data loader that can not be parsed (#15983)

This commit is contained in:
Vadim Ogievetsky 2024-02-28 14:37:24 -08:00 committed by GitHub
parent f757231420
commit 6e222d47c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 4 deletions

View File

@ -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: {

View File

@ -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];
}
}

View File

@ -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);
}