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
This commit is contained in:
Sven Grossmann
2023-07-10 09:49:56 +02:00
committed by GitHub
parent 607670a9fa
commit b05cc2d651
2 changed files with 50 additions and 0 deletions

View File

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

View File

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