Skip to content

Commit

Permalink
fix: correctly deserialize Option with tagged type
Browse files Browse the repository at this point in the history
Signed-off-by: Stan Bondi <sdbondi@users.noreply.github.com>
  • Loading branch information
sdbondi authored and rjzak committed Apr 25, 2023
1 parent dc9d27d commit ac38b3d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
17 changes: 7 additions & 10 deletions ciborium/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,16 +518,13 @@ where

#[inline]
fn deserialize_option<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
loop {
return match self.decoder.pull()? {
Header::Simple(simple::UNDEFINED) => visitor.visit_none(),
Header::Simple(simple::NULL) => visitor.visit_none(),
Header::Tag(..) => continue,
header => {
self.decoder.push(header);
visitor.visit_some(self)
}
};
match self.decoder.pull()? {
Header::Simple(simple::UNDEFINED) => visitor.visit_none(),
Header::Simple(simple::NULL) => visitor.visit_none(),
header => {
self.decoder.push(header);
visitor.visit_some(self)
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions ciborium/tests/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
extern crate std;

use ciborium::cbor;
use ciborium::tag::Required;
use ciborium::value::CanonicalValue;
use rand::prelude::*;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -89,3 +90,22 @@ fn negative_numbers() {

assert_eq!(array, golden);
}

#[test]
fn tagged_option() {
let mut opt = Some(Required::<u64, 0xff>(2u32.into()));

let mut bytes = Vec::new();
ciborium::ser::into_writer(&opt, &mut bytes).unwrap();

let output = ciborium::de::from_reader(&bytes[..]).unwrap();
assert_eq!(opt, output);

opt = None;

let mut bytes = Vec::new();
ciborium::ser::into_writer(&opt, &mut bytes).unwrap();

let output = ciborium::de::from_reader(&bytes[..]).unwrap();
assert_eq!(opt, output);
}

0 comments on commit ac38b3d

Please sign in to comment.