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

[Rust] Validation fails for

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Not A Problem
    • None
    • None
    • rust

    Description

      Schema validation fails when referencing a previously defined type which has inherited a namespace.

       

      Schema:

       

      {
          "type": "record",
          "name": "Foo",
          "namespace": "name.space",
          "fields":
          [
              {
                  "name": "barInit",
                  "type":
                  {
                      "type": "enum",
                      "name": "Bar",
                      "symbols":
                      [
                          "bar0",
                          "bar1"
                      ]
                  }
              },
              {
                  "name": "barUse",
                  "type": "Bar"
              }
          ]
      } 

       

       

      Rust Objects (from rsgen)

       

      #[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Clone, serde::Deserialize, serde::Serialize,)]
      pub enum Bar {
          #[serde(rename = "bar0")]
          Bar0,
          #[serde(rename = "bar1")]
          Bar1,
      }
      
      #[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
      pub struct Foo {
          #[serde(rename = "barInit")]
          pub bar_init: Bar,
          #[serde(rename = "barUse")]
          pub bar_use: Bar,
      }
      

       

       

      Test

       

      #[test]
      fn test_serialize() {
          testing_logger::setup();
      
          let schema = Schema::parse_str(&get_raw_example_schema()).unwrap();
          let foo = Foo {
              bar_init: Bar::Bar0,
              bar_use: Bar::Bar1,
          };
      
          let avro_value = to_value(foo).unwrap();
          println!(
              "value is valid for schema: {}",
              avro_value.validate(&schema)
          );
          testing_logger::validate(|logs| {
              for log in logs {
                  println!("{}", log.body)
              }
          });
      
          let mut writer = Writer::new(&schema, Vec::new());
      
          // schema validation happens here
          writer.append(avro_value).unwrap();
      } 

       

       

      Test & Log Output

       

      value is valid for schema: false
      Invalid value: Record([("barInit", Enum(0, "bar0")), ("barUse", Enum(1, "bar1"))]) for schema: Record { name: Name { name: "Foo", namespace: Some("name.space") }, aliases: None, doc: None, fields: [RecordField { name: "barInit", doc: None, default: None, schema: Enum { name: Name { name: "Bar", namespace: None }, aliases: None, doc: None, symbols: ["bar0", "bar1"] }, order: Ascending, position: 0 }, RecordField { name: "barUse", doc: None, default: None, schema: Ref { name: Name { name: "Bar", namespace: None } }, order: Ascending, position: 1 }], lookup: {"barInit": 0, "barUse": 1} }. Reason: Unresolved schema reference: 'Bar'. Parsed names: [Name { name: "Bar", namespace: Some("name.space") }, Name { name: "Foo", namespace: Some("name.space") }]
      thread 'test::test_serialize' panicked at 'called `Result::unwrap()` on an `Err` value: ValidationWithReason("Unresolved schema reference: 'Bar'. Parsed names: [Name { name: \"Foo\", namespace: Some(\"name.space\") }, Name { name: \"Bar\", namespace: Some(\"name.space\") }]")', src/main.rs:33:35 

       

      full test repo is attached

       

      Attachments

        1. avro_example.zip
          22 kB
          Matthew Cargille

        Issue Links

          Activity

            People

              mgrigorov Martin Tzvetanov Grigorov
              mcargille__optum Matthew Cargille
              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