Skip to content

Commit

Permalink
Fix using serde_bytes with an internally tagged enum (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlsverre authored Oct 6, 2023
1 parent 0138156 commit 43dad25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ impl<'de> de::Deserializer<'de> for Deserializer {
visitor.visit_string(v)
} else if Array::is_array(&self.value) {
self.deserialize_seq(visitor)
} else if let Some(bytes) = self.as_bytes() {
// We need to handle this here because serde uses `deserialize_any`
// for internally tagged enums
visitor.visit_byte_buf(bytes)
} else if self.value.is_object() &&
// The only reason we want to support objects here is because serde uses
// `deserialize_any` for internally tagged enums
Expand Down
22 changes: 20 additions & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,19 @@ fn enums() {
A: Ord,
{
Unit,
Struct { a: A, b: B },
Sequence { seq: Vec<A> },
Struct {
a: A,
b: B,
},
Sequence {
seq: Vec<A>,
},
Map(BTreeMap<A, B>),
Bytes {
#[serde(with = "serde_bytes")]
serde_bytes: Vec<u8>,
raw: Vec<u8>,
},
}

test_via_json(InternallyTagged::Unit::<(), ()>);
Expand Down Expand Up @@ -636,6 +646,14 @@ fn enums() {
&BIGINT_SERIALIZER,
);

test_via_round_trip_with_config(
InternallyTagged::<(), ()>::Bytes {
serde_bytes: vec![0, 1, 2],
raw: vec![3, 4, 5],
},
&SERIALIZER,
);

test_enum! {
#[serde(tag = "tag", content = "content")]
AdjacentlyTagged
Expand Down

0 comments on commit 43dad25

Please sign in to comment.