Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): Response ignore unknown fields #1353

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading