mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
@@ -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 = {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user