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

Value::Record containing enums fail to validate when using namespaces in Schema

    XMLWordPrintableJSON

Details

    Description

      Consider the following schema:

      {
          "type": "record",
          "name": "NamespacedMessage",
          "namespace": "com.domain",
          "fields": [
              {
                  "type": "record",
                  "name": "field_a",
                  "fields": [
                      {
                          "name": "enum_a",
                          "type": {
                              "type": "enum",
                              "name": "EnumType",
                              "symbols": [
                                  "SYMBOL_1",
                                  "SYMBOL_2"
                              ],
                              "default": "SYMBOL_1"
                          }
                      },
                      {
                          "name": "enum_b",
                          "type": "EnumType"
                      }
                  ]
              }
          ]
      }

      I might represent this in Rust using the following structs:

      #[derive(Serialize)]
      enum EnumType {
          #[serde(rename = "SYMBOL_1")]
          Symbol1,
          #[serde(rename = "SYMBOL_2")]
          Symbol2,
      }
      
      #[derive(Serialize)]
      struct FieldA {
          enum_a: EnumType,
          enum_b: EnumType,
      }
      
      #[derive(Serialize)]
      struct NamespacedMessage {
          field_a: FieldA,
      }
      
      let msg = NamespacedMessage {
          field_a: FieldA {
              enum_a: EnumType::Symbol2,
              enum_b: EnumType::Symbol1,
          },
      };
      

      and then serialize this into a `Value` using the following logic:

      let mut ser = Serializer::default();
      let test_value: Value = msg.serialize(&mut ser).unwrap();
      

      After serializing into `test_value` I would expect that `test_value.validate(&schema)` yields True. However this is not the case.

      I can work around it by removing the `namespace` definition from my schema which allows the validation to proceed.

      I believe the cause of schema validation failure is [this lookup failing](https://github.com/apache/avro/blob/release-1.11.1-rc1/lang/rust/avro/src/types.rs#L370) when schemas are utilized as the `Value` will not have a namespace associated with it.

      I believe this could be fixed by altering `validate_internal` to accept an optional namespace that is derived from the provided schema to `validate`. However, I'm not sure if this is an appropriate fix.

      Attachments

        Activity

          People

            mgrigorov Martin Tzvetanov Grigorov
            rheijdens Rik Heijdens
            Votes:
            0 Vote for this issue
            Watchers:
            4 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 - 2h 20m
                2h 20m