You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Swamy Thota <sw...@gmail.com> on 2021/07/05 19:17:31 UTC

Re: Issue with ReflectDatumWriter With Enums

Hello Ryan,
Below example illustrates the problem with more details. It seem to cause issues when the enum is a property of another POJO.
package com.avro.test;

import lombok.Builder;
import lombok.Data;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.reflect.ReflectData;
import org.apache.avro.reflect.ReflectDatumWriter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class AvroTest {
    public static void main(String[] args) {
        Pojo pojo = Pojo.builder().testEnum(TestEnum.K).build();
        serialize(pojo);
    }

    public static byte[] serialize(Pojo object) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Encoder encoder = EncoderFactory.get().binaryEncoder(baos, null);

        ReflectDatumWriter<Pojo> rdw = new ReflectDatumWriter<Pojo>(Pojo.class, ReflectData.AllowNull.get());
        try {
            rdw.write(object, encoder);
            encoder.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    @Data
    @Builder
    public static class Pojo {
        private TestEnum testEnum;
    }

    enum TestEnum {
        V {
            @Override
            public boolean is_V() {
                return true;
            }
        },

        K {
            @Override
            public boolean is_K() {
                return true;
            }
        };

        public boolean is_V() {
            return false;
        }

        public boolean is_K() {
            return false;
        }
    }
}


> On 30-Jun-2021, at 8:59 PM, Ryan Skraba <ry...@skraba.com> wrote:
> 
> Hello!  I'm pretty sure that I've used enums with implementations and
> ReflectData successfully, even with old versions of Avro.
> 
> It seems to work with 1.9.x+ with the following ReflectDatumWriter
> (where datum is an instance of the TestEnum):
> 
>      Encoder encoder = EncoderFactory.get().binaryEncoder(baos, null);
>      DatumWriter<T> w = new
> ReflectDatumWriter<>(ReflectData.get().getSchema(TestEnum.class));
>      w.write(datum, encoder);
>      encoder.flush();
> 
> Do you have any extra detail about how you're constructing the
> ReflectDatumWriter or getting the schema for the TestEnum?
> 
> On Fri, Jun 25, 2021 at 9:39 AM Swamy Thota <sw...@gmail.com> wrote:
>> 
>> Hi All,
>> 
>> I’m seeing an issue with ReflectDatumWriter when the enum implements methods as below:
>> 
>> enum TestEnum {
>>        V{
>>                @Override
>>                public boolean is_V(){
>>                        return true;
>>                }
>>        }
>> 
>>        K{
>>                @Override
>>                public boolean is_K(){
>>                        return true;
>>                }
>>        }
>> 
>>        public boolean is_V(){
>>                return false;
>>        }
>> 
>>        public boolean is_K(){
>>                return false;
>>        }
>> }
>> 
>> This type of enums are failing with SchemaParseException: Empty name. Is there any work around or a fix available, appreciate the help in advance.
>> 
>> Thanks,
>> Swamy