From 5235061ef9a3af8f89ae22a6a4ae9d3b5a3a61a8 Mon Sep 17 00:00:00 2001 From: Vladimir Rangelov Date: Thu, 18 Dec 2025 14:22:34 +0000 Subject: [PATCH] fix: Patch events handle when parameters in jsonrpc message are array --- .../src/shared/Gateway/Bidirectional.mjs | 10 +++++++++- .../src/shared/Transport/MockTransport.mjs | 12 +++++------- test_sdk/suite/properties.test.js | 15 +++++++++++++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/languages/javascript/src/shared/Gateway/Bidirectional.mjs b/languages/javascript/src/shared/Gateway/Bidirectional.mjs index e3e650b7..4f1e3848 100644 --- a/languages/javascript/src/shared/Gateway/Bidirectional.mjs +++ b/languages/javascript/src/shared/Gateway/Bidirectional.mjs @@ -39,7 +39,15 @@ function processMessage(json) { if (json.id !== undefined) { PlatformApi.request(json.id, json.method, json.params); } else { - PlatformApi.notify(json.method, json.params); + let params = json.params; + // TODO: Check if json.params is an array and if so, convert to a key-value object + // This is necessary because for Ripple, if params is an array this means that + // the callback function is expected to have one argument - the array itself. + // This is not compliant with the JSON-RPC specification, i.e. it should be removed. + if (Array.isArray(params)) { + params = { "value": json.params }; + } + PlatformApi.notify(json.method, params); } } else if (json.id !== undefined) { AppApi.response(json.id, json.result, json.error); diff --git a/languages/javascript/src/shared/Transport/MockTransport.mjs b/languages/javascript/src/shared/Transport/MockTransport.mjs index ae961ba4..a52645b1 100644 --- a/languages/javascript/src/shared/Transport/MockTransport.mjs +++ b/languages/javascript/src/shared/Transport/MockTransport.mjs @@ -101,16 +101,14 @@ function receive(_callback) { } } -function event(module, event, value) { +function event(module, event, data) { callback(JSON.stringify({ jsonrpc: '2.0', method: `${module}.${event}`, - params: [ - { - name: 'value', - value: value - } - ] + params: + { + value: data + } })) } diff --git a/test_sdk/suite/properties.test.js b/test_sdk/suite/properties.test.js index b2ba189b..8ecb3c0c 100644 --- a/test_sdk/suite/properties.test.js +++ b/test_sdk/suite/properties.test.js @@ -67,12 +67,23 @@ test('Basic Property get', () => { test('Basic Property subscribe', () => { let p = Simple.plainProperty(value => { - expect(value.value).toBe("value 123") + expect(value).toBe("value 123") + Simple.clear(); }) MockTransport.event("Simple","onPlainPropertyChanged", "value 123"); return p; }); +// This is to test the patch in Bidirectional gateway that handles event payloads that are arrays +test('Test event payload and array should be handled as a single argument array', () => { + let p = Simple.plainProperty(value => { + expect(Array.isArray(value)).toBe(true) + Simple.clear(); + }) + MockTransport.event("Simple","onPlainPropertyChanged", [1,2,3]); + return p; +}); + test('Basic Property set', () => { Simple.plainProperty({ foo: 'a new foo!' @@ -94,7 +105,7 @@ test('Basic Property set with null', () => { test('Basic Property subscribe to event', () => { Simple.clear("onPlainPropertyChanged"); let p = Simple.listen("onPlainPropertyChanged", value => { - expect(value.value).toBe( "value 123") + expect(value).toBe( "value 123") }) MockTransport.event("Simple","onPlainPropertyChanged", "value 123"); return p;