Kriskras99 commented on issue #400:
URL: https://github.com/apache/avro-rs/issues/400#issuecomment-3755254193
I've figured out why it is inserted _before_ the schema definition is
created. If a type is recursive:
```rust
struct ConsList {
value: i32,
next: Option<Box<ConsList>>,
}
```
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 by doing this:
```rust
let name =
apache_avro::schema::Name::new(#full_schema_name).expect(concat!("Unable to
parse schema name ",
#full_schema_name)).fully_qualified_name(enclosing_namespace);
if named_schemas.contains_key(&name) {
apache_avro::schema::Schema::Ref{name}
} else {
let enclosing_namespace = &name.namespace;
// This is needed because otherwise recursive types will recurse forever
and cause a stack overflow
// TODO: Breaking change to AvroSchemaComponent, have named_schemas be a
set instead
named_schemas.insert(name.clone(),
apache_avro::schema::Schema::Ref{name: name.clone()});
let schema = #schema_def;
named_schemas.insert(name, schema.clone());
schema
}
```
I really think we should change it from a `HashMap` to a `HashSet`:
1. The old behaviour is useless, it just inserts `Schema::Ref` which you can
get from the name
2. The new behavior is wastefull, as in 99% of usecases `named_schemas` is
discarded after the schema is generated
3. It won't break that much code, as most users will be using the
`#[derive(AvroSchema)]` and `AvroSchema::get_schema` instead of interacting
with `AvroSchemaComponent`. Fixing the breakage is also really easy for users.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]