Skip to content

Commit

Permalink
[serde-ser] Fix incorrect field count handling in skip_field
Browse files Browse the repository at this point in the history
Apparently the specified expected length does not include skipped fields.

Fixes #41
  • Loading branch information
Marcono1234 committed Jan 30, 2024
1 parent 4dec779 commit 456c193
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
28 changes: 2 additions & 26 deletions src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,18 +796,6 @@ impl<W: JsonWriter> serde::ser::SerializeStruct for SerializeStruct<'_, '_, W> {
value.serialize(&mut *self.ser)
}

fn skip_field(&mut self, _key: &'static str) -> Result<(), Self::Error> {
if self.len >= self.expected_len {
return Err(SerializerError::IncorrectElementsCount {
expected: self.expected_len,
// + 1 for currently added field
actual: self.len + 1,
});
}
self.len += 1;
Ok(())
}

fn end(self) -> Result<Self::Ok, Self::Error> {
if self.len < self.expected_len {
return Err(SerializerError::IncorrectElementsCount {
Expand Down Expand Up @@ -850,18 +838,6 @@ impl<W: JsonWriter> serde::ser::SerializeStructVariant for SerializeStructVarian
value.serialize(&mut *self.ser)
}

fn skip_field(&mut self, _key: &'static str) -> Result<(), Self::Error> {
if self.len >= self.expected_len {
return Err(SerializerError::IncorrectElementsCount {
expected: self.expected_len,
// + 1 for currently added field
actual: self.len + 1,
});
}
self.len += 1;
Ok(())
}

fn end(self) -> Result<Self::Ok, Self::Error> {
if self.len < self.expected_len {
return Err(SerializerError::IncorrectElementsCount {
Expand Down Expand Up @@ -1739,7 +1715,7 @@ mod tests {

assert_serialized_cmp!(
|s| {
let mut struc = s.serialize_struct("name", 3)?;
let mut struc = s.serialize_struct("name", 2)?;
struc.serialize_field("key1", &1)?;
struc.skip_field("skipped")?;
struc.serialize_field("key2", &2)?;
Expand Down Expand Up @@ -1781,7 +1757,7 @@ mod tests {

assert_serialized_cmp!(
|s| {
let mut struc = s.serialize_struct_variant("name", 1, "variant", 3)?;
let mut struc = s.serialize_struct_variant("name", 1, "variant", 2)?;
struc.serialize_field("key1", &1)?;
struc.skip_field("skipped")?;
struc.serialize_field("key2", &2)?;
Expand Down
37 changes: 37 additions & 0 deletions tests/serde_serialize_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,40 @@ fn serialize_unit_variant() {

assert_serialized(E::A, "\"A\"");
}

#[test]
fn serialize_skipped_field() {
#[derive(Serialize)]
struct S {
a: u32,
#[allow(dead_code)]
#[serde(skip)]
b: u32,
}

assert_serialized(S { a: 1, b: 2 }, r#"{"a":1}"#);
}

#[test]
fn serialize_conditional_skipped_field() {
#[derive(Serialize)]
struct S {
a: u32,
#[serde(skip_serializing_if = "Option::is_none")]
b: Option<u32>,
}

assert_serialized(S { a: 1, b: Some(2) }, r#"{"a":1,"b":2}"#);
assert_serialized(S { a: 1, b: None }, r#"{"a":1}"#);

#[derive(Serialize)]
enum E {
S {
a: u32,
#[serde(skip_serializing_if = "Option::is_none")]
b: Option<u32>,
},
}
assert_serialized(E::S { a: 1, b: Some(2) }, r#"{"S":{"a":1,"b":2}}"#);
assert_serialized(E::S { a: 1, b: None }, r#"{"S":{"a":1}}"#);
}

0 comments on commit 456c193

Please sign in to comment.