Repository navigation
Closed
Milestone
Description
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
martin-g commentedon Sep 20, 2025
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"...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 ?
vkopio commentedon Sep 22, 2025
I tested this only using
0.20.0. I mentioned the version for the sake of reproducibility, but should have been more clear.martin-g commentedon Sep 30, 2025
The following schema works fine:
Notice:
I will debug why the reported schema leads to problems. Its
itemsis not recognized and it is stored as custom_attributes.fix: Do not store "items" for Schema::Array and "values" for Schema::…
fix: Do not store "items" for Schema::Array and "values" for Schema::…