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

Christophe Le Saec commented on AVRO-2378:
------------------------------------------

Is it still the case, as unit test :
 
{code:java}
  public static class ArrayByte {
    private byte[] array;

    public byte[] getArray() {
      return array;
    }

    public void setArray(final byte[] array) {
      this.array = array;
    }
  }

  @Test
  void testArray() throws IOException {

    Schema schema = ReflectData.AllowNull.get().getSchema(ArrayByte.class);
    Assertions.assertNotNull(schema);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ArrayByte aa = new ArrayByte();
    aa.setArray("Hello".getBytes(UTF_8));
    ReflectDatumWriter<ArrayByte> writer = new ReflectDatumWriter<>(schema);
    JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, baos);
    writer.write(aa, encoder);
    encoder.flush();
    System.out.println(baos.toString());
  }
{code}
work fine add show 

{noformat}
{"array":{"bytes":"Hello"}}
{noformat}


 

> byte[] is not supported by Reflect data when specified in union as "type": 
> "null", "bytes"
> ------------------------------------------------------------------------------------------
>
>                 Key: AVRO-2378
>                 URL: https://issues.apache.org/jira/browse/AVRO-2378
>             Project: Apache Avro
>          Issue Type: Bug
>    Affects Versions: 1.8.2
>            Reporter: RAJAT SOMANI
>            Priority: Major
>
> byte[] data type resolves to array type when specified in union as \{ "type": 
> "null", "bytes" } in the avro schema defined for a byte[] by using
> ReflectData.allowNull().get().getSchema(x.class);
> and then fails because byte primitive is not supported in array type after 
> resolveUnion resolves it to array using Reflect data for datum class - class 
> [B as described in this function below:
>  
> {code:java}
> protected void writeArray(Schema schema, Object datum, Encoder out)
>     throws IOException {
>     if (datum instanceof Collection) {
>       super.writeArray(schema, datum, out);
>       return;
>     }
>     Class<?> elementClass = datum.getClass().getComponentType();
>     if (null == elementClass) {
>       // not a Collection or an Array
>       throw new AvroRuntimeException("Array data must be a Collection or 
> Array");
>     }
>     Schema element = schema.getElementType();
>     if (elementClass.isPrimitive()) {
>       Schema.Type type = element.getType();
>       out.writeArrayStart();
>       switch(type) {
>       case BOOLEAN:
>         if(elementClass.isPrimitive())
>         ArrayAccessor.writeArray((boolean[]) datum, out);
>         break;
>       case DOUBLE:
>         ArrayAccessor.writeArray((double[]) datum, out);
>         break;
>       case FLOAT:
>         ArrayAccessor.writeArray((float[]) datum, out);
>         break;
>       case INT:
>         if(elementClass.equals(int.class)) {
>           ArrayAccessor.writeArray((int[]) datum, out);
>         } else if(elementClass.equals(char.class)) {
>           ArrayAccessor.writeArray((char[]) datum, out);
>         } else if(elementClass.equals(short.class)) {
>           ArrayAccessor.writeArray((short[]) datum, out);
>         } else {
>           arrayError(elementClass, type);
>         }
>         break;
>       case LONG:
>         ArrayAccessor.writeArray((long[]) datum, out);
>         break;
>       default:
>         arrayError(elementClass, type);
>       }
>       out.writeArrayEnd();
>     } else {
>       out.writeArrayStart();
>       writeObjectArray(element, (Object[]) datum, out);
>       out.writeArrayEnd();
>     }
>   }
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to