fix(juniper_graphql_ws): correct null deserialization issue (#738)
Closes #735
This commit is contained in:
parent
8d7ba8295c
commit
fdad97aa08
4 changed files with 31 additions and 2 deletions
juniper_graphql_ws
|
@ -1,3 +1,4 @@
|
||||||
# master
|
# master
|
||||||
|
|
||||||
|
- Fix null deserialization issue ([#735](https://github.com/graphql-rust/juniper/issues/735))
|
||||||
- Initial Release
|
- Initial Release
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::utils::default_for_null;
|
||||||
use juniper::{ScalarValue, Variables};
|
use juniper::{ScalarValue, Variables};
|
||||||
|
|
||||||
/// The payload for a client's "start" message. This triggers execution of a query, mutation, or
|
/// The payload for a client's "start" message. This triggers execution of a query, mutation, or
|
||||||
|
@ -10,7 +11,7 @@ pub struct StartPayload<S: ScalarValue> {
|
||||||
pub query: String,
|
pub query: String,
|
||||||
|
|
||||||
/// The optional variables.
|
/// The optional variables.
|
||||||
#[serde(default)]
|
#[serde(default, deserialize_with = "default_for_null")]
|
||||||
pub variables: Variables<S>,
|
pub variables: Variables<S>,
|
||||||
|
|
||||||
/// The optional operation name (required if the document contains multiple operations).
|
/// The optional operation name (required if the document contains multiple operations).
|
||||||
|
@ -27,7 +28,7 @@ pub enum ClientMessage<S: ScalarValue> {
|
||||||
ConnectionInit {
|
ConnectionInit {
|
||||||
/// Optional parameters of any type sent from the client. These are often used for
|
/// Optional parameters of any type sent from the client. These are often used for
|
||||||
/// authentication.
|
/// authentication.
|
||||||
#[serde(default)]
|
#[serde(default, deserialize_with = "default_for_null")]
|
||||||
payload: Variables<S>,
|
payload: Variables<S>,
|
||||||
},
|
},
|
||||||
/// Start messages are used to execute a GraphQL operation.
|
/// Start messages are used to execute a GraphQL operation.
|
||||||
|
@ -128,4 +129,20 @@ mod test {
|
||||||
serde_json::from_str(r##"{"type": "connection_terminate"}"##).unwrap(),
|
serde_json::from_str(r##"{"type": "connection_terminate"}"##).unwrap(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_deserialization_of_null() -> serde_json::Result<()> {
|
||||||
|
let payload = r#"{"query":"query","variables":null}"#;
|
||||||
|
let payload: StartPayload<DefaultScalarValue> = serde_json::from_str(payload)?;
|
||||||
|
|
||||||
|
let expected = StartPayload {
|
||||||
|
query: "query".into(),
|
||||||
|
variables: Variables::default(),
|
||||||
|
operation_name: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(expected, payload);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,8 @@ pub use server_message::*;
|
||||||
mod schema;
|
mod schema;
|
||||||
pub use schema::*;
|
pub use schema::*;
|
||||||
|
|
||||||
|
mod utils;
|
||||||
|
|
||||||
use juniper::{
|
use juniper::{
|
||||||
futures::{
|
futures::{
|
||||||
channel::oneshot,
|
channel::oneshot,
|
||||||
|
|
9
juniper_graphql_ws/src/utils.rs
Normal file
9
juniper_graphql_ws/src/utils.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
use serde::{Deserialize, Deserializer};
|
||||||
|
|
||||||
|
pub(crate) fn default_for_null<'de, D, T>(deserializer: D) -> Result<T, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
T: Deserialize<'de> + Default,
|
||||||
|
{
|
||||||
|
Ok(Option::<T>::deserialize(deserializer)?.unwrap_or_default())
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue