You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Scott Carey (Commented) (JIRA)" <ji...@apache.org> on 2012/01/19 03:00:41 UTC

[jira] [Commented] (AVRO-997) Union of enum and null cannot be serialized

    [ https://issues.apache.org/jira/browse/AVRO-997?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13188873#comment-13188873 ] 

Scott Carey commented on AVRO-997:
----------------------------------

The following works in Avro 1.6.1, not sure about Avro 1.5.1 (it would need to be modified since it uses Schema.Parser)
This does not test an object created by the Specific Record API.

{code}
  @Test
  public void testWriteNullableEnum() throws IOException {
    Schema.Parser p = new Schema.Parser();
    Schema genderSchema = p.parse(
        "{"
        + "\"type\": \"enum\","
        + "\"name\": \"Gender\","
        + "\"symbols\": [\"M\", \"F\"]"
        +"}");
    Schema fooSchema = p.parse(
        "{"
        + "\"type\" : \"record\","
        + "\"name\" : \"Foo\","
        + "\"fields\" : ["
        + "{\"type\" : [\"Gender\", \"null\"], \"name\" : \"gender\" }"
        +"]}");
    
    GenericData.Record foo = new GenericData.Record(fooSchema);
    
    foo.put(0, new GenericData.EnumSymbol(genderSchema, "M"));
    
    GenericDatumWriter<GenericData.Record> w = new GenericDatumWriter<GenericData.Record>(fooSchema);
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Encoder e = EncoderFactory.get().binaryEncoder(baos, null);
    
    w.write(foo, e);
    
  }
{code}


Note that with a SpecificRecord, the enum must be a GenericData.EnumSymbol, it does not currently support a Java enum.
                
> Union of enum and null cannot be serialized
> -------------------------------------------
>
>                 Key: AVRO-997
>                 URL: https://issues.apache.org/jira/browse/AVRO-997
>             Project: Avro
>          Issue Type: Bug
>    Affects Versions: 1.5.1
>            Reporter: Aaron Kimball
>
> I have a schema like:
> {code}
> [
> {
>   "type": "enum",
>   "name": "Gender",
>   "symbols": ["M", "F"]
> },
> {
>   "type" : "record",
>   "name" : "Foo",
>   "fields" : [
>     { "type" : ["Gender", "null"], "name" : "gender" },
>     ...
>   ]
> }
> ]
> {code}
> I build a record like {{Foo foo = new Foo(); foo.gender = Gender.M;}}
> When I go to serialize this, I get:
> {code}Not in union [{"type":"enum","name":"Gender","symbols":["M","F"]},"null"]: M
> 	at org.apache.avro.generic.GenericData.resolveUnion(GenericData.java:482)
> 	at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:70)
> 	at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:104)
> 	at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:65)
> 	at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:57)
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira