Skip to content

Commit

Permalink
feat: handle null coercion in cynic-parser-deser (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
obmarg authored Nov 20, 2024
1 parent 0115d67 commit 3ff690f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cynic-parser-deser/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,17 @@ where
fn deserialize(input: DeserValue<'a>) -> Result<Self, Error> {
match input {
DeserValue::List(list) => list.items().map(T::deserialize).collect(),
other => Err(Error::unexpected_type(ValueType::List, other)),
other => {
if !other.is_null() {
// List coercion
//
// I am not 100% sure this is right but lets see...
if let Ok(inner) = T::deserialize(other) {
return Ok(vec![inner]);
}
}
Err(Error::unexpected_type(ValueType::List, other))
}
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions cynic-parser-deser/tests/deser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,37 @@ fn test_rename_rule() {
assert_eq!(deser::<RenameRule>("@id(fooBar: 1)").unwrap().foo_bar, 1);
}

#[derive(ValueDeserialize, PartialEq, Debug)]
struct ListCoercion {
ints: Vec<u32>,
strings: Vec<String>,
}

#[test]
fn test_list_coercion() {
assert_eq!(
deser::<ListCoercion>("@id(ints: 1, strings: \"hello\")").unwrap(),
ListCoercion {
ints: vec![1],
strings: vec!["hello".into()]
}
);

assert_eq!(
deser::<ListCoercion>("@id(ints: \"hello\", strings: 1)")
.unwrap_err()
.to_string(),
"found a String where we expected a List"
);

assert_eq!(
deser::<ListCoercion>("@id(ints: null, strings: 1)")
.unwrap_err()
.to_string(),
"found a Null where we expected a List"
);
}

fn deser<T>(input: &str) -> Result<T, cynic_parser_deser::Error>
where
T: ValueDeserializeOwned,
Expand Down

0 comments on commit 3ff690f

Please sign in to comment.