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

Omit empty params in v2 request object #46

Merged
merged 17 commits into from
Aug 26, 2021

Conversation

chrisabruce
Copy link
Contributor

This pull request removes empty params from v2 request objects if they are empty.

Resolves #45

Copy link
Owner

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Thank you!

See inline comments.
Can you add a changelog entry as well please?

lib/src/lib.rs Outdated
@@ -166,7 +166,8 @@ pub struct Request {
pub id: Id,
pub jsonrpc: Version,
pub method: String,
pub params: Params,
#[serde(skip_serializing_if = "Option::is_none")]
Copy link
Owner

Choose a reason for hiding this comment

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

Can we get the same result if we slap a similar annotation onto the inner variants of Params?

That would allow us to not break the public API and release this as a patch.

If we have to use an Option, I would prefer if we:

  1. Initialize it to None
  2. Set it to Some on the first call to push
  3. Make serialize an immutable getter again

Copy link
Owner

Choose a reason for hiding this comment

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

Actually, we should only initialize Params to None for the v1 request. It is only for v2 where it can be omitted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we can't use serde skip_if on enums with variants. In order to not break the pub on the struct, I have implemented Serializer to detect if map is empty.

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 have also updated change log and bumped version (since no public changes).

Chris Bruce added 3 commits August 20, 2021 13:39

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Copy link
Owner

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Thank you!

The idea of implementing Serialize manually is good because it isn't actually very verbose and gives us a lot of flexibility!

Please see inline comments!

CHANGELOG.md Outdated
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.7.1] - 2021-08-20
Copy link
Owner

Choose a reason for hiding this comment

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

Setting the version and date happens automatically as part of the release automation, so this line needs to be removed again sorry. A bit too ambitious :)

Suggested change
## [0.7.1] - 2021-08-20

lib/Cargo.toml Outdated
version = "0.7.0"
authors = [ "Thomas Eizinger <thomas@eizinger.io>" ]
categories = [ "web-programming::http-client", "api-bindings", "network-programming" ]
authors = ["Thomas Eizinger <thomas@eizinger.io>"]
Copy link
Owner

Choose a reason for hiding this comment

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

What happened here? Can you please undo these formatting changes?

lib/src/lib.rs Outdated
where
S: Serializer,
{
let mut s = s.serialize_struct("request", 4)?;
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
let mut s = s.serialize_struct("request", 4)?;
let mut s = s.serialize_struct("Request", 4)?;

Copy link
Owner

Choose a reason for hiding this comment

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

Also, the 4 isn't always correct, is it? I am not sure what implications it has but I think we need to make this dynamic based on whether we will be serializing params or not.

Copy link
Owner

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Thank you! That looks good! :)

I am sorry about the CI test failure, I've fixed the test here: #48

Can you rebase onto latest master please?

@chrisabruce
Copy link
Contributor Author

@thomaseizinger think this is ready now?

@thomaseizinger
Copy link
Owner

@thomaseizinger think this is ready now?

Yes. It does seem that you merged instead of rebased so the diff includes my commits as well but that is not so much of a big deal :)

@thomaseizinger thomaseizinger merged commit 935d002 into thomaseizinger:master Aug 26, 2021
@chrisabruce chrisabruce deleted the cb/omit-empty-params-v2 branch October 2, 2021 11:31
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.

Empty Params should be Omitted in Request Object
2 participants