From 6e1c5d50e9e318275c5a75e642f9442baac4671c Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Fri, 22 Mar 2024 15:30:53 -0400 Subject: [PATCH 1/6] Fix for checkForFunctionProperty so that order does not matter Signed-off-by: Craig Perkins --- .../public/data_model/utils.test.js | 32 +++++++++++++++++++ .../vis_type_vega/public/data_model/utils.ts | 17 +++++----- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/plugins/vis_type_vega/public/data_model/utils.test.js b/src/plugins/vis_type_vega/public/data_model/utils.test.js index 9fe648c71139..c85954b8006b 100644 --- a/src/plugins/vis_type_vega/public/data_model/utils.test.js +++ b/src/plugins/vis_type_vega/public/data_model/utils.test.js @@ -91,4 +91,36 @@ describe('Utils.handleInvalidQuery', () => { }).toThrowError(); }); }); + + test('should identify object contains function properties', async () => { + const input1 = { + key1: 'value1', + key2: () => { + alert('Hello!'); + }, + }; + + expect(Utils.checkForFunctionProperty(input1)).toBe(true); + + const input2 = { + key2: () => { + alert('Hello!'); + }, + key1: 'value1', + }; + + expect(Utils.checkForFunctionProperty(input2)).toBe(true); + + const nestedInput = { + key1: { + nestedKey1: 'nestedValue1', + nestedKey2: () => { + alert('Hello!'); + }, + }, + key2: 'value1', + }; + + expect(Utils.checkForFunctionProperty(nestedInput)).toBe(true); + }); }); diff --git a/src/plugins/vis_type_vega/public/data_model/utils.ts b/src/plugins/vis_type_vega/public/data_model/utils.ts index 4e7a85062354..036d5cea693c 100644 --- a/src/plugins/vis_type_vega/public/data_model/utils.ts +++ b/src/plugins/vis_type_vega/public/data_model/utils.ts @@ -30,6 +30,7 @@ import compactStringify from 'json-stringify-pretty-compact'; import { validateObject } from '@osd/std'; +import { inspect } from 'util'; export class Utils { /** @@ -79,14 +80,14 @@ export class Utils { } static checkForFunctionProperty(object: object): boolean { - let result = false; - Object.values(object).forEach((value) => { - result = - typeof value === 'function' - ? true - : Utils.isObject(value) && Utils.checkForFunctionProperty(value); - }); - return result; + for (const value of Object.values(object)) { + if (typeof value === 'function') { + return true; + } else if (Utils.isObject(value) && Utils.checkForFunctionProperty(value)) { + return true; + } + } + return false; } static handleInvalidDate(date: unknown): number | string | Date | null { From c02a0a3d15f6fee1ad464b326be65a6e4fbe0964 Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Fri, 22 Mar 2024 15:33:48 -0400 Subject: [PATCH 2/6] Remove import for inspect Signed-off-by: Craig Perkins --- src/plugins/vis_type_vega/public/data_model/utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plugins/vis_type_vega/public/data_model/utils.ts b/src/plugins/vis_type_vega/public/data_model/utils.ts index 036d5cea693c..2aba9f9fa577 100644 --- a/src/plugins/vis_type_vega/public/data_model/utils.ts +++ b/src/plugins/vis_type_vega/public/data_model/utils.ts @@ -30,7 +30,6 @@ import compactStringify from 'json-stringify-pretty-compact'; import { validateObject } from '@osd/std'; -import { inspect } from 'util'; export class Utils { /** From d496b2d9614d92c69c044279791e74619dc2b076 Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Fri, 22 Mar 2024 15:40:45 -0400 Subject: [PATCH 3/6] Add to CHANGELOG Signed-off-by: Craig Perkins --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4849bfcf13b2..24533597f9dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [BUG][Multiple Datasource] Fix data source filter bug and add tests ([#6152](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6152)) - [BUG][Multiple Datasource] Fix obsolete snapshots for test within data source management plugin ([#6185](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6185)) - [Workspace] Add base path when parse url in http service ([#6233](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6233)) +- [BUG] ([#6248](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6248)) ### 🚞 Infrastructure From 506881cfa87733074ab43be8863a6d4caa0df668 Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Fri, 22 Mar 2024 17:35:50 -0400 Subject: [PATCH 4/6] replace with jest.fn() Signed-off-by: Craig Perkins --- .../public/data_model/utils.test.js | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/plugins/vis_type_vega/public/data_model/utils.test.js b/src/plugins/vis_type_vega/public/data_model/utils.test.js index c85954b8006b..1035e6890b95 100644 --- a/src/plugins/vis_type_vega/public/data_model/utils.test.js +++ b/src/plugins/vis_type_vega/public/data_model/utils.test.js @@ -59,9 +59,7 @@ describe('Utils.handleInvalidQuery', () => { test('should return null if input object has function as property', async () => { const input = { key1: 'value1', - key2: () => { - alert('Hello!'); - }, + key2: () => jest.fn(), }; expect(Utils.handleInvalidQuery(input)).toBe(null); @@ -71,9 +69,7 @@ describe('Utils.handleInvalidQuery', () => { const input = { key1: 'value1', key2: { - func: () => { - alert('Hello!'); - }, + func: () => jest.fn(), }, }; expect(Utils.handleInvalidQuery(input)).toBe(null); @@ -95,17 +91,13 @@ describe('Utils.handleInvalidQuery', () => { test('should identify object contains function properties', async () => { const input1 = { key1: 'value1', - key2: () => { - alert('Hello!'); - }, + key2: () => jest.fn(), }; expect(Utils.checkForFunctionProperty(input1)).toBe(true); const input2 = { - key2: () => { - alert('Hello!'); - }, + key2: () => jest.fn(), key1: 'value1', }; @@ -114,9 +106,7 @@ describe('Utils.handleInvalidQuery', () => { const nestedInput = { key1: { nestedKey1: 'nestedValue1', - nestedKey2: () => { - alert('Hello!'); - }, + nestedKey2: () => jest.fn(), }, key2: 'value1', }; From 65020700f54d621512989318235111495fc791e8 Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Fri, 22 Mar 2024 17:42:20 -0400 Subject: [PATCH 5/6] Update CHANGELOG Signed-off-by: Craig Perkins --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 791ddea44bb6..f47f67e87b02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,7 +82,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [BUG][Multiple Datasource] Fix data source filter bug and add tests ([#6152](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6152)) - [BUG][Multiple Datasource] Fix obsolete snapshots for test within data source management plugin ([#6185](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6185)) - [Workspace] Add base path when parse url in http service ([#6233](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6233)) -- [BUG] ([#6248](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6248)) +- [BUG] Fix for checkForFunctionProperty so that order does not matter ([#6248](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6248)) ### 🚞 Infrastructure From 79d7fcdf5062d4e2ff58f057879e665eda7043ea Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Sun, 24 Mar 2024 12:39:17 -0400 Subject: [PATCH 6/6] Add scenarios where object does not contain fn Signed-off-by: Craig Perkins --- .../public/data_model/utils.test.js | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/plugins/vis_type_vega/public/data_model/utils.test.js b/src/plugins/vis_type_vega/public/data_model/utils.test.js index 1035e6890b95..9d6d67d793bb 100644 --- a/src/plugins/vis_type_vega/public/data_model/utils.test.js +++ b/src/plugins/vis_type_vega/public/data_model/utils.test.js @@ -97,8 +97,8 @@ describe('Utils.handleInvalidQuery', () => { expect(Utils.checkForFunctionProperty(input1)).toBe(true); const input2 = { - key2: () => jest.fn(), - key1: 'value1', + key1: () => jest.fn(), + key2: 'value1', }; expect(Utils.checkForFunctionProperty(input2)).toBe(true); @@ -112,5 +112,22 @@ describe('Utils.handleInvalidQuery', () => { }; expect(Utils.checkForFunctionProperty(nestedInput)).toBe(true); + + const inputWithoutFn = { + key1: 'value1', + key2: 'value2', + }; + + expect(Utils.checkForFunctionProperty(inputWithoutFn)).toBe(false); + + const nestedInputWithoutFn = { + key1: { + nestedKey1: 'nestedValue1', + nestedKey2: 'nestedValue2', + }, + key2: 'value2', + }; + + expect(Utils.checkForFunctionProperty(nestedInputWithoutFn)).toBe(false); }); });