From 43dad25dd4fdce37a9d47cf1276de5942626ba85 Mon Sep 17 00:00:00 2001 From: Carl Sverre Date: Thu, 5 Oct 2023 18:55:11 -0700 Subject: [PATCH] Fix using serde_bytes with an internally tagged enum (#58) --- src/de.rs | 4 ++++ tests/common/mod.rs | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/de.rs b/src/de.rs index 06754b1..02b70d7 100644 --- a/src/de.rs +++ b/src/de.rs @@ -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 diff --git a/tests/common/mod.rs b/tests/common/mod.rs index c5a5c36..f5a56b2 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -598,9 +598,19 @@ fn enums() { A: Ord, { Unit, - Struct { a: A, b: B }, - Sequence { seq: Vec }, + Struct { + a: A, + b: B, + }, + Sequence { + seq: Vec, + }, Map(BTreeMap), + Bytes { + #[serde(with = "serde_bytes")] + serde_bytes: Vec, + raw: Vec, + }, } test_via_json(InternallyTagged::Unit::<(), ()>); @@ -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