avro-kotlin / avro4k

Avro format support for Kotlin
Apache License 2.0
198 stars 37 forks source link

Avro.NULL doesn't appear to be working #97

Closed RyanAtWare2Go closed 3 years ago

RyanAtWare2Go commented 3 years ago

I am using: implementation("com.sksamuel.avro4k", "avro4k-core", "0.30.0")

I have this class:

@Serializable
data class AnalysisDto(
    val id: String,
    val name: String,
    val organizationId: String,
    val questionnaire: QuestionnaireDto,
    @AvroDefault(Avro.NULL)
    val source: String? = null,
    @Serializable(with= StringDateTimeSerializer::class)
    val createdAt: LocalDateTime,
    @Serializable(with= StringDateTimeSerializer::class)
    val updatedAt: LocalDateTime,
    @AvroDefault(Avro.NULL)
    val status: String? = null
)

When I run Avro.default.schema(AnalysisDto.serializer()), I get:

{"type":"record","name":"AnalysisDto","namespace":"com.rjf.www","fields":[..removed a bunch of fields for clarity, {"name":"status","type":["null","string"]}]}

I would have thought because I was using the @AvroDefault(Avro.NULL) annotation that there would be a default field on status. i.e. {"name":"status", "default": null, "type":["null","string"]}

I have not been able to find any issues in the source after debugging through it for quite a while.

thake commented 3 years ago

Hi @RyanAtWare2Go. Can you have a try with the current release com.github.avro-kotlin.avro4k:avro4k-core:1.1.0? You'll also find a lot of examples of how to use AvroDefault in the corresponding test file https://github.com/avro-kotlin/avro4k/blob/master/src/test/kotlin/com/github/avrokotlin/avro4k/schema/AvroDefaultSchemaTest.kt

Let me know if using the current version fixes the problem for you.

RyanAtWare2Go commented 3 years ago

I feel very dumb. Apparently there is an AvroDefault provided by Apache; that was the annotation I was using. I imported the RIGHT library this time and it worked. Sorry y'all.