Skip to content

Commit

Permalink
fix(types): Response ignore unknown fields (#1353)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Apr 29, 2024
1 parent 5134502 commit ad67381
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions types/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl<'a, T: Clone> ResponsePayload<'a, T> {
Self::Error(e.into())
}

/// Create a borrowd error response payload.
/// Create a borrowed error response payload.
pub fn error_borrowed(e: impl Into<ErrorObject<'a>>) -> Self {
Self::Error(e.into())
}
Expand All @@ -189,11 +189,13 @@ where
D: Deserializer<'de>,
T: Deserialize<'de> + Clone,
{
#[derive(Debug)]
enum Field {
Jsonrpc,
Result,
Error,
Id,
Ignore,
}

impl<'de> Deserialize<'de> for Field {
Expand All @@ -219,7 +221,7 @@ where
"result" => Ok(Field::Result),
"error" => Ok(Field::Error),
"id" => Ok(Field::Id),
_ => Err(serde::de::Error::unknown_field(value, FIELDS)),
_ => Ok(Field::Ignore),
}
}
}
Expand Down Expand Up @@ -279,6 +281,9 @@ where
}
jsonrpc = Some(map.next_value()?);
}
Field::Ignore => {
let _ = map.next_value::<serde::de::IgnoredAny>()?;
}
}
}

Expand Down Expand Up @@ -405,4 +410,14 @@ mod tests {
assert_eq!(dsr.payload, exp.payload);
assert_eq!(dsr.id, exp.id);
}

#[test]
fn deserialize_with_unknown_field() {
let exp = Response { jsonrpc: None, payload: ResponsePayload::success(99_u64), id: Id::Number(11) };
let dsr: Response<u64> =
serde_json::from_str(r#"{"jsonrpc":null, "result":99, "id":11, "unknown":11}"#).unwrap();
assert_eq!(dsr.jsonrpc, exp.jsonrpc);
assert_eq!(dsr.payload, exp.payload);
assert_eq!(dsr.id, exp.id);
}
}

0 comments on commit ad67381

Please sign in to comment.