sourcegraph / jsonrpc2

Package jsonrpc2 provides a client and server implementation of JSON-RPC 2.0 (http://www.jsonrpc.org/specification)
MIT License
190 stars 62 forks source link

Add ability to omit params member from request #61

Closed samherrmann closed 1 year ago

samherrmann commented 1 year ago

The JSON-RPC 2.0 specification allows the params member of a request to be omitted 1. Before this commit, this library did not allow the params member to be omitted. When the params argument of the Conn.Call or Conn.Notify method was set to nil, then Request.Params was set to the JSON encoding of nil which is null.

This commit adds a ConnOption named OmitNilParams. If OmitNilParams is applied on Conn and Conn.Call or Conn.Notify are invoked with their params argument set to nil, then the params member in the JSON encoding of Request is omitted. If the OmitNilParams option is not applied on Conn then the previous behavior is maintained. In other words, the changes in this commit are backwards compatible.

References

samherrmann commented 1 year ago

I found it interesting that the struct tag of Request.Params actually includes the omitEmpty option. That means that it was possible to receive a request in a handler with Request.Params set to nil, but it was not possible to send a request (using Conn.Call or Conn.Notify) with Request.Params set to nil.

samherrmann commented 1 year ago

I'm actually not sure anymore if the implementation in this pull request is the best option. I have also made Draft PR #62 as an alternate implementation. The difference between the two approaches is that this PR implements OmitNilParams as a ConnOpt while #62 implements OmitNilParams as a CallOption. This difference means that OmitNilParams is used by Request.SetParams instead of Conn.DispatchCall and Conn.Notify. Personally I prefer the look of #62 over this PR.

samherrmann commented 1 year ago

Closing in favor of #62. I'm thinking that we can always resurrect this PR if needed.