-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Record serialization is sensitive to order of fields in struct #47
Comments
Hmm good point, thanks for raising that. I feel like it should still be an error, as for these two different schemas:
and
Avro will encode them differently, and schema resolution will not succeed if writing from one and reading from the other. 🙂 Another limitation is that we currently do not rely on the schema to transform a |
Yeah, I understand the point about two differing schema, but I think it may be confusing for people coming from the Rust community where I don't think order of fields on a struct ever matters. I don't know much about other implementations of Serde serializers, but if, for example, many other implementations where able to handle this case, it might be a sticking point. Either way, I don't think its critical, once you understand what's going on, it's easy to handle. Maybe a note in the documentation somewhere? |
Avro fields are resolved by name not order, so reordering field order is completely legal according to the Schema Resolution rules. I haven't looked into EDIT: Also here are their parsing and resolution docs https://avro.apache.org/docs/1.8.1/api/java/org/apache/avro/io/parsing/doc-files/parsing.html |
I just ran into this - was pretty confusing, if the plan is to stick this - it would be helpful if it was mentioned somewhere. |
I've changed the record validation loop to this just to remove the ordering constraint, although this implementation isn't the cleanest. This issue makes it difficult to enforce the presence of fields with a common use std::collections::HashMap;
if fields.len() == record_fields.len() {
let data_fields: HashMap<String, Value> = record_fields.iter().cloned().collect();
fields
.iter()
.try_for_each(|field| {
if let Some(value) = data_fields.get(&field.name) {
validate(value, &field.schema)
} else {
Err(Error::from(format!(
"Expected field named {} but could not find it",
field.name
)))
}
})
// … |
It appears that this is also a problem when you want to use |
A PR with a fix at apache/avro#1650 |
Looks like apache/avro#1650 was merged - should this be closed? |
I'm not sure this is not intended behavior, but I just got tripped up by this so I thought I'd point it out. If you declare a schema like this:
and then try to de/serialize it into a rust struct of this type:
You will get an error, "value does not match schema". I'm not sure that should be an error, seems like there's an obvious way to translate from one to another.
The operation that is erroring for me is:
to_avro_datum(&record_scheme, to_value(record)?)?
The text was updated successfully, but these errors were encountered: