Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonEnumDefaultValue not supported when using AvroMapper to generate schema from JAVA class #388

Open
Sonic-Rage opened this issue Aug 2, 2023 · 3 comments · May be fixed by #391
Open

Comments

@Sonic-Rage
Copy link

Sonic-Rage commented Aug 2, 2023

public enum Foobar {
    @JsonEnumDefaultValue
    FOO,
    BAR;
}

produces 
{
  "type" : "enum",
  "name" : "Foobar",
  "namespace" : "foo",
  "symbols" : [ "FOO", "BAR" ]
}

Need it to have the default key value pair in order to make enum forward compatible

{
  "type" : "enum",
  "name" : "FooBar",
  "namespace" : "foo",
  "symbols" : [ "FOO", "BAR" ],
  "default" : "FOO"
}

Unless I'm missing something I can't find a way to configure the class to fill in the default value when creating the schema

Configured the AvroMapper in some groovy to create the schema for reference

      avroMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false)
                    .configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
          avroMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true)

      AvroSchemaGenerator gen = new AvroSchemaGenerator();
        gen.enableLogicalTypes();
        avroMapper.acceptJsonFormatVisitor(classFile, gen);
        AvroSchema schemaWrapper = gen.getGeneratedSchema();

        org.apache.avro.Schema avroSchema = schemaWrapper.getAvroSchema();
        String avroSchemaInJSON = avroSchema.toString(true);
@cowtowncoder
Copy link
Member

Sounds like missing functionality. I don't know off-hand how easy it'd be to add: I think StringVisitor is the place to start from, if anyone has time and interest to look into this; followed by AvroSchemaHelper.createEnumSchema()

If anyone wants to tackle this, I'd be happy to help with PR.

@Sonic-Rage
Copy link
Author

Sonic-Rage commented Aug 25, 2023

@cowtowncoder

Yup createEnumSchema is where the update is needed. org.apache.avro.Schema createEnum is overloaded with additional param for setting default value. https://github.com/apache/avro/blob/master/lang/java/avro/src/main/java/org/apache/avro/Schema.java#L238

Do you know how we get @JsonEnumDefaultValue value to use that method to use for createEnum?

@cowtowncoder
Copy link
Member

cowtowncoder commented Aug 25, 2023

@Sonic-Rage All access to annotations is via AnnotationIntrospector, specifically JacksonAnnotationIntrospector from jackson-databind. Handler mentioned has access to configured introspector.

Introspection would need to be done by caller of Schema.createEnum()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants