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

Make it possible to treat empty JSON response as no params #446

Merged
merged 23 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
115ae9b
treat empty json as no params when optional
niklasad1 Aug 27, 2021
74f5511
add test with nested array brackets
niklasad1 Aug 27, 2021
1708b72
address grumbles: better tests
niklasad1 Aug 30, 2021
5f1bab1
Update types/src/v2/params.rs
niklasad1 Aug 30, 2021
e1910ff
introduce RpcParams::sequence_ignore_empty
niklasad1 Aug 30, 2021
36fa65d
address grumbles: ignore empty json in RpcParams
niklasad1 Aug 30, 2021
8a849c8
Update proc-macros/src/render_server.rs
niklasad1 Aug 30, 2021
8078149
address grumbles: remove trim in next_inner()
niklasad1 Aug 30, 2021
138778f
address grumbles: trim_start after params.next()
niklasad1 Aug 31, 2021
c4409a0
address more grumbles
niklasad1 Aug 31, 2021
ceb2b10
[proc macros]: add test for empty array
niklasad1 Aug 31, 2021
92dadcf
make proc macro kind of work
niklasad1 Aug 31, 2021
4a66099
add hack to make it work in proc macros
niklasad1 Aug 31, 2021
97cf71c
add hack to make it work in proc macros
niklasad1 Aug 31, 2021
abb9709
Merge branch 'na-fix-441' of github.com:paritytech/jsonrpsee into na-…
niklasad1 Sep 1, 2021
e990edf
[] ->
niklasad1 Sep 1, 2021
e29e7f2
revert cerebral palsy
niklasad1 Sep 1, 2021
0f07423
Update proc-macros/tests/ui/correct/basic.rs
niklasad1 Sep 1, 2021
15f06af
Update proc-macros/tests/ui/correct/basic.rs
niklasad1 Sep 1, 2021
c6c5863
address grumbles
niklasad1 Sep 1, 2021
372feb1
Merge branch 'na-fix-441' of github.com:paritytech/jsonrpsee into na-…
niklasad1 Sep 1, 2021
0773e33
improve is_option
niklasad1 Sep 1, 2021
1effe8c
add test for core::option::Option
niklasad1 Sep 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion proc-macros/src/render_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ impl RpcDescription {
let decode_fields = params.iter().map(|(name, ty)| {
if is_option(ty) {
quote! {
let #name: #ty = seq.optional_next()?;
// treat empty array or map as no params.
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
let #name: #ty = seq.optional_next_ignore_empty_json()?;
}
} else {
quote! {
Expand Down
54 changes: 50 additions & 4 deletions types/src/v2/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,17 @@ impl<'a> RpcParams<'a> {
pub struct RpcParamsSequence<'a>(&'a str);

impl<'a> RpcParamsSequence<'a> {
fn next_inner<T>(&mut self) -> Option<Result<T, CallError>>
fn next_inner<T>(&mut self, treat_empty_json_as_no_params: bool) -> Option<Result<T, CallError>>
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
where
T: Deserialize<'a>,
{
let mut json = self.0.trim_start();
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved

if treat_empty_json_as_no_params && json == "[]" || json == "{}" {
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
self.0 = "";
return None;
}

match json.as_bytes().get(0)? {
b']' => {
self.0 = "";
Expand Down Expand Up @@ -201,7 +206,7 @@ impl<'a> RpcParamsSequence<'a> {
where
T: Deserialize<'a>,
{
match self.next_inner() {
match self.next_inner(false) {
Some(result) => result,
None => Err(CallError::InvalidParams),
}
Expand All @@ -221,15 +226,34 @@ impl<'a> RpcParamsSequence<'a> {
/// seq.optional_next().unwrap(),
/// seq.optional_next().unwrap(),
/// seq.optional_next().unwrap(),
/// ];;
/// ];
///
/// assert_eq!(params, [Some(1), Some(2), None, None]);
/// ```
pub fn optional_next<T>(&mut self) -> Result<Option<T>, CallError>
where
T: Deserialize<'a>,
{
match self.next_inner::<Option<T>>() {
match self.next_inner::<Option<T>>(false) {
Some(result) => result,
None => Ok(None),
}
}

/// Similar to `RpcParamsSequence::optional_next` but regards empty JSON Array and JSON Object
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
/// as no params supplied.
///
/// ```
/// # use jsonrpsee_types::v2::params::RpcParams;
/// let params = RpcParams::new(Some(r#"[]"#));
/// let mut seq = params.sequence();
/// assert_eq!(None::<u64>, seq.optional_next_ignore_empty_json().unwrap())
/// ```
pub fn optional_next_ignore_empty_json<T>(&mut self) -> Result<Option<T>, CallError>
where
T: Deserialize<'a>,
{
match self.next_inner::<Option<T>>(true) {
Some(result) => result,
None => Ok(None),
}
Expand Down Expand Up @@ -482,4 +506,26 @@ mod test {
assert_eq!(dsr.subscription, SubscriptionId::Str("9".into()));
assert_eq!(dsr.result, serde_json::json!("offside"));
}

#[test]
fn params_sequence_optional_ignore_empty() {
let params = RpcParams::new(Some(r#"["foo", "bar"]"#));
let mut seq = params.sequence();

assert_eq!(seq.optional_next_ignore_empty_json::<&str>().unwrap(), Some("foo"));
assert_eq!(seq.optional_next_ignore_empty_json::<&str>().unwrap(), Some("bar"));

let params = RpcParams::new(Some(r#"[]"#));
let mut seq = params.sequence();
assert_eq!(seq.optional_next_ignore_empty_json::<&str>().unwrap(), None);

let params = RpcParams::new(Some(r#"[12, "[]"]"#));
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
let mut seq = params.sequence();
assert_eq!(seq.optional_next_ignore_empty_json::<u64>().unwrap(), Some(12));
assert_eq!(seq.optional_next_ignore_empty_json::<&str>().unwrap(), Some("[]"));
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved

let params = RpcParams::new(Some(r#"{}"#));
let mut seq = params.sequence();
assert_eq!(seq.optional_next_ignore_empty_json::<&str>().unwrap(), None);
}
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
}