Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically omit "params" when it's empty #8

Closed
Makman2 opened this issue Dec 4, 2023 · 2 comments · Fixed by #9
Closed

Automatically omit "params" when it's empty #8

Makman2 opened this issue Dec 4, 2023 · 2 comments · Fixed by #9

Comments

@Makman2
Copy link
Contributor

Makman2 commented Dec 4, 2023

Hello 😄

I would propose to remove the "params" field in Request.serialize not only when it's None, but also when it's empty (i.e. when calling a method without any parameters).

Motivations:
  • Performance in the form of smaller messages

  • For my particular use-case I have a server that does not support empty objects/dicts specifically (we have a C++ implementation without any argument-name mapping - only positional arguments work). I am currently doing a workaround by monkey-patching the implementation of serialize with:

    original_serialize = Request.serialize
    def remove_empty_params(request: Request):
        data = json.loads(original_serialize(request))
        if 'params' in data and len(data['params']) == 0:
            del data['params']
        return json.dumps(data)
    Request.serialize = remove_empty_params

I hope that would be a worthwile addition. I would gladly submit a PR if this is deemed useful 😄

EDIT:

Forgot to mention the actual reference that omitting "params" is actually possible according to the JSON-RPC spec.

@emlove
Copy link
Owner

emlove commented Dec 4, 2023

Thanks for the proposal, yeah that makes sense to me! If you're willing to put together a PR I'd be happy to merge. Please make sure to include tests!

Makman2 added a commit to Makman2/jsonrpc-base that referenced this issue Dec 10, 2023
According to the JSON-RPC 2.0 Specification Section 4
(https://www.jsonrpc.org/specification#request_object)
the "params" key may be omitted.

Closes emlove#8
@Makman2
Copy link
Contributor Author

Makman2 commented Dec 10, 2023

@emlove submitted 😉

@emlove emlove closed this as completed in #9 Dec 11, 2023
emlove pushed a commit that referenced this issue Dec 11, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants