Skip to content

feat: Allow types to provide default values - #477

Merged
Kriskras99 merged 10 commits into
mainfrom
feat/avro_schema_default
Feb 25, 2026
Merged

Kriskras99 merged 10 commits into
mainfrom
feat/avro_schema_default

Conversation

@Kriskras99

Copy link
Copy Markdown
Contributor

AvroSchemaComponent is extended with a function field_default which will be called when deriving a record to set the default value for a field. The default implementation is to return None, which means no default.

On the derive side, it is now possible to specify a default for a type using #[avro(default = "..")]. It is also possible to disable setting a default for a field with #[avro(default = false)].

This enables users to use #[serde(skip_serializing{_if})] on most fields without having to provide a default value.

Closes #434

`AvroSchemaComponent` is extended with a function `field_default`
which will be called when deriving a record to set the default value
for a field. The default implementation is to return `None`, which
means no default.

On the derive side, it is now possible to specify a default for a type
using `#[avro(default = "..")]`. It is also possible to disable setting
a default for a field with `#[avro(default = false)]`.

This enables users to use `#[serde(skip_serializing{_if})]` on most fields
without having to provide a default value.
@martin-g martin-g added this to the 0.22.0 milestone Feb 20, 2026

@martin-g martin-g left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO FieldDefault::Disabled should be the default.

By default AvroSchema shouldn't add default to the generated Schema.
Only when the user application sets one (via Value(e.g. #[avro(default = XYZ)]) or via Trait (returning Some from field_default())) it should be added.

Comment thread avro/src/serde/derive.rs Resolved
Comment thread avro/src/serde/derive.rs Resolved
Comment thread avro_derive/src/attributes/mod.rs Resolved
Comment thread avro/src/serde/derive.rs Resolved
Comment thread avro/src/serde/derive.rs Resolved
Comment thread avro_derive/src/attributes/mod.rs Resolved
Comment thread avro_derive/src/attributes/mod.rs Resolved
Comment thread avro_derive/src/attributes/mod.rs Resolved
Comment thread avro_derive/tests/derive.rs Resolved
Comment thread avro/src/serde/derive.rs Resolved

@Kriskras99 Kriskras99 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My goal for this PR is to allow users to have defaults for fields with as little effort as possible.
That's why I implemented AvroSchemaComponent::field_default() as Some(Default::default()) for all types we implement.
I can understand if some users don't want to have a default for a field, which is why I added #[avro(default = false)] to disable getting a default for that field. This allows for usecases like in #434

Comment thread avro_derive/tests/derive.rs Resolved
Comment thread avro_derive/src/attributes/mod.rs Resolved
Comment thread avro/src/serde/derive.rs Resolved
Comment thread avro/src/serde/derive.rs Resolved
Comment thread avro_derive/src/attributes/mod.rs Resolved
@Kriskras99
Kriskras99 force-pushed the feat/avro_schema_default branch from 810ebb3 to 40a4506 Compare February 20, 2026 14:58
Comment thread avro/src/serde/derive.rs Resolved
@Kriskras99

Copy link
Copy Markdown
Contributor Author

I've removed the field_default implementation for all types except Option<T>. It's easier to add new implementations later than removing existing implementations.

@Kriskras99
Kriskras99 requested a review from martin-g February 23, 2026 19:37
@Kriskras99
Kriskras99 force-pushed the feat/avro_schema_default branch from 5b306d5 to 3915546 Compare February 23, 2026 22:14
@martin-g

Copy link
Copy Markdown
Member

@Kriskras99 Please check my commits

@Kriskras99

Copy link
Copy Markdown
Contributor Author

Looks good!

@Kriskras99
Kriskras99 merged commit 62ad6dd into main Feb 25, 2026
21 checks passed
@Kriskras99
Kriskras99 deleted the feat/avro_schema_default branch February 25, 2026 10:17
@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.

support for serde_as: skip_serializing_none

2 participants