File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -152,7 +152,17 @@ where
152
152
fn deserialize ( input : DeserValue < ' a > ) -> Result < Self , Error > {
153
153
match input {
154
154
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
+ }
156
166
}
157
167
}
158
168
}
Original file line number Diff line number Diff line change @@ -93,6 +93,37 @@ fn test_rename_rule() {
93
93
assert_eq ! ( deser:: <RenameRule >( "@id(fooBar: 1)" ) . unwrap( ) . foo_bar, 1 ) ;
94
94
}
95
95
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
+
96
127
fn deser < T > ( input : & str ) -> Result < T , cynic_parser_deser:: Error >
97
128
where
98
129
T : ValueDeserializeOwned ,
You can’t perform that action at this time.
0 commit comments