Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 72aba6e

Browse files
authored
Merge pull request #659 from Xanewok/fix-initialized-msg-type
Fix Initialized message parsing
2 parents 912d5d4 + 6615c91 commit 72aba6e

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cargo = { git = "https://github.com/rust-lang/cargo" }
1414
env_logger = "0.4"
1515
failure = "0.1.1"
1616
jsonrpc-core = "8.0.1"
17-
languageserver-types = "0.17"
17+
languageserver-types = { git = "https://github.com/Xanewok/languageserver-types", branch = "backport-0.17-fix-params-types" }
1818
lazy_static = "0.2"
1919
log = "0.3"
2020
racer = "2.0.12"

src/server/mod.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use jsonrpc_core::{self as jsonrpc, Id};
1717
use vfs::Vfs;
1818
use serde;
1919
use serde::ser::{Serialize, Serializer, SerializeStruct};
20+
use serde::Deserialize;
2021
use serde_json;
2122

2223
use version;
@@ -517,10 +518,10 @@ struct RawMessage {
517518
}
518519

519520
impl RawMessage {
520-
fn parse_as_request<'de, R, P>(&'de self) -> Result<Request<R>, jsonrpc::Error>
521+
fn parse_as_request<'de, R>(&'de self) -> Result<Request<R>, jsonrpc::Error>
521522
where
522-
R: LSPRequest<Params = P>,
523-
P: serde::Deserialize<'de>,
523+
R: LSPRequest,
524+
<R as LSPRequest>::Params: serde::Deserialize<'de>,
524525
{
525526
// FIXME: We only support numeric responses, ideally we should switch from using parsed usize
526527
// to using jsonrpc_core::Id
@@ -530,9 +531,9 @@ impl RawMessage {
530531
_ => None,
531532
};
532533

533-
let params = P::deserialize(&self.params).map_err(|e| {
534+
let params = R::Params::deserialize(&self.params).map_err(|e| {
534535
debug!("error when parsing as request: {}", e);
535-
jsonrpc::Error::invalid_request()
536+
jsonrpc::Error::invalid_params(format!("{}", e))
536537
})?;
537538

538539
match parsed_numeric_id {
@@ -546,14 +547,14 @@ impl RawMessage {
546547
}
547548
}
548549

549-
fn parse_as_notification<'de, T, P>(&'de self) -> Result<Notification<T>, jsonrpc::Error>
550+
fn parse_as_notification<'de, T>(&'de self) -> Result<Notification<T>, jsonrpc::Error>
550551
where
551-
T: LSPNotification<Params = P>,
552-
P: serde::Deserialize<'de>,
552+
T: LSPNotification,
553+
<T as LSPNotification>::Params: serde::Deserialize<'de>,
553554
{
554555
let params = T::Params::deserialize(&self.params).map_err(|e| {
555556
debug!("error when parsing as notification: {}", e);
556-
jsonrpc::Error::invalid_request()
557+
jsonrpc::Error::invalid_params(format!("{}", e))
557558
})?;
558559

559560
Ok(Notification {
@@ -680,12 +681,12 @@ mod test {
680681
let raw = RawMessage {
681682
method: "initialize".to_owned(),
682683
id: None,
683-
params: serde_json::Value::Null,
684+
params: serde_json::Value::Object(serde_json::Map::new()),
684685
};
685686
let notification: Notification<notifications::Initialized> =
686687
raw.parse_as_notification().unwrap();
687688

688-
let expected = Notification::<notifications::Initialized>::new(());
689+
let expected = Notification::<notifications::Initialized>::new(InitializedParams {});
689690

690691
assert_eq!(notification.params, expected.params);
691692
assert_eq!(notification._action, expected._action);
@@ -752,4 +753,11 @@ mod test {
752753

753754
assert_eq!(*deser.get("params").unwrap(), json!({}));
754755
}
756+
757+
#[test]
758+
fn deserialize_message_empty_params() {
759+
let msg = r#"{"jsonrpc":"2.0","method":"initialized","params":{}}"#;
760+
let parsed = RawMessage::try_parse(msg).unwrap().unwrap();
761+
parsed.parse_as_notification::<notifications::Initialized>().unwrap();
762+
}
755763
}

0 commit comments

Comments
 (0)