Skip to content

Commit 5235061

Browse files
fix: Patch events handle when parameters in jsonrpc message are array
1 parent 8fa37b2 commit 5235061

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

languages/javascript/src/shared/Gateway/Bidirectional.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ function processMessage(json) {
3939
if (json.id !== undefined) {
4040
PlatformApi.request(json.id, json.method, json.params);
4141
} else {
42-
PlatformApi.notify(json.method, json.params);
42+
let params = json.params;
43+
// TODO: Check if json.params is an array and if so, convert to a key-value object
44+
// This is necessary because for Ripple, if params is an array this means that
45+
// the callback function is expected to have one argument - the array itself.
46+
// This is not compliant with the JSON-RPC specification, i.e. it should be removed.
47+
if (Array.isArray(params)) {
48+
params = { "value": json.params };
49+
}
50+
PlatformApi.notify(json.method, params);
4351
}
4452
} else if (json.id !== undefined) {
4553
AppApi.response(json.id, json.result, json.error);

languages/javascript/src/shared/Transport/MockTransport.mjs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,14 @@ function receive(_callback) {
101101
}
102102
}
103103

104-
function event(module, event, value) {
104+
function event(module, event, data) {
105105
callback(JSON.stringify({
106106
jsonrpc: '2.0',
107107
method: `${module}.${event}`,
108-
params: [
109-
{
110-
name: 'value',
111-
value: value
112-
}
113-
]
108+
params:
109+
{
110+
value: data
111+
}
114112
}))
115113
}
116114

test_sdk/suite/properties.test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,23 @@ test('Basic Property get', () => {
6767

6868
test('Basic Property subscribe', () => {
6969
let p = Simple.plainProperty(value => {
70-
expect(value.value).toBe("value 123")
70+
expect(value).toBe("value 123")
71+
Simple.clear();
7172
})
7273
MockTransport.event("Simple","onPlainPropertyChanged", "value 123");
7374
return p;
7475
});
7576

77+
// This is to test the patch in Bidirectional gateway that handles event payloads that are arrays
78+
test('Test event payload and array should be handled as a single argument array', () => {
79+
let p = Simple.plainProperty(value => {
80+
expect(Array.isArray(value)).toBe(true)
81+
Simple.clear();
82+
})
83+
MockTransport.event("Simple","onPlainPropertyChanged", [1,2,3]);
84+
return p;
85+
});
86+
7687
test('Basic Property set', () => {
7788
Simple.plainProperty({
7889
foo: 'a new foo!'
@@ -94,7 +105,7 @@ test('Basic Property set with null', () => {
94105
test('Basic Property subscribe to event', () => {
95106
Simple.clear("onPlainPropertyChanged");
96107
let p = Simple.listen("onPlainPropertyChanged", value => {
97-
expect(value.value).toBe( "value 123")
108+
expect(value).toBe( "value 123")
98109
})
99110
MockTransport.event("Simple","onPlainPropertyChanged", "value 123");
100111
return p;

0 commit comments

Comments
 (0)