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

GenericRecord and specific class return different java type for the same field with logicalType

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.8.2
    • None
    • java, logical types
    • None

    Description

      I have an Avro schema which contains field with logical type timestamp-millis. When I deserialize  an object with this schema from json to GenericRecord, I get Long as type of this field. When I deserialize it to specific class, I get DateTime. This prevents me to write a generic code which would handle both cases (I would expect this to be possible since the generated class implements GenericRecord interface).

      E.g.

      final GenericRecord timestampGenericRecord = readJsonGeneric("timestamptest.json", schema);
      final TimestampTest timestampTestIn = readJson("timestamptest.json", timestampTestOut.getClass(), schema);
      
      System.out.println("------------ Generic record --------------");
      printTimestamp(timestampGenericRecord);
      
      System.out.println("------------ Specific record --------------");
      printTimestamp(timestampTestIn);
      
      ----
      
      private static void printTimestamp(GenericRecord record) {
         System.out.println(record);
         System.out.println(record.get("timestampDateTime").getClass());
      }

      prints out:

      ------------ Generic record --------------
      {"timestampLong": 1530023620474, "timestampDateTime": 1530023620478}
      class java.lang.Long
      ------------ Specific record --------------
      {"timestampLong": 1530023620474, "timestampDateTime": 2018-06-26T14:33:40.478Z}
      class org.joda.time.DateTime
      

      I am attaching sample code which demonstrates the problem.

      Attachments

        1. avrotest.zip
          3 kB
          Eva Krejcirova

        Activity

          People

            Unassigned Unassigned
            eva.krejcirova Eva Krejcirova
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: