lsp: Do not set error/result fields if they're missing. (#10610)
We were previously not conforming to LSP spec, as we were setting **both** result field and error field on response fields, which could confuse servers. Excerpt from the spec: > * The result of a request. This member is REQUIRED on success. > * This member MUST NOT exist if there was an error invoking the method. Fixes #10595 Release Notes: - N/A
This commit is contained in:
parent
263023021d
commit
e34c443331
1 changed files with 36 additions and 7 deletions
|
@ -138,8 +138,16 @@ struct AnyResponse<'a> {
|
||||||
struct Response<T> {
|
struct Response<T> {
|
||||||
jsonrpc: &'static str,
|
jsonrpc: &'static str,
|
||||||
id: RequestId,
|
id: RequestId,
|
||||||
result: Option<T>,
|
#[serde(flatten)]
|
||||||
error: Option<Error>,
|
value: LspResult<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
enum LspResult<T> {
|
||||||
|
#[serde(rename = "result")]
|
||||||
|
Ok(Option<T>),
|
||||||
|
Error(Option<Error>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Language server protocol RPC notification message.
|
/// Language server protocol RPC notification message.
|
||||||
|
@ -867,16 +875,14 @@ impl LanguageServer {
|
||||||
Ok(result) => Response {
|
Ok(result) => Response {
|
||||||
jsonrpc: JSON_RPC_VERSION,
|
jsonrpc: JSON_RPC_VERSION,
|
||||||
id,
|
id,
|
||||||
result: Some(result),
|
value: LspResult::Ok(Some(result)),
|
||||||
error: None,
|
|
||||||
},
|
},
|
||||||
Err(error) => Response {
|
Err(error) => Response {
|
||||||
jsonrpc: JSON_RPC_VERSION,
|
jsonrpc: JSON_RPC_VERSION,
|
||||||
id,
|
id,
|
||||||
result: None,
|
value: LspResult::Error(Some(Error {
|
||||||
error: Some(Error {
|
|
||||||
message: error.to_string(),
|
message: error.to_string(),
|
||||||
}),
|
})),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
if let Some(response) =
|
if let Some(response) =
|
||||||
|
@ -1503,4 +1509,27 @@ mod tests {
|
||||||
let expected_id = RequestId::Int(2);
|
let expected_id = RequestId::Int(2);
|
||||||
assert_eq!(notification.id, Some(expected_id));
|
assert_eq!(notification.id, Some(expected_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_serialize_has_no_nulls() {
|
||||||
|
// Ensure we're not setting both result and error variants. (ticket #10595)
|
||||||
|
let no_tag = Response::<u32> {
|
||||||
|
jsonrpc: "",
|
||||||
|
id: RequestId::Int(0),
|
||||||
|
value: LspResult::Ok(None),
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_string(&no_tag).unwrap(),
|
||||||
|
"{\"jsonrpc\":\"\",\"id\":0,\"result\":null}"
|
||||||
|
);
|
||||||
|
let no_tag = Response::<u32> {
|
||||||
|
jsonrpc: "",
|
||||||
|
id: RequestId::Int(0),
|
||||||
|
value: LspResult::Error(None),
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_string(&no_tag).unwrap(),
|
||||||
|
"{\"jsonrpc\":\"\",\"id\":0,\"error\":null}"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue