Hi Martin,

On Mon, Aug 29, 2022 at 1:43 PM Martin Grigorov <[email protected]> wrote:

[...]

> I'd recommend you this nice tool for generating Rust structs from Avro
> schema: https://github.com/lerouxrgd/rsgen-avro

Thanks for the reply and the pointer to `rsgen-avro`. :-)

It looks like there seems to be an issue in the way Serde is serializing
the schema.

Here is the example:

```main.rs
use apache_avro::types::{Record, Value};
use apache_avro::Schema;

#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct Abcd {
    pub b: Option<Vec<u8>>,
}

#[inline(always)]
fn default_abcd_b() -> Option<Vec<u8>> {
    None
}

impl Default for Abcd {
    fn default() -> Abcd {
        Abcd {
            b: default_abcd_b(),
        }
    }
}

fn main() {
    let writers_schema = Schema::parse_str(
        r#"
{
  "type": "record",
  "name": "Abcd",
  "fields": [
    {"name": "b", "type": ["null", "bytes"], "default": null}
  ]
}
"#,
    )
    .unwrap();

    let mut abcd_manual = Record::new(&writers_schema).unwrap();
    abcd_manual.put(
        "b",
        Value::Union(1,
Box::new(Value::Bytes("hello_world".as_bytes().to_vec()))),
    );

    println!("{:?}", abcd_manual);

    let abcd_manual_bytes =
apache_avro::to_avro_datum(&writers_schema, abcd_manual).unwrap();
    println!("{:?}", abcd_manual_bytes);

    let abcd_serde_value = apache_avro::to_value(Abcd {
b: Some("hello_world".as_bytes().to_vec()),
    }).unwrap();

    println!("{:?}", abcd_serde_value);

    let abcd_serde_bytes = apache_avro::to_avro_datum(&writers_schema,
abcd_serde_value);

    println!("{:?}", abcd_serde_bytes);
}
```

Rather than creating an Avro Value of the form,

```
Record { fields: [("b", Union(1, Bytes([104, 101, 108, 108, 111, 95,
119, 111, 114, 108, 100])))], schema_lookup: {"b": 0} }
```

Serde seems to be generating an Avro Value,

```
Record([("b", Union(1, Array([Int(104), Int(101), Int(108), Int(108),
Int(111), Int(95), Int(119), Int(111), Int(114), Int(108),
Int(100)])))])
```

which is causing the subsequent conversion to bytes to fail.

I was wondering if this is a known issue?

Best,
Rajiv

Reply via email to