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

Support single-element unions for Java code generation

    XMLWordPrintableJSON

Details

    Description

      I am writing code to integrate with existing Avro-based data created by someone else. Their schema currently has one-element unions for their types, rather than just the specific types. For example:

      {
        "type": "record",
        "name": "Issue",
        "namespace": "invalid.example",
        "fields": [
          {
            "name": "myField",
            "type": ["long"]
          }
        ]
      }
      

      instead of:

      {
        "type": "record",
        "name": "Issue",
        "namespace": "invalid.example",
        "fields": [
          {
            "name": "myField",
            "type": "long"
          }
        ]
      }
      

      While this isn't idiomatic, looking at the Avro Specification, it does appear to be valid. It should also be logically equivalent to the non-union version of this.

      The Java code generator, however, generates this field as an Object, not as a long like it would with the non-union type (or the Long if it were the ["null", "long"] or ["long", "null"] union).

      java -jar avro-tools-1.11.3.jar compile schema issue.avsc .
      

      produces

        private java.lang.Object myField;
      

      instead of

        private long myField;
      

      For my case, I'm going to work with the other project's developers to change their schema type to be the primitive type.

      However, I'm raising this issue in case you want to improve the handling of this case to have a single-element union generate the same code as it would if just as if it were instead just that single element. i.e. {"name": myField", "type": ["long"]} would generate the same code as {"name": myField", "type": "long"} currently does, which is as a long value in the Java code.

      Attachments

        Activity

          People

            Unassigned Unassigned
            mjjustin M. Justin
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 20m
                20m