avro-kotlin / avro4k

Avro format support for Kotlin
Apache License 2.0
188 stars 36 forks source link

Support @AvroDefault(value=null) #95

Closed fugithub79 closed 3 years ago

fugithub79 commented 3 years ago

I want to generate a schema file. Given fields like the following

    @AvroDoc("For coupons only.  If the reward is a coupon, this will be the id")
    val coupon_id:Int? = null,

    @AvroDoc("For coupons only.  The start date of the coupon.")
    @Serializable(with= InstantSerializer::class)
    val coupon_timestamp:Instant? = null,

    @AvroDoc("For coupons only.  The date when the coupon should first be displayed to the user.")
    @Serializable(with= InstantSerializer::class)
    val coupon_display_timestamp:Instant? = null,

I would like to add "default": null to each of these in the generated schema. Unfortunately, it appears that I can't do something like:

    ...
    @Serializable(with= InstantSerializer::class)
   @AvroDoc(value=null)
    val coupon_display_timestamp:Instant? = null

Because the parser considers this to be invalid.

Is this an actual issue? Short of writing my own serializer, is there another way to accomplish the same thing?

kossi commented 3 years ago

Hi @fugithub79

I don't know if this is an actual issue or should this be actually supported but have you tried to use the @AvroDefault annotation? Or on my current project this works or a "generator" does something like this ->

@AvroDefault(Avro.NULL)
val maybe: Sting? = null
thake commented 3 years ago

Should work as @kossi has written. Closing this issue.

samu-developments commented 1 year ago

I tried to get the "default": null in my schema, however it seems like I'm missing something?


@Serializable
data class Test(
    @AvroDefault(Avro.NULL)
    val nullable: String? = null
)

val record = Avro.default.schema(
    serializer = Test.serializer()
)
println(record)

yields {"type":"record","name":"Test","namespace":"com.test.avro","fields":[{"name":"nullable","type":["null","string"]}]}

How can I get

{"type":"record","name":"Test","namespace":"com.test.avro","fields":[{"name":"nullable","type":["null","string"], "default": null}]}

I use avro4k with AWS MSK+Glue schema registry with auto registration of new schemas, and schema compatibility fails since Glue complains about new nullable fields without a default value.

Thanks

edit. Wrong AvroDefault import :facepalm: