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

Detect unknown struct variant fields deserializing from Value #796

Merged
merged 2 commits into from Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/value/de.rs
Expand Up @@ -538,7 +538,7 @@ impl<'de> VariantAccess<'de> for VariantDeserializer {
V: Visitor<'de>,
{
match self.value {
Some(Value::Object(v)) => visitor.visit_map(MapDeserializer::new(v)),
Some(Value::Object(v)) => visit_object(v, visitor),
Some(other) => Err(serde::de::Error::invalid_type(
other.unexpected(),
&"struct variant",
Expand Down Expand Up @@ -1021,7 +1021,7 @@ impl<'de> VariantAccess<'de> for VariantRefDeserializer<'de> {
V: Visitor<'de>,
{
match self.value {
Some(&Value::Object(ref v)) => visitor.visit_map(MapRefDeserializer::new(v)),
Some(&Value::Object(ref v)) => visit_object_ref(v, visitor),
Some(other) => Err(serde::de::Error::invalid_type(
other.unexpected(),
&"struct variant",
Expand Down
57 changes: 57 additions & 0 deletions tests/regression/issue795.rs
@@ -0,0 +1,57 @@
use serde::de::{
Deserialize, Deserializer, EnumAccess, IgnoredAny, MapAccess, VariantAccess, Visitor,
};
use serde_json::json;
use std::fmt;

#[derive(Debug)]
enum Enum {
Variant { x: u8 },
}

impl<'de> Deserialize<'de> for Enum {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct EnumVisitor;

impl<'de> Visitor<'de> for EnumVisitor {
type Value = Enum;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("enum Enum")
}

fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
where
A: EnumAccess<'de>,
{
let (IgnoredAny, variant) = data.variant()?;
variant.struct_variant(&["x"], self)
}

fn visit_map<A>(self, mut data: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut x = 0;
if let Some((IgnoredAny, value)) = data.next_entry()? {
x = value;
}
Ok(Enum::Variant { x })
}
}

deserializer.deserialize_enum("Enum", &["Variant"], EnumVisitor)
}
}

#[test]
fn test() {
let s = r#" {"Variant":{"x":0,"y":0}} "#;
assert!(serde_json::from_str::<Enum>(s).is_err());

let j = json!({"Variant":{"x":0,"y":0}});
assert!(serde_json::from_value::<Enum>(j).is_err());
}