You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
apache-avro-derive: how to handle bytes data? #350
When reading avro bytes from an avro to a struct, it's pretty easy to get that into some data type that rust understand (like Vec<u8>, using apache_avro::serde_avro_bytes_opt). But how would I go about making this all work with apache avro derive, to auto generate schemas where fields should of of type bytes? What I mean by that is how should I create a struct so that a schema generated from it can use the bytes dtype: what should be the dtype in the struct itself? what macros do I need if any?
I wonder if there is a more elegant solution, but I ended up writing the following:
// Wrapper around cutout bytes, so we can implement AvroSchemaComponent// for it, to serialize as bytes in Avro#[derive(Debug, serde::Deserialize, serde::Serialize)]pubstructCutoutBytes(Vec<u8>);impl apache_avro::schema::derive::AvroSchemaComponentforCutoutBytes{fnget_schema_in_ctxt(_named_schemas:&mutHashMap<apache_avro::schema::Name, apache_avro::schema::Schema>,_enclosing_namespace:&apache_avro::schema::Namespace,) -> apache_avro::Schema{
apache_avro::Schema::Bytes}}pubfnserialize_cutout_bytes_option<S>(cutout:&Option<CutoutBytes>,serializer:S) -> Result<S::Ok,S::Error>whereS: serde::ser::Serializer,{match cutout {Some(cutout_bytes) => apache_avro::serde_avro_bytes::serialize(&cutout_bytes.0, serializer),None => serializer.serialize_none(),}}
then a struct would just use CutoutBytes in place of a Vec + the serializer defined above, and the schema uses the bytes type + the data is serialized to bytes.
If you have existing schema that you're trying to match, I would suggest using rsgen-avro. It allows you to overwrite types with newtypes.
If you do not have existing schema, why not just use whatever #[derive(AvroSchema)] generates? It will generate a new Record for CutoutBytes with only one field, but you won't be able to tell the difference after serialisation.
If these options don't work for you, I think the solution you have is the best option.
Sorry perhaps I didn't explain my usecase well enough. The data in my structs (let's call it image_data) is just made of bytes. In my struct that happens to be Vec but I could make it into anything. What I want is the auto-generated schema from apache-avro-derive to create a schema where image_data is of type "bytes". Right now, I see no pathway to apache-avro-derive creating a schema with any avro type bytes. What I am trying to understand is what would be the recommended way to do that, i.e. what should be the rust dtype of the data in my struct for the auto generated avro schema to have image_data of type bytes.
rs-gen avro is made to generate structs from avro schemas, but I'm trying to do the exact opposite: use apacher-avro-derive to generate a schema from a struct (and then serialize to avro using that schema and struct), but I don't see a way to get to a bytes avro dtype.
Looking at the derive code, it's not possible to get a bytes schema for a field. This is because we implement AvroSchemaComponent for Vec<T> as an array of T. Because Rust doesn't support specialisation (yet), we can't have a separate implementation for Vec<u8>.
Thus, the only way to get a bytes is using a new type and manually implementing AvroSchemaComponent and Serialize/Deserialize, which is what you did. So unfortunately I don't have a better solution for you.
Noted, thanks. I just wanted to make sure that I didn't miss anything obvious. The new type is a bit cumbersome but it "just works", which is all one really cares about!
Both solutions are fine by me! The attribute/macro might be a little easier to implement for the user, as they don't even need to change pieces of code that use the associated struct (A custom type might break other things)
Activity
Theodlz commentedon Dec 4, 2025
I wonder if there is a more elegant solution, but I ended up writing the following:
then a struct would just use
CutoutBytesin place of a Vec + the serializer defined above, and the schema uses thebytestype + the data is serialized to bytes.Theodlz commentedon Dec 4, 2025
(sorry for the domain-specific name of
CutoutBytes, but that could be anything else obviously, likeBytesDataKriskras99 commentedon Dec 4, 2025
If you have existing schema that you're trying to match, I would suggest using rsgen-avro. It allows you to overwrite types with newtypes.
If you do not have existing schema, why not just use whatever
#[derive(AvroSchema)]generates? It will generate a newRecordforCutoutByteswith only one field, but you won't be able to tell the difference after serialisation.If these options don't work for you, I think the solution you have is the best option.
Theodlz commentedon Dec 4, 2025
Sorry perhaps I didn't explain my usecase well enough. The data in my structs (let's call it
image_data) is just made of bytes. In my struct that happens to be Vec but I could make it into anything. What I want is the auto-generated schema from apache-avro-derive to create a schema where image_data is of type "bytes". Right now, I see no pathway to apache-avro-derive creating a schema with any avro typebytes. What I am trying to understand is what would be the recommended way to do that, i.e. what should be the rust dtype of the data in my struct for the auto generated avro schema to haveimage_dataof typebytes.Theodlz commentedon Dec 4, 2025
rs-gen avro is made to generate structs from avro schemas, but I'm trying to do the exact opposite: use apacher-avro-derive to generate a schema from a struct (and then serialize to avro using that schema and struct), but I don't see a way to get to a
bytesavro dtype.Kriskras99 commentedon Dec 4, 2025
Ah, I get the problem now.
Looking at the derive code, it's not possible to get a
bytesschema for a field. This is because we implementAvroSchemaComponentforVec<T>as an array ofT. Because Rust doesn't support specialisation (yet), we can't have a separate implementation forVec<u8>.Thus, the only way to get a
bytesis using a new type and manually implementingAvroSchemaComponentandSerialize/Deserialize, which is what you did. So unfortunately I don't have a better solution for you.Theodlz commentedon Dec 4, 2025
Noted, thanks. I just wanted to make sure that I didn't miss anything obvious. The new type is a bit cumbersome but it "just works", which is all one really cares about!
Kriskras99 commentedon Dec 5, 2025
I thkn there are two ways to improve the situation:
Vecu8?)bytes) (needs to check that the right serde attributes are set)Theodlz commentedon Dec 8, 2025
Hi all,
Both solutions are fine by me! The attribute/macro might be a little easier to implement for the user, as they don't even need to change pieces of code that use the associated struct (A custom type might break other things)
Kriskras99 commentedon Dec 9, 2025
Thanks @Theodlz! I agree with your observation. I've added it to #361 and you can track progress there.
#[derive(AvroSchema)]improvements #361