From b05cc2d65145647978a20c89d11d7e3be86cc400 Mon Sep 17 00:00:00 2001 From: Sven Grossmann Date: Mon, 10 Jul 2023 09:49:56 +0200 Subject: [PATCH] Transformations: Fix `extractFields` throwing Error if one value is undefined or null (#71070) * fix extract fields with null values * rename test * check for null values * revert changes * improve position --- .../extractFields/extractFields.test.ts | 46 +++++++++++++++++++ .../extractFields/extractFields.ts | 4 ++ 2 files changed, 50 insertions(+) diff --git a/public/app/features/transformers/extractFields/extractFields.test.ts b/public/app/features/transformers/extractFields/extractFields.test.ts index 63c6c2bd72c..1895e57bead 100644 --- a/public/app/features/transformers/extractFields/extractFields.test.ts +++ b/public/app/features/transformers/extractFields/extractFields.test.ts @@ -187,6 +187,52 @@ describe('Fields from JSON', () => { } `); }); + + it('skips null values', async () => { + const cfg: ExtractFieldsOptions = { + source: 'line', + replace: false, + }; + const ctx = { interpolate: (v: string) => v }; + + const testDataFrame: DataFrame = { + fields: [ + { config: {}, name: 'Time', type: FieldType.time, values: [1, 2] }, + { config: {}, name: 'line', type: FieldType.other, values: ['{"foo":"bar"}', null] }, + ], + length: 2, + }; + + const frames = extractFieldsTransformer.transformer(cfg, ctx)([testDataFrame]); + expect(frames.length).toEqual(1); + expect(frames[0]).toEqual({ + fields: [ + { + config: {}, + name: 'Time', + type: 'time', + values: [1, 2], + state: { + displayName: 'Time', + multipleFrames: false, + }, + }, + { + config: {}, + name: 'line', + type: 'other', + values: ['{"foo":"bar"}', null], + }, + { + name: 'foo', + values: ['bar', undefined], + type: 'string', + config: {}, + }, + ], + length: 2, + }); + }); }); const testFieldTime: Field = { diff --git a/public/app/features/transformers/extractFields/extractFields.ts b/public/app/features/transformers/extractFields/extractFields.ts index 9db5202153b..ac9d163a9cb 100644 --- a/public/app/features/transformers/extractFields/extractFields.ts +++ b/public/app/features/transformers/extractFields/extractFields.ts @@ -62,6 +62,10 @@ function addExtractedFields(frame: DataFrame, options: ExtractFieldsOptions): Da } } + if (obj == null) { + continue; + } + if (options.format === FieldExtractorID.JSON && options.jsonPaths && options.jsonPaths?.length > 0) { const newObj: { [k: string]: unknown } = {}; // filter out empty paths