Skip to content

feat(derive): allow overriding the schema for a field and change the schema for uuid::Uuid - #397

Merged
martin-g merged 7 commits into
mainfrom
feat/derive_with
Jan 16, 2026
Merged

martin-g merged 7 commits into
mainfrom
feat/derive_with

Conversation

@Kriskras99

Copy link
Copy Markdown
Contributor

A user can now override the schema used by #[derive(AvroSchema)] using #[avro(with)].

If no argument is supplied to with, then it will use the same module as specified in #[serde(with = ".."]. It is a compile error if the Serde attribute is missing.

If an argument is supplied to with, it will call this function with the same arguments as AvroSchemaComponent::get_schema_in_ctxt.

I've also replaced the AvroSchemaComponent implementation for uuid::Uuid with the Schema::Uuid(UuidSchema::Fixed) representation. Users who want the old schema can use #[avro(with)] to revert back.

User-facing documentation will follow in a separate PR, which also contains an overhaul of the entire Serde/derive documentation of these crates.

@Kriskras99

Copy link
Copy Markdown
Contributor Author

Comment thread avro_derive/src/attributes/avro.rs Resolved
Comment thread avro_derive/src/attributes/avro.rs Resolved
Comment thread avro_derive/src/attributes/avro.rs Resolved
Comment thread avro_derive/src/attributes/avro.rs Resolved
Comment thread avro_derive/src/attributes/mod.rs Resolved
Comment thread avro/src/schema.rs Resolved
Comment thread avro_derive/src/attributes/avro.rs Resolved
Comment thread avro_derive/src/attributes/avro.rs Resolved
Comment thread avro_derive/src/attributes/mod.rs Resolved
Comment thread avro_derive/src/attributes/mod.rs Resolved
@martin-g

Copy link
Copy Markdown
Member

For the with attribute the argument has no quotes, matching Bon (https://bon-rs.com/reference/builder/member/with#with). Serde always has quotes (https://serde.rs/field-attrs.html#serialize_with). Which one do we prefer?

I prefer the no-quotes one. It looks cleaner and more type safe to me. Although the quoted one in Serde is equally type safe, I guess.

@Kriskras99
Kriskras99 force-pushed the feat/derive_with branch 2 times, most recently from 8028f15 to 3ee13c8 Compare January 13, 2026 17:37
@Kriskras99

Copy link
Copy Markdown
Contributor Author

Accepting a closure seems to be quite difficult, looking at the Bon code. Let's not do that for now

Comment thread avro_derive/src/attributes/mod.rs Resolved
Comment thread avro/src/bytes.rs Resolved
@martin-g

martin-g commented Jan 14, 2026 •

Copy link
Copy Markdown
Member

Please add a test for deriving a Schema for:

#[derive(AvroSchema)]
struct Foo {
  #[serde(rename="baz")]
  bar: uuid::Uuid
}

Comment thread avro_derive/src/lib.rs Outdated
With::Serde(path) => {
quote! {#path::get_schema_in_ctxt(named_schemas, enclosing_namespace)}
}
With::Expr(expr) => quote! {#expr(named_schemas, enclosing_namespace)},

@martin-g martin-g Jan 16, 2026 •

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.

Is there anything else that mandates the expr to be a function ?
I don't see any restrictions in avro.rs and mod.rs to verify that the provided expression is a syn::Expr::Call.

My main question is: What leads to expected function in errors like error[E0618]: expected function, found apache_avro::Schema ?

I am experimenting with

                         quote! {#path::get_schema_in_ctxt(named_schemas, enclosing_namespace)}
                     }
-                    With::Expr(expr) => quote! {#expr(named_schemas, enclosing_namespace)},
+                    With::Expr(expr) => match expr {
+                        Expr::Closure(closure) => {
+                            dbg!("==========");
+                            quote! {#closure()}
+                        }
+                        _ => quote! {#expr(named_schemas, enclosing_namespace)},
+                    },
                 };

but it seems something earlier makes the check for a function.

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.

Actually the dbg!() code is executed ! I'm continuing my debug session

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.

-                    With::Expr(expr) => quote! {#expr(named_schemas, enclosing_namespace)},
+                    With::Expr(expr) => match expr {
+                        Expr::Closure(closure) => {
+                            let inputs = &closure.inputs;
+                            let body = &closure.body;
+                            dbg!("==========", &closure);
+                            dbg!(">>>>>", quote! {#body});
+                            quote! {#body}
+                        }
+                        _ => quote! {#expr(named_schemas, enclosing_namespace)},
+                    },
                 };

This seems to work for || Schema::Bytes !

I guess we could make it work also for |named_schemas, enclosing_namespace| Schema::Bytes

@martin-g martin-g Jan 16, 2026 •

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.

I have pushed the support for lambda/closure.
I think we should not support inputs for now.

How about supporting also #[avro(with = Schema::Bytes)] too ? Seems reasonable to me.
Using the lambda should be good enough!

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.

I don't think it's possible to distinguish between a function path path::to::function and a constructor Schema::Bytes.
Nice work adding the lambda!

@Kriskras99

Copy link
Copy Markdown
Contributor Author

@martin-g I've added validation that the closure has no inputs, and added an explicit error if the expression is not a function or a closure.
If you think it's good, it's ready to merge!

@martin-g
martin-g merged commit 283c4b1 into main Jan 16, 2026
@Kriskras99
Kriskras99 deleted the feat/derive_with branch January 16, 2026 15:38
@martin-g martin-g added this to the 0.22.0 milestone Jan 19, 2026
@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