Skip to content

Commit

Permalink
Support negative integer values as request IDs
Browse files Browse the repository at this point in the history
Although a rare occurrence, the use of negative integer values as
request IDs is nonetheless permitted by the JSON-RPC 2.0 specification.
To ensure compatibility with as many JSON-RPC client implementations as
possible, we must support parsing negative IDs, even though we never
emit them ourselves.
  • Loading branch information
ebkalderon committed Jun 12, 2021
1 parent 982bf36 commit 77a871e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Types for sending data to and from the language client.

use std::fmt::{self, Debug, Display, Formatter};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;

use futures::channel::mpsc::Sender;
Expand All @@ -18,7 +18,7 @@ use super::{ServerState, State};

struct ClientInner {
sender: Sender<Outgoing>,
request_id: AtomicU64,
request_id: AtomicU32,
pending_requests: Arc<ClientRequests>,
state: Arc<ServerState>,
}
Expand All @@ -43,7 +43,7 @@ impl Client {
Client {
inner: Arc::new(ClientInner {
sender,
request_id: AtomicU64::new(0),
request_id: AtomicU32::new(0),
pending_requests,
state,
}),
Expand Down Expand Up @@ -356,7 +356,7 @@ impl Client {
let id = self.inner.request_id.fetch_add(1, Ordering::Relaxed);
let message = Outgoing::Request(ClientRequest::request::<R>(id, params));

let response_waiter = self.inner.pending_requests.wait(Id::Number(id));
let response_waiter = self.inner.pending_requests.wait(Id::Number(id as i64));

if self.inner.sender.clone().send(message).await.is_err() {
error!("failed to send request");
Expand Down
26 changes: 19 additions & 7 deletions src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ pub type Result<T> = std::result::Result<T, Error>;
#[serde(untagged)]
pub enum Id {
/// Numeric ID.
Number(u64),
Number(i64),
/// String ID.
String(String),
/// Null ID.
///
/// While the use of `null` as a request ID is permitted by the JSON-RPC 2.0 specification, it
/// is _strongly_ discouraged because the specification also uses a `null` value to indicate an
/// unknown ID in the `Response` object.
/// While `null` is considered a valid request ID by the JSON-RPC 2.0 specification, its use is
/// _strongly_ discouraged because the specification also uses a `null` value to indicate an
/// unknown ID in the [`Response`] object.
Null,
}

Expand All @@ -59,7 +59,7 @@ impl Display for Id {
impl From<NumberOrString> for Id {
fn from(num_or_str: NumberOrString) -> Self {
match num_or_str {
NumberOrString::Number(num) => Id::Number(num as u64),
NumberOrString::Number(num) => Id::Number(num as i64),
NumberOrString::String(s) => Id::String(s),
}
}
Expand Down Expand Up @@ -152,15 +152,15 @@ pub struct ClientRequest {

impl ClientRequest {
/// Constructs a JSON-RPC request from its corresponding LSP type.
pub(crate) fn request<R: Request>(id: u64, params: R::Params) -> Self {
pub(crate) fn request<R: Request>(id: u32, params: R::Params) -> Self {
// Since `R::Params` come from the `lsp-types` crate and validity is enforced via the
// `Request` trait, the `unwrap()` call below should never fail.
ClientRequest {
jsonrpc: Version,
method: R::METHOD.into(),
kind: ClientMethod::Request {
params: serde_json::to_value(params).unwrap(),
id: Id::Number(id),
id: Id::Number(id as i64),
},
}
}
Expand Down Expand Up @@ -340,4 +340,16 @@ mod tests {
let incoming = serde_json::from_value(missing_method_with_id).unwrap();
assert!(matches!(incoming, Incoming::Request(_)));
}

#[test]
fn accepts_null_request_id() {
let request_id: Id = serde_json::from_value(json!(null)).unwrap();
assert_eq!(request_id, Id::Null);
}

#[test]
fn accepts_negative_integer_request_id() {
let request_id: Id = serde_json::from_value(json!(-1)).unwrap();
assert_eq!(request_id, Id::Number(-1));
}
}

0 comments on commit 77a871e

Please sign in to comment.