From 4d1ac461b94ca6e28312bc3b3c06f9982001ffef Mon Sep 17 00:00:00 2001 From: tom Date: Tue, 10 Oct 2023 15:03:57 +0800 Subject: [PATCH] AVRO-3881: [rust] Writer should write user metadata even if the body is empty (#2545) * writer should write user metadata even if the body is empty * AVRO-3881: Give a better name to a new test case --------- Co-authored-by: Martin Grigorov --- lang/rust/avro/src/writer.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lang/rust/avro/src/writer.rs b/lang/rust/avro/src/writer.rs index 83b7d8b00a0..8802ce604a9 100644 --- a/lang/rust/avro/src/writer.rs +++ b/lang/rust/avro/src/writer.rs @@ -323,6 +323,7 @@ impl<'a, W: Write> Writer<'a, W> { /// **NOTE** This function forces the written data to be flushed (an implicit /// call to [`flush`](struct.Writer.html#method.flush) is performed). pub fn into_inner(mut self) -> AvroResult { + self.maybe_write_header()?; self.flush()?; Ok(self.writer) } @@ -635,6 +636,7 @@ mod tests { schema::{DecimalSchema, FixedSchema, Name}, types::Record, util::zig_i64, + Reader, }; use pretty_assertions::assert_eq; use serde::{Deserialize, Serialize}; @@ -1139,6 +1141,22 @@ mod tests { Ok(()) } + #[test] + fn test_avro_3881_metadata_empty_body() -> TestResult { + let schema = Schema::parse_str(SCHEMA)?; + let mut writer = Writer::new(&schema, Vec::new()); + writer.add_user_metadata("a".to_string(), "b")?; + let result = writer.into_inner()?; + + let reader = Reader::with_schema(&schema, &result[..])?; + let mut expected = HashMap::new(); + expected.insert("a".to_string(), vec![b'b']); + assert_eq!(reader.user_metadata(), &expected); + assert_eq!(reader.into_iter().count(), 0); + + Ok(()) + } + #[test] fn test_avro_3405_writer_add_metadata_failure() -> TestResult { let schema = Schema::parse_str(SCHEMA)?;