You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Ryan Skraba (Jira)" <ji...@apache.org> on 2020/06/08 16:12:00 UTC

[jira] [Updated] (AVRO-1664) PHP library can't serialise records with optional (union-null) values

     [ https://issues.apache.org/jira/browse/AVRO-1664?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Ryan Skraba updated AVRO-1664:
------------------------------
    Fix Version/s: 1.10.0

> PHP library can't serialise records with optional (union-null) values
> ---------------------------------------------------------------------
>
>                 Key: AVRO-1664
>                 URL: https://issues.apache.org/jira/browse/AVRO-1664
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: php
>    Affects Versions: 1.7.7
>         Environment: php 5.5.15 OS X 10.9 
>            Reporter: Paul Banks
>            Assignee: Siad Ardroumli
>            Priority: Major
>             Fix For: 1.10.0
>
>
> PHP avro serialising doesn't appear to support "optional" fields in records.
> Consider the PHP script below:
> {code}
> <?php
> require_once('lib/avro.php');
> $schema_json = <<<_JSON
> {"name":"member",
>  "type":"record",
>  "fields":[{"name":"one", "type":"int"},
>            {"name":"two", "type":["null", "string"]}
>            ]}
> _JSON;
> $schema = AvroSchema::parse($schema_json);
> // Our datum is missing the 'optional' field (i.e. it's null)
> $datum = array("one" => 1);
> $io = new AvroStringIO();
> $writer = new AvroIODatumWriter($schema);
> $encoder = new AvroIOBinaryEncoder($io);
> $writer->write($datum, $encoder);
> $bin = $io->string();
> echo bin2hex($bin) . "\n";
> {code}
> My understanding from documentation is that this should work and output the encoded binary in hex.
> Instead it throws:
> {code}
> PHP Fatal error:  Uncaught exception 'AvroIOTypeException' with message 'The datum array (
>   'one' => 1,
> ) is not an example of schema {"type":"record","name":"member","fields":[{"name":"one","type":"int"},{"name":"two","type":["null","string"]}]}'
> {code}
> It's possible that this is not a valid usage of Avro and I'm mistaken in my expectations, so I tried the python library as a comparison. Sure enough the following script works as expected:
> {code}
> from avro import schema
> from avro import io
> from StringIO import StringIO
> s = schema.parse("""
>     {"name":"member",
>     "type":"record",
>     "fields":[{"name":"one", "type":"int"},
>               {"name":"two", "type":["null", "string"]}
>              ]}""")
> writer = StringIO()
> encoder = io.BinaryEncoder(writer)
> datum_writer = io.DatumWriter(s)
> datum_writer.write({"one": 1}, encoder)
> print writer.getvalue().encode("hex")
> {code}
> which outputs:
> {code}
> $ python avro_test.py
> 0200
> {code}
> As expected.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)