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

Always omit params member from request when empty #67

Merged
merged 3 commits into from
Feb 22, 2023

Conversation

samherrmann
Copy link
Contributor

With this commit, the JSON encoding of Request always omits the params member when calling Conn.Call, Conn.DispatchCall, or Conn.Notify with the params argument set to nil. This change also removes the OmitNilParams call option that was added in commit 8012d49 (#62).

As of this commit, if users desire to send a JSON-RPC request with a params value of null, then they may do so by explicitly setting the params argument of Conn.Call/Conn.DispatchCall/Conn.Notify to json.RawMessage("null").

// don't want this expected error to appear in os.Stderr though when
// running tests in verbose mode or when other tests fail.
jsonrpc2.SetLogger(log.New(io.Discard, "", 0)),
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't actually have anything to do with the rest of this PR. I slipped this in because I kept seeing jsonrpc2: protocol error: invalid character 'i' looking for beginning of value in the test logs while working on tests. I introduced this test in 028a50b (#64) and somehow missed that this test was spamming the test logs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am only noticing now that this line is commented out. Wow, that's sloppy of me. Will fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f74a03b.

}
// Assert that conn is closed by trying to write to it.
_, got := conn.Write(nil)
want := io.ErrClosedPipe
if got != want {
t.Fatalf("got %q, want %q", got, want)
t.Errorf("got %s, want %s", got, want)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched from t.Fatalf to t.Errorf because this function is being reused in multiple tests. A reusable function such as this one cannot know if it's being used in the main goroutine or in a sub-goroutine. It's invalid to call t.Fatal* in a goroutine other than the main goroutine. Using t.Fatalf in this function hasn't caused any issues (so far), but I figured I would make this change for better hygiene. 🙂

@@ -32,7 +32,7 @@ type Error struct {
Data *json.RawMessage `json:"data,omitempty"`
}

// SetError sets e.Data to the JSON representation of v. If JSON
// SetError sets e.Data to the JSON encoding of v. If JSON
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use consistent terminology. I personally prefer "encoding" over "representation" solely because it's shorter, but "encoding" also aligns with the terminology used by json.Marshal in the standard library.

With this commit, the JSON encoding of `Request` always omits the params
member when calling `Conn.Call`, `Conn.DispatchCall`, or `Conn.Notify`
with the `params` argument set to `nil`. This change also removes the
`OmitNilParams` call option that was added in commit 8012d49 (sourcegraph#62).

As of this commit, if users desire to send a JSON-RPC request with a
`params` value of `null`, then they may do so by explicitly setting the
`params` argument of `Conn.Call`/`Conn.DispatchCall`/`Conn.Notify` to
`json.RawMessage("null")`.
@samherrmann
Copy link
Contributor Author

I definitely feel like making this change was a good move considering how simple it is for users to send an explicit null value if they so desire:

conn.Call(ctx, method, json.RawMessage("null"), &result)

@samherrmann samherrmann marked this pull request as ready for review February 12, 2023 19:57
@keegancsmith keegancsmith self-requested a review February 21, 2023 12:54
Copy link
Member

@keegancsmith keegancsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!

With this commit, the JSON encoding of Request always omits the params member when calling Conn.Call, Conn.DispatchCall, or Conn.Notify with the params argument set to nil. This change also removes the OmitNilParams call option that was added in commit 8012d49 (#62).

As of this commit, if users desire to send a JSON-RPC request with a params value of null, then they may do so by explicitly setting the params argument of Conn.Call/Conn.DispatchCall/Conn.Notify to json.RawMessage("null").

Can you add either documentation or an example test which demonstrates this?

The line of code changed in this commit was commented out by accident.
@samherrmann
Copy link
Contributor Author

Once this lands, can we have a 0.2.0 release? That would ensure that the example tests are available when someone visits https://pkg.go.dev/github.com/sourcegraph/jsonrpc2.

@keegancsmith keegancsmith merged commit ae88a5e into sourcegraph:master Feb 22, 2023
@samherrmann samherrmann deleted the omit-empty-params branch February 22, 2023 20:55
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 this pull request may close these issues.

None yet

2 participants