Skip to content

Commit 22d4bf1

Browse files
committed
Fix #6: implicitly defined named schemas should resolve in union variants
1 parent 961b35a commit 22d4bf1

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/value.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,10 @@ fn resolve_union<'a>(
357357
| (Value::Bytes(_), Variant::Bytes)
358358
| (Value::Str(_), Variant::Str)
359359
| (Value::Map(_), Variant::Map { .. })
360-
| (Value::Array(_), Variant::Array { .. }) => return Ok((idx, variant)),
361-
(Value::Fixed(_), Variant::Fixed { .. }) => return Ok((idx, variant)),
360+
| (Value::Array(_), Variant::Array { .. })
361+
| (Value::Fixed(_), Variant::Fixed { .. })
362+
| (Value::Enum(_), Variant::Enum { .. })
363+
| (Value::Record(_), Variant::Record { .. }) => return Ok((idx, variant)),
362364
(Value::Array(v), Variant::Fixed { size, .. }) => {
363365
if v.len() == *size {
364366
return Ok((idx, variant));

tests/read_write.rs

+68
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,71 @@ fn read_deflate_reuse() {
402402
let _v: LongList = from_value(&i).unwrap();
403403
}
404404
}
405+
406+
#[test]
407+
fn parses_field_record_defined_within_union() {
408+
#[derive(Serialize, Deserialize, PartialEq, Debug)]
409+
struct Reference {
410+
#[serde(rename = "feedReference")]
411+
pub feed_reference: Option<FeedReference>,
412+
}
413+
414+
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
415+
pub struct FeedReference {
416+
pub instance: String,
417+
pub provider: String,
418+
}
419+
420+
impl Default for FeedReference {
421+
fn default() -> FeedReference {
422+
FeedReference {
423+
instance: String::default(),
424+
provider: String::default(),
425+
}
426+
}
427+
}
428+
429+
let schema = r##"
430+
{
431+
"name": "Reference",
432+
"type": "record",
433+
"fields": [
434+
{
435+
"name": "feedReference",
436+
"type": [
437+
"null",
438+
{
439+
"name": "FeedReference",
440+
"type": "record",
441+
"fields": [
442+
{
443+
"name": "instance",
444+
"type": "string"
445+
},
446+
{
447+
"name": "provider",
448+
"type": "string"
449+
}
450+
]
451+
}
452+
],
453+
"default": null
454+
}
455+
]
456+
}
457+
"##;
458+
459+
let reference = Reference {
460+
feed_reference: Some(FeedReference::default()),
461+
};
462+
463+
let schema = Schema::from_str(&schema).unwrap();
464+
let mut writer = avrow::Writer::new(&schema, vec![]).unwrap();
465+
writer.serialize(&reference).unwrap();
466+
let a = writer.into_inner().unwrap();
467+
let reader = Reader::new(a.as_slice()).unwrap();
468+
for i in reader {
469+
let a: Reference = from_value(&i).unwrap();
470+
assert_eq!(a, reference);
471+
}
472+
}

0 commit comments

Comments
 (0)