From c08815c98ebaa757503f56e053e699b038ea948a Mon Sep 17 00:00:00 2001 From: Makman2 Date: Mon, 11 Dec 2023 00:51:52 +0100 Subject: [PATCH] Omit empty "params" in requests According to the JSON-RPC 2.0 Specification Section 4 (https://www.jsonrpc.org/specification#request_object) the "params" key may be omitted. Closes #8 --- jsonrpc_base/jsonrpc.py | 2 +- tests.py | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/jsonrpc_base/jsonrpc.py b/jsonrpc_base/jsonrpc.py index 0c8ddca..9600f1e 100644 --- a/jsonrpc_base/jsonrpc.py +++ b/jsonrpc_base/jsonrpc.py @@ -193,7 +193,7 @@ def response_id(self): def serialize(self): """Generate the raw JSON message to be sent to the server""" data = {'jsonrpc': '2.0', 'method': self.method} - if self.params is not None: + if self.params: data['params'] = self.params if self.msg_id is not None: data['id'] = self.msg_id diff --git a/tests.py b/tests.py index 90a0d2e..a636dc6 100644 --- a/tests.py +++ b/tests.py @@ -62,11 +62,16 @@ def test_dumps(server): ) # test empty args dict assertSameJSON( - '''{"params": {}, "jsonrpc": "2.0", "method": "my_method_name", "id": - 1}''', + '''{"jsonrpc": "2.0", "method": "my_method_name", "id": 1}''', jsonrpc_base.Request( 'my_method_name', params={}, msg_id=1).serialize() ) + # test empty args array + assertSameJSON( + '''{"jsonrpc": "2.0", "method": "my_method_name", "id": 1}''', + jsonrpc_base.Request( + 'my_method_name', params=[], msg_id=1).serialize() + ) # test keyword args assertSameJSON( '''{"params": {"foo": "bar"}, "jsonrpc": "2.0", "method":