Skip to content

fix: flatten no longer causes duplicate names - #448

Merged
Kriskras99 merged 7 commits into
mainfrom
fix/avro_schema_flatten
Jan 30, 2026
Merged

Kriskras99 merged 7 commits into
mainfrom
fix/avro_schema_flatten

Conversation

@Kriskras99

@Kriskras99 Kriskras99 commented Jan 28, 2026 •

Copy link
Copy Markdown
Contributor

When a type was used both via flatten and directly, the schema generated would contain duplicate names (and schemas). This is because flatten would use an empty named_schemas to get the record schema. If the existing named_schemas was used, flatten might get a Schema::Ref if the type was already used. Or when flatten was used first, if the type was used after it would create a Schema::Ref to a schema that does not exist.

This is solved by adding a new function to the AvroSchemaComponent that returns the fields directly. To not break code currently implementing this trait, it has a default implementation that will work around the issues above. This default implementation is also used for fields with the #[avro(with = ||)] and #[avro(with = path)] attributes, as they don't have a way to provide the field directly. Users of #[avro(with)] will need to implement get_record_fields_in_ctxt in their module.

TODO:

  • Create tests for the logic of the default get_record_fields_in_ctxt implementation
  • Test interactions with transparent
  • Test interactions with with
  • Update the documentation in the branch feat/documentation

Fixes one of the issues raised in #435

When a type was used both via `flatten` and directly, the schema generated would
contain duplicate names (and schemas). This is because `flatten` would use an empty
`named_schemas` to get the record schema. If the existing `named_schemas` was used,
`flatten` might get a `Schema::Ref` if the type was already used. Or when `flatten`
was used first, if the type was used after it would create a `Schema::Ref` to a schema
that does not exist.

This is solved by adding a new function to the `AvroSchemaComponent` that returns
the fields directly. To not break code currently implementing this trait, it has
a default implementation that will work around the issues above. This default
implementation is also used for fields with the `#[avro(with = ||)]` and `#[avro(with = path)]`
attributes, as they don't have a way to provide the field directly. Users of `#[avro(with)]`
will need to implement `get_record_fields_in_ctxt` in their module.
@Kriskras99
Kriskras99 marked this pull request as draft January 28, 2026 22:03
@Kriskras99

Copy link
Copy Markdown
Contributor Author

@martin-g this still needs some additional tests, but do you agree with the general direction/implementation of this PR?

…hemaComponent`

The implementation for `serde_json::Map<String, T>` has been removed. This is **not** a breaking change, as `serde_json::Map` can only be constructed for `<String, Value>` and `Value` does not implement `AvroSchemaComponent`.
@Kriskras99
Kriskras99 marked this pull request as ready for review January 29, 2026 16:01
@Kriskras99
Kriskras99 requested a review from martin-g January 29, 2026 16:02
Comment thread avro/src/serde/derive.rs Resolved
Comment thread avro/tests/get_record_fields.rs Resolved
Comment thread avro/tests/get_record_fields.rs Resolved
Comment thread avro_derive/src/lib.rs Resolved
Comment thread avro_derive/src/lib.rs Resolved
Comment thread avro_derive/src/lib.rs Resolved
Comment thread avro/src/serde/derive.rs Resolved
Comment thread avro/src/serde/derive.rs Resolved
Comment thread avro/src/serde/with.rs Resolved
Comment thread avro_derive/src/lib.rs Resolved
@Kriskras99
Kriskras99 merged commit 7cff925 into main Jan 30, 2026
@martin-g martin-g added this to the 0.22.0 milestone Feb 18, 2026
@Kriskras99
Kriskras99 deleted the fix/avro_schema_flatten branch March 26, 2026 19:09
@Kriskras99 Kriskras99 mentioned this pull request Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants