-
Notifications
You must be signed in to change notification settings - Fork 38
Description
Since the supposedly bugfix #66 in avro-php 4.3.0 the handling of unions with complex types does not work correctly any more: No matter which of the union members is provided, always the first member is written (with value NULL).
For illustration please see the attached file containing tests for a sample schema.
UnionsWithoutDefaultsTest.txt
In my opinion, there are multiple aspects causing this problem:
- The function
default_value()returns NULL if no default value exists for a field. - The function
is_valid_datumin schema.php replacesfield->name()byfield->default_value()and thus gets a NULL, if no default value has been defined. Therefore, always the first union member is evaluated as "valid datum". - The same happens in function
write_recordin datum.php: Always the first union member is written with (supposed) default NULL.
I think a solution would include the following fixes:
=> A record that is missing an expected field must not pass as a valid record if the field is not nullable.
=> If an expected field is missing, this field must not be written at all (instead of writing it with value NULL). If the field is not nullable, this must raise an error.
BTW according to Avro spec, default values are not to be used when writing. They only provide a means for the reader to replace missing fields by default values, see https://avro.apache.org/docs/1.10.2/spec.html:
default: A default value for this field, only used when reading instances that lack the field for schema evolution purposes. The presence of a default value does not make the field optional at encoding time.