Skip to content

Commit

Permalink
Omit empty "params" in requests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Makman2 committed Dec 10, 2023
1 parent 0de6d21 commit c08815c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion jsonrpc_base/jsonrpc.py
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions tests.py
Expand Up @@ -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":
Expand Down

0 comments on commit c08815c

Please sign in to comment.