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

Dean Ericson commented on AVRO-1604:
------------------------------------

I just built the 1.8.0 SNAPSHOT for testing purposes and I am finding a strange 
error message which might be a result of the @Nullable annotation.

The following code always produces: 

{code}
org.apache.avro.AvroTypeException: Unknown union branch hour
        at org.apache.avro.io.JsonDecoder.readIndex(JsonDecoder.java:445)
        at 
org.apache.avro.io.ResolvingDecoder.doAction(ResolvingDecoder.java:290)
        at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
        at 
org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:267)
        at 
org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:178)
        at 
org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:152)
        at 
org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:240)
        at 
org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:230)
        at 
org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:174)
        at 
org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:152)
        at 
org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:144)
        at avro.TestAvro.jsonToAvro(TestAvro.java:182)
        at avro.TestAvro.testAvroJsonToBinarySerialization(TestAvro.java:97)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at 
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at 
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at 
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
        at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
        at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
{code}


{code}
     @Test
     public void testAvroJsonToBinarySerialization() throws Exception {
             String json = "{\"cost\": 0, \"stamp\": {\"hour\": 12, \"second\": 
0}}";
             Schema schema = new Schema.Parser().parse(schema.toString());

             byte[] avroByteArray = jsonToAvro(json,schema);
              ...
    }
    public static byte[] jsonToAvro(String json, Schema schema) throws 
IOException {
        DatumReader<Object> reader = new GenericDatumReader<>(schema);
        GenericDatumWriter<Object> writer = new GenericDatumWriter<>(schema);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Decoder decoder = DecoderFactory.get().jsonDecoder(schema, json);
        Encoder encoder = EncoderFactory.get().binaryEncoder(output, null);
        Object datum = reader.read(null, decoder);
        writer.write(datum, encoder);
        encoder.flush();
        return output.toByteArray();
    }
{code}

My **generated** schema looks like:
{code}
{"type":"record","name":"Packet","namespace":"avro","fields":[{"name":"cost","type":"int"},{"name":"stamp","type":["null",{"type":"record","name":"TimeStamp","namespace":"avro.Packet$","fields":[{"name":"hour","type":"int"},{"name":"second","type":"int"}]}],"default":null}]}
{code}

I was hoping that building the 1.8 snapshot would fix the issue dealing with 
the @nullable annotation

Am I using the API wrong?  What would be causing this issue?

> ReflectData.AllowNull fails to generate schemas when @Nullable is present.
> --------------------------------------------------------------------------
>
>                 Key: AVRO-1604
>                 URL: https://issues.apache.org/jira/browse/AVRO-1604
>             Project: Avro
>          Issue Type: Bug
>            Reporter: Ryan Blue
>            Assignee: Ryan Blue
>             Fix For: 1.8.0
>
>         Attachments: AVRO-1604-1.patch
>
>
> The {{@Nullable}} annotation causes a failure because the 
> {{makeNullable(Schema)}} method assumes that the type passed in is not 
> already a union. When it is a union, either from using {{Nullable}}, 
> {{AvroSchema}}, or a {{Union}}-annotated type, an exception is thrown that 
> unions can't contain unions.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to