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

skip params when it is None #660

Open
hxzhao527 opened this issue Feb 6, 2022 · 0 comments
Open

skip params when it is None #660

hxzhao527 opened this issue Feb 6, 2022 · 0 comments

Comments

@hxzhao527
Copy link

hxzhao527 commented Feb 6, 2022

when I use this lib to make a call to aria2c system.listMethods, I find the params serialized to null even when the method defined without args.

use jsonrpc_core_client::transports::http;
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;

/// Rpc trait
#[rpc]
pub trait Rpc {
	/// Returns a protocol version
	#[rpc(name = "system.listMethods")]
	fn list_methods(&self) -> Result<Vec<String>>;
}

#[tokio::main]
async fn main() -> Result<()>{
	let client = http::connect::<gen_client::Client>("http://127.0.0.1:6800/jsonrpc").await.unwrap();
	println!("{:?}", client.list_methods().await.unwrap()); 

	Ok(())
}

the code will send {"jsonrpc":"2.0","method":"system.listMethods","params":null,"id":0} to aria2 and then got HTTPError: HTTP Error 500: Internal Server Error

if skip the params when it is None, things go right.

/// Represents jsonrpc request which is a method call.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct MethodCall {
	/// A String specifying the version of the JSON-RPC protocol.
	pub jsonrpc: Option<Version>,
	/// A String containing the name of the method to be invoked.
	pub method: String,
	/// A Structured value that holds the parameter values to be used
	/// during the invocation of the method. This member MAY be omitted.
	#[serde(default = "default_params")]
	#[serde(skip_serializing_if="omit_params")]
	pub params: Params,
	/// An identifier established by the Client that MUST contain a String,
	/// Number, or NULL value if included. If it is not included it is assumed
	/// to be a notification.
	pub id: Id,
}

fn omit_params(param: &Params) -> bool {
	&Params::None == param
}

so could we add this annotation to skip serializing when it is None?

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

No branches or pull requests

1 participant