jackson-dataformats-binary icon indicating copy to clipboard operation
jackson-dataformats-binary copied to clipboard

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

Open Sonic-Rage opened this issue 2 years ago • 3 comments

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);

Sonic-Rage avatar Aug 02 '23 20:08 Sonic-Rage

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.

cowtowncoder avatar Aug 19 '23 01:08 cowtowncoder

@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?

Sonic-Rage avatar Aug 25 '23 16:08 Sonic-Rage

@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()

cowtowncoder avatar Aug 25 '23 16:08 cowtowncoder