Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-1870

Avro compiler generate JAVA enum interfaces but writeEnum expects GenericEnumSymbol

Add voteVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.8.1
    • None
    • java
    • None

    Description

      Avro compiler generate an enum JAVA class from an enum Avro type
      and this class is used in the Java class of the records pointing to the enum type. However, org.apache.avro.generic.GenericDatumWriter.writeEnum expects a GenericEnumSymbol rather than Java Enum!
      This is problem because the class interfaces for Avro messages expect Java Enum and not GenericEnumSymbol!

      Consider the following schema:

      TrafficLights.avsc:
      {"namespace": "com.comp.message",
      "type": "record",
      "name": "TrafficLights",
      "fields": [

      {"name": "id", "type": "string"}

      ,

      {"name": "signal", "type": "Signals" }

      ]
      }

      Signals.avsc:
      {
      "namespace": "com.comp.message",
      "type": "enum",
      "name":"Signals",
      "symbols" : ["GREEN", "YELLOW", "RED"]
      }

      Using maven-avro-plugin to import "Signals" as an external schema, Avro compiler creates the following constructor for TrafficLights class:
      public TrafficLights(java.lang.CharSequence id, com.comp.message.Signals signal)

      {...}

      and an Enum class fot the Signals:

      package com.comp.message;
      @SuppressWarnings("all")
      @org.apache.avro.specific.AvroGenerated
      public enum Signals {
      GREEN, YELLOW, RED ;
      public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("

      {\"type\":\"enum\",\"name\":\"Signals\",\"namespace\":\"com.comp.message\",\"symbols\":[\"GREEN\",\"YELLOW\",\"RED\"]}

      ");
      public static org.apache.avro.Schema getClassSchema()

      { return SCHEMA$; }

      }

      However, when I try to serialize a TrafficLights message, GenericDatumWriter.writeEnum throws an exeception:

      TrafficLights mainTrafficLight = new TrafficLights ("123", Signals.GREEN);
      byte[] msg = new AvroMsgManager( mainTrafficLight .getSchema().toString() ).serialize (mainTrafficLight))

      org.apache.avro.AvroTypeException: Not an enum: GREEN
      at org.apache.avro.generic.GenericDatumWriter.writeEnum(GenericDatumWriter.java:164)
      at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:106)
      at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:73)
      at org.apache.avro.generic.GenericDatumWriter.writeField(GenericDatumWriter.java:153)
      at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:143)
      at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:105)
      at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:73)
      at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:60)

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            a_hashemi@yahoo.com Ali Hashemi

            Dates

              Created:
              Updated:

              Slack

                Issue deployment