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

Avro ResolvingGrammarGenerator does not honor "avro.java.string" property in inner record schemas

    XMLWordPrintableJSON

Details

    Description

      The type property "avro.java.string" is being used to qualify the CharSequence implementation of a string type in java. This property will be set in the java code generated by the avro maven plugin, if the <stringType> property is set to "String".

      However the ResolvingGrammarGenerator, which helps in matching the writer schema to the reader schema, does not honor this property for inner records within unions. Instead of deserializing to java.lang.String, the strings of the inner record will be deserialized to org.apache.avro.util.Utf8. String properties belonging to the outer record will be correctly deserialized to java.lang.String.

      If you try to deserialize an Avro record from a schema that has an inner record within an union type with the java code generated by the maven plugin (<stringType> is set to "String"), you'll get a ClassCastException:

      Caused by: java.lang.ClassCastException: class org.apache.avro.util.Utf8 cannot be cast to class java.lang.String
      

      This is because the generated java code expects the strings to be deserialized according to the "avro.java.string" property which does not happen for the inner record.

      I would expect that the deserializer treats the strings in the inner record the same as the strings in the outer record.

      Example:

      writer schema:

      {
        "type": "record",
        "name": "foo",
        "fields": [
          {
            "name": "k",
            "type": "string"
          },
          {
            "name": "value",
            "type": [
              "null",
              {
                "type": "record",
                "name": "bar",
                "fields": [
                  {
                    "name": "str",
                    "type": "string"
                  }
                ]
              }
            ]
          }
        ]
      }
      

       reader schema:

      {
        "type": "record",
        "name": "foo",
        "fields": [
          {
            "name": "k",
            "type": {
              "type": "string",
              "avro.java.string": "String"
            }
          },
          {
            "name": "value",
            "type": [
              "null",
              {
                "type": "record",
                "name": "bar",
                "fields": [
                  {
                    "name": "str",
                    "type": {
                      "type": "string",
                      "avro.java.string": "String"
                    }
                  }
                ]
              }
            ]
          }
        ]
      }
      

      You'll find some example kotlin code demonstrating the problem in the attached Bar.kt.

      Attachments

        1. Bar.kt
          4 kB
          Thorsten Hake

        Issue Links

          Activity

            People

              abellemare Adam Bellemare
              thorsten.hake Thorsten Hake
              Votes:
              2 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: