Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-3821

Rust: Record (de?)serialization is sensitive to order of fields in struct

    XMLWordPrintableJSON

Details

    Description

      A previous issue describes a bug where record validation failed due to a struct's field declaration order not matching its schema: https://issues.apache.org/jira/browse/AVRO-3495

       

      That issue's fix appears to be incomplete. Validation during writing works, however the written records cannot be read back - it appears that the fields are mismatched during deserialization. I've reproduced using the existing `avro_old_issue_47` test case with a modification to read the data back:

       

      diff --git a/lang/rust/avro/tests/schema.rs b/lang/rust/avro/tests/schema.rs
      index 00be0ab01..d5b1ee904 100644
      --- a/lang/rust/avro/tests/schema.rs
      +++ b/lang/rust/avro/tests/schema.rs
      @@ -18,6 +18,7 @@
       use std::io::{Cursor, Read};
       
       use apache_avro::{
      +    from_avro_datum, from_value,
           schema::{EnumSchema, FixedSchema, Name, RecordField, RecordSchema},
           to_avro_datum, to_value,
           types::{Record, Value},
      @@ -1452,7 +1453,7 @@ fn avro_old_issue_47() -> TestResult {
       
           use serde::{Deserialize, Serialize};
       
      -    #[derive(Deserialize, Serialize)]
      +    #[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
           pub struct MyRecord {
               b: String,
               a: i64,
      @@ -1463,7 +1464,15 @@ fn avro_old_issue_47() -> TestResult {
               a: 1,
           };
       
      -    let _ = to_avro_datum(&schema, to_value(record)?)?;
      +    let serialized_bytes = to_avro_datum(&schema, to_value(record.clone())?)?;
      +
      +    let deserialized_record = from_value::<MyRecord>(&from_avro_datum(
      +        &schema,
      +        &mut Cursor::new(serialized_bytes),
      +        None,
      +    )?)?;
      +
      +    assert_eq!(record, deserialized_record);
           Ok(())
       }
       
       

      This fails as written, and passes if `a` and `b` are swapped in the MyRecord definition.

       

      Attachments

        Issue Links

          Activity

            People

              mgrigorov Martin Tzvetanov Grigorov
              rnarubin Renar Narubin
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m