Skip to content

Commit 3ff690f

Browse files
authored
feat: handle null coercion in cynic-parser-deser (#1100)
1 parent 0115d67 commit 3ff690f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

cynic-parser-deser/src/deserialize.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,17 @@ where
152152
fn deserialize(input: DeserValue<'a>) -> Result<Self, Error> {
153153
match input {
154154
DeserValue::List(list) => list.items().map(T::deserialize).collect(),
155-
other => Err(Error::unexpected_type(ValueType::List, other)),
155+
other => {
156+
if !other.is_null() {
157+
// List coercion
158+
//
159+
// I am not 100% sure this is right but lets see...
160+
if let Ok(inner) = T::deserialize(other) {
161+
return Ok(vec![inner]);
162+
}
163+
}
164+
Err(Error::unexpected_type(ValueType::List, other))
165+
}
156166
}
157167
}
158168
}

cynic-parser-deser/tests/deser.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,37 @@ fn test_rename_rule() {
9393
assert_eq!(deser::<RenameRule>("@id(fooBar: 1)").unwrap().foo_bar, 1);
9494
}
9595

96+
#[derive(ValueDeserialize, PartialEq, Debug)]
97+
struct ListCoercion {
98+
ints: Vec<u32>,
99+
strings: Vec<String>,
100+
}
101+
102+
#[test]
103+
fn test_list_coercion() {
104+
assert_eq!(
105+
deser::<ListCoercion>("@id(ints: 1, strings: \"hello\")").unwrap(),
106+
ListCoercion {
107+
ints: vec![1],
108+
strings: vec!["hello".into()]
109+
}
110+
);
111+
112+
assert_eq!(
113+
deser::<ListCoercion>("@id(ints: \"hello\", strings: 1)")
114+
.unwrap_err()
115+
.to_string(),
116+
"found a String where we expected a List"
117+
);
118+
119+
assert_eq!(
120+
deser::<ListCoercion>("@id(ints: null, strings: 1)")
121+
.unwrap_err()
122+
.to_string(),
123+
"found a Null where we expected a List"
124+
);
125+
}
126+
96127
fn deser<T>(input: &str) -> Result<T, cynic_parser_deser::Error>
97128
where
98129
T: ValueDeserializeOwned,

0 commit comments

Comments
 (0)