Kriskras99 commented on issue #435:
URL: https://github.com/apache/avro-rs/issues/435#issuecomment-3796611261
The second problem happens at `ser_schema.rs:1455`:
```rust
fn serialize_unit_variant_with_schema(
&mut self,
name: &'static str,
variant_index: u32,
variant: &'static str,
schema: &Schema,
) -> Result<usize, Error> {
let create_error = |cause: String| {
Error::new(Details::SerializeValueWithSchema {
value_type: "unit variant",
value: format!("{name}::{variant} (index={variant_index}).
Cause: {cause}"),
schema: schema.clone(),
})
};
match schema {
Schema::Enum(enum_schema) => {
if variant_index as usize >= enum_schema.symbols.len() {
return Err(create_error(format!(
"Variant index out of bounds: {}. The Enum schema
has '{}' symbols",
variant_index,
enum_schema.symbols.len()
)));
}
encode_int(variant_index as i32, &mut self.writer)
}
Schema::Union(union_schema) => {
if variant_index as usize >= union_schema.schemas.len() {
return Err(create_error(format!(
"Variant index out of bounds: {}. The union schema
has '{}' schemas",
variant_index,
union_schema.schemas.len()
)));
}
encode_int(variant_index as i32, &mut self.writer)?;
// It goes wrong here
self.serialize_unit_struct_with_schema(
name,
&union_schema.schemas[variant_index as usize],
)
}
Schema::Ref { name: ref_name } => {
let ref_schema = self.get_ref_schema(ref_name)?;
self.serialize_unit_variant_with_schema(name, variant_index,
variant, ref_schema)
}
unsupported => Err(create_error(format!(
"Unsupported schema: {unsupported:?}. Expected: Enum, Union
or Ref"
))),
}
}
```
But the actual problem is caused by `serialize_some_with_schema`:
```rust
fn serialize_some_with_schema<T>(&mut self, value: &T, schema: &Schema)
-> Result<usize, Error>
where
T: ?Sized + ser::Serialize,
{
println!("serialize_some_with_schema: {schema}");
let mut inner_ser = SchemaAwareWriteSerializer::new(
&mut *self.writer,
schema,
self.names,
self.enclosing_namespace.clone(),
);
// This should peel the schema but doesn't
value.serialize(&mut inner_ser)
}
```
The root cause is that the serializer is too lenient. It allows serializing
an `Option<i64>` to a `long`, instead of only allowing serializing an
`Option<i64>` to an `["null", "long"]`.
@martin-g I think we should make `SchemaAwareStructSerializer` a lot more
strict in what it accepts, that would also help with supporting enums with
struct variants.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]