Skip to content

apache-avro-derive: how to handle bytes data? #350

Closed as duplicate
Closed as duplicate

Description

@Theodlz

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?

Activity

  1. Theodlz commented on Dec 4, 2025

    @Theodlz
    Author

    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)]
    pub struct CutoutBytes(Vec<u8>);
    
    impl apache_avro::schema::derive::AvroSchemaComponent for CutoutBytes {
        fn get_schema_in_ctxt(
            _named_schemas: &mut HashMap<apache_avro::schema::Name, apache_avro::schema::Schema>,
            _enclosing_namespace: &apache_avro::schema::Namespace,
        ) -> apache_avro::Schema {
            apache_avro::Schema::Bytes
        }
    }
    
    pub fn serialize_cutout_bytes_option<S>(cutout: &Option<CutoutBytes>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: 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.

  2. Theodlz commented on Dec 4, 2025

    @Theodlz
    Author

    (sorry for the domain-specific name of CutoutBytes, but that could be anything else obviously, like BytesData

  3. Kriskras99 commented on Dec 4, 2025

    @Kriskras99
    Contributor

    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.

  4. Theodlz commented on Dec 4, 2025

    @Theodlz
    Author

    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.

  5. Theodlz commented on Dec 4, 2025

    @Theodlz
    Author

    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.

  6. Kriskras99 commented on Dec 4, 2025

    @Kriskras99
    Contributor

    Ah, I get the problem now.

    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.

  7. Theodlz commented on Dec 4, 2025

    @Theodlz
    Author

    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!

  8. Kriskras99 commented on Dec 5, 2025

    @Kriskras99
    Contributor

    I thkn there are two ways to improve the situation:

    1. Provide a newtype, that implements what you implemented (naming is a bit of an issue, maybe Vecu8?)
    2. Provide a field attribute that sets the avro type to bytes) (needs to check that the right serde attributes are set)
  9. Theodlz commented on Dec 8, 2025

    @Theodlz
    Author

    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)

  10. Kriskras99 commented on Dec 9, 2025

    @Kriskras99
    Contributor

    Thanks @Theodlz! I agree with your observation. I've added it to #361 and you can track progress there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions