Skip to content

Shading Avro in pulsar-client #3762

@jerrypeng

Description

@jerrypeng

Describe the bug

Because we shade Avro in the pulsar-client, the ReflectData API to get a schema from a POJO fails to get the correct schema for generated Avro java classes

To Reproduce

Given an avro schema:

{
  "namespace": "example.avro",
  "type": "record",
  "name": "User",
  "fields": [
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "favorite_number",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "favorite_color",
      "type": "string"
    },
    {
      "name": "age",
      "type": "int",
      "default": 19
    }
  ]
}

Passing the generated java class from the above schema to the AvroSchema in pulsar-client produces the following result:

System.out.println(new String(org.apache.pulsar.client.impl.schema.AvroSchema.of(User.class).getSchemaInfo().getSchema()));

outputs:

{"type":"record","name":"User","namespace":"example.avro","fields":[{"name":"name","type":["null","string"],"default":null},{"name":"favorite_number","type":["null","int"],"default":null},{"name":"favorite_color","type":["null","string"],"default":null},{"name":"age","type":"int"}]}

which is incorrect while

System.out.println(ReflectData.get().getSchema(User.class));

outputs the correct schema that matches the original schema:

{"type":"record","name":"User","namespace":"example.avro","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":"string"},{"name":"age","type":"int","default":19}]}

Here is the line in Avro responsible for getting the schema from generated classes:
https://github.com/apache/avro/blob/release-1.8.2/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java#L277

Here is a project I created that demonstrates this problem:

https://github.com/jerrypeng/TestAvro

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/clienttype/bugThe PR fixed a bug or issue reported a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions