Repository navigation
Closed
Description
AvroSchemaComponent looks like this:
pub trait AvroSchemaComponent {
fn get_schema_in_ctxt(named_schemas: &mut Names, enclosing_namespace: &Namespace) -> Schema;
}but looking at the code generated by #[derive(AvroSchema)] we only insert Schema::Refs into that map:
impl #impl_generics apache_avro::AvroSchemaComponent for #ident #ty_generics #where_clause {
fn get_schema_in_ctxt(named_schemas: &mut std::collections::HashMap<apache_avro::schema::Name, apache_avro::schema::Schema>, enclosing_namespace: &Option<String>) -> apache_avro::schema::Schema {
let name = apache_avro::schema::Name::new(#full_schema_name).expect(&format!("Unable to parse schema name {}", #full_schema_name)[..]).fully_qualified_name(enclosing_namespace);
let enclosing_namespace = &name.namespace;
if named_schemas.contains_key(&name) {
apache_avro::schema::Schema::Ref{name: name.clone()}
} else {
named_schemas.insert(name.clone(), apache_avro::schema::Schema::Ref{name: name.clone()});
#schema_def
}
}
}As the key is the name, we could also just use a HashSet instead:
impl #impl_generics apache_avro::AvroSchemaComponent for #ident #ty_generics #where_clause {
fn get_schema_in_ctxt(named_schemas: &mut std::collections::HashSet<apache_avro::schema::Name, enclosing_namespace: &Option<String>) -> apache_avro::schema::Schema {
let name = apache_avro::schema::Name::new(#full_schema_name).expect(&format!("Unable to parse schema name {}", #full_schema_name)[..]).fully_qualified_name(enclosing_namespace);
let enclosing_namespace = &name.namespace;
if named_schemas.get(&name).is_some() {
apache_avro::schema::Schema::Ref{name}
} else {
named_schemas.insert(name.clone());
#schema_def
}
}
}If we are supposed to put actual schemas in there, then we are generating the wrong code and should be generating something like this instead:
impl #impl_generics apache_avro::AvroSchemaComponent for #ident #ty_generics #where_clause {
fn get_schema_in_ctxt(named_schemas: &mut std::collections::HashMap<apache_avro::schema::Name, apache_avro::schema::Schema>, enclosing_namespace: &Option<String>) -> apache_avro::schema::Schema {
let name = apache_avro::schema::Name::new(#full_schema_name).expect(&format!("Unable to parse schema name {}", #full_schema_name)[..]).fully_qualified_name(enclosing_namespace);
let enclosing_namespace = &name.namespace;
if named_schemas.contains_key(&name) {
apache_avro::schema::Schema::Ref{name: name.clone()}
} else {
let schema = #schema_def
named_schemas.insert(name, schema.clone())
schema
}
}
}Metadata
Metadata
Assignees
Labels
No labels
Activity
martin-g commentedon Jan 13, 2026
@jklamer Do you want to join the workstream here as the original author ? :-)
I haven't checked the code but I think we have to change it to the last suggestion, i.e.
named_schemas.insert(name, schema.clone())Kriskras99 commentedon Jan 15, 2026
I've figured out why it is inserted before the schema definition is created. If a type is recursive:
and you don't insert the name into
named_schemas, then it will loop forever and eventually cause a stack overflow.I've 'fixed' it in the derive code (#397) by doing this:
I really think we should change it from a
HashMapto aHashSet:Schema::Refwhich you can get from the namenamed_schemasis discarded after the schema is generated#[derive(AvroSchema)]andAvroSchema::get_schemainstead of interacting withAvroSchemaComponent. Fixing the breakage is also really easy for users.martin-g commentedon Jan 16, 2026
I don't understand this.
What
let schema = #schema_def;does that requires the Ref entry innamed_schemas?Kriskras99 commentedon Jan 16, 2026
Sorry, that's my fault.
#schema_defis something from the avro_derive code. Here's a type expanded, so you can see what's going on:Note: it does not matter what is inserted into
named_schemasthe first time, instead of aSchema::Refit could also be aSchema::Null. The reason that I insert aSchema::Ref, is that user implementations ofAvroSchemaComponentmay depend on the type that is innamed_schemas.