Skip to content

Inability to specify custom namespace for java-key-class entries when using @AvroNamespace annotation in those classes #611

@MartinK99

Description

@MartinK99

Hey, so when generating a avro schema using the AvroMapper and AvroSchemaGenerator, thanks to #310 we can specify the custom namespace each class should have. Now, given a class

ExampleClass.java
package com.example.test

import com.fasterxml.jackson.dataformat.avro.annotation.AvroNamespace;

import java.util.Map;

@AvroNamespace("com.github.test")
public class ExampleClass {

    public Map<ExampleEnum, String> exampleMap;
}

and the enum

ExampleEnum.java
package com.example.test

import com.fasterxml.jackson.dataformat.avro.annotation.AvroNamespace;

@AvroNamespace("com.github.test")
public enum ExampleEnum {
    FOO, BAR
}

, generating the schema by calling both classes explicitely (since nested generation doesn't work here) will yield

ExampleClass.avsc
{
  "type" : "record",
  "name" : "ExampleClass",
  "namespace" : "com.github.test",
  "fields" : [ {
    "name" : "exampleMap",
    "type" : [ "null", {
      "type" : "map",
      "values" : "string",
      "java-key-class" : "com.example.test"
    } ]
  } ]
}

and

ExampleEnum.avsc
{
  "type" : "enum",
  "name" : "ExampleEnum",
  "namespace" : "com.github.test",
  "symbols" : [ "FOO", "BAR" ]
}

Notice how the specified java-key-class has the wrong namespace defined, since it is simply resolving the package name of the existing Java object.
The likely correct way would be to fetch the contents of the @AvroNamespace annotation inside the enum class and resolve it that way when a java-key-class entry is generated, if possible, and otherwise resolve it the old way.
Because I'm not sure of that possiblity though, another suggestion would be to implement an @AvroKeyClass annotation to be able to manually override that package definition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    avrogood first issueIssue that seems easy to resolve and is likely a good candidate for contributors new to projectpr-neededFeature request for which PR likely needed (no active development but idea is workable)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions