Skip to content

Invalid canonical form parsing in some situations when array items has an inlined record type #292

Closed

Description

@vkopio

I noticed that with version 0.20.0 when a schema has an array with an inlined record in the items field, the canonical form parsing will produce an invalid schema in certain situations.

This can be reproduced with the following function. Here the inlining is done with two nested arrays:

fn avro_bug() {
    let raw_schema = r#"{
            "type": "array",
            "items": {
                "name": "foo",
                "type": "record",
                "fields": [
                    {
                        "name": "bar",
                        "type": "array",
                        "items": {
                            "type": "record",
                            "name": "baz",
                            "fields": [
                                {
                                    "name": "quux",
                                    "type": "int"
                                }
                            ]
                        }
                    }
                ]
            }
        }"#;

    let schema = apache_avro::Schema::parse_str(&raw_schema).unwrap();

    println!("{}", &schema.canonical_form());

    let schema2 = apache_avro::Schema::parse_str(&schema.canonical_form()).unwrap(); // Panics here

    println!("{}", &schema2.canonical_form());
}

The output is the following:

{"type":"array","items":{"name":"foo","type":"record","fields":[{"name":"bar","type":{"type":"array","items":"baz"},"items":{"name":"baz","type":"record","fields":[{"name":"quux","type":"int"}]}}]}}

thread 'main' panicked at src/main.rs:30:76:
called `Result::unwrap()` on an `Err` value: Error { details: Unknown primitive type: baz }

Expanding the output from the first canonical form:

{
  "type": "array",
  "items": {
    "name": "foo",
    "type": "record",
    "fields": [
      {
        "name": "bar",
        "type": {
          "type": "array",
          "items": "baz"
        },
        "items": {
          "name": "baz",
          "type": "record",
          "fields": [
            {
              "name": "quux",
              "type": "int"
            }
          ]
        }
      }
    ]
  }
}

So the type of bar changes from "array" to {"type": "array", "items": "baz"}, but the outer array is canonized correctly.

However, if we wrap the outer array into a record, this type change happens with the outer array but the inner array is canonized correctly. Here is a schema that can be used to reproduce this:

{
  "name": "wrapper",
  "type": "record",
  "fields": [
    {
      "name": "wrapped",
      "type": "array",
      "items": {
        "name": "foo",
        "type": "record",
        "fields": [
          {
            "name": "bar",
            "type": "array",
            "items": {
              "type": "record",
              "name": "baz",
              "fields": [
                {
                  "name": "quux",
                  "type": "int"
                }
              ]
            }
          }
        ]
      }
    }
  ]
}

Activity

  1. martin-g commented on Sep 20, 2025

    @martin-g
    Member

    Thanks for the report!
    I'll try to find some time to debug it soon!
    The problem seems to be around "... type":{"type":"array","items":"baz"},"items":{"name":"baz"...

    I noticed that with version 0.20.0 when a schema has an array with an inlined record in the items field, the canonical form parsing will produce an invalid schema in certain situations.

    Did it work correctly in 0.19.0 ? I.e. is there a regression in 0.20.0 ?
    Or you just tried it with 0.20.0 for a first time ?

  2. vkopio commented on Sep 22, 2025

    @vkopio
    Author

    Did it work correctly in 0.19.0 ? I.e. is there a regression in 0.20.0 ? Or you just tried it with 0.20.0 for a first time ?

    I tested this only using 0.20.0. I mentioned the version for the sake of reproducibility, but should have been more clear.

  3. self-assigned this
    on Sep 30, 2025
  4. martin-g commented on Sep 30, 2025

    @martin-g
    Member

    The following schema works fine:

    let raw_schema = r#"{
                      "type": "array",
                      "items": {
                          "name": "foo",
                          "type": "record",
                          "fields": [
                              {
                                  "name": "bar",
                                  "type": {
                                      "type": "array",
                                      "items": {
                                          "type": "record",
                                          "name": "baz",
                                          "fields": [
                                              {
                                                  "name": "quux",
                                                  "type": "int"
                                              }
                                          ]
                                      }
                                  }
                              }
                          ]
                      }
                  }"#;

    Notice:

     "type": {
                                      "type": "array",
                                      "items": {
    

    I will debug why the reported schema leads to problems. Its items is not recognized and it is stored as custom_attributes.

  5. added a commit that references this issue on Sep 30, 2025
    93622a5
  6. added this to the 0.21.0 milestone on Sep 30, 2025
  7. added a commit that references this issue on Sep 30, 2025
    18ee808
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions