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

Schema resolution panics when a custom record field is included multiple times

    XMLWordPrintableJSON

Details

    Description

      Consider the following Avro schema:

      {
          "type": "record",
          "name": "Message",
          "fields": [
              {
                  "name": "field_a",
                  "type": [
                      "null",
                      {
                          "name": "Inner",
                          "type": "record",
                          "fields": [
                              {
                                  "name": "inner_a",
                                  "type": "string"
                              }
                          ]
                      }
                  ],
                  "default": null
              },
              {
                  "name": "field_b",
                  "type": [
                      "null",
                      "Inner"
                  ],
                  "default": null
              }
          ]
      }

      This can be represented in Rust through the following structs:

      #[derive(Serialize, Deserialize)]
      struct Inner {
          inner_a: String
      }
      
      #[derive(Serialize, Deserialize)]
      struct Message {
          field_a: Option<Inner>,
          field_b: Option<Inner>
      }
      

      If I instantiate an instance of `message`, set `field_a` and then serialize it using the following code:

              let schema = Schema::parse_str(&schema_str).unwrap();
      
              let msg = Message {
                  field_a: Some(Inner {
                      inner_a: "foo".to_string()
                  }),
                  field_b: None
              };
      
              let mut ser = Serializer::default();
              let test_value: Value = msg.serialize(&mut ser).unwrap();
              assert!(test_value.validate(&schema), "test_value should validate");
              assert!(
                  test_value.resolve(&schema).is_ok(),
                  "test_value should resolve"
              );
      

      Then my assertions pass. However if I set field_b to `Inner`, then my call to `test_value.resolve(&schema)` panics:

      let schema = Schema::parse_str(&schema_str).unwrap();
      
      let msg = Message {
          field_a: Some(Inner {
              inner_a: "foo".to_string()
          }),
          field_b: Some(Inner {
              inner_a: "bar".to_string()
          })
      };
      
      let mut ser = Serializer::default();
      let test_value: Value = msg.serialize(&mut ser).unwrap();
      assert!(test_value.validate(&schema), "test_value should validate");
      assert!(
          test_value.resolve(&schema).is_ok(),
          "test_value should resolve"
      );
      

      This seems to be a bug in the schema resolution logic that's causing an unhandled panic.

      Attachments

        Issue Links

          Activity

            People

              rheijdens Rik Heijdens
              rheijdens Rik Heijdens
              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 - 0.5h
                  0.5h