avro-kotlin / avro4k

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

Generic classes lead to serialisation issues #153

Closed upMKuhn closed 10 months ago

upMKuhn commented 10 months ago

Hello,

I have a class called DatedValue of type String and Double. The schema for this class is generated wrongly. As you can see the first definition says the value is of type string and then this reference is wrongly repeated. Is this a bug in avro4k or a limitation in avro schemas?

Thank you!

java.lang.ClassCastException: value 638.2585562648774 (a java.lang.Double) cannot be cast to expected type string at [v1, v1, v1, v1, v1, v1, v1, v1][v1].owners[0].quantity[0].value at org.apache.avro.path.TracingClassCastException.summarize(TracingClassCastException.java:79) at org.apache.avro.path.TracingClassCastException.summarize(TracingClassCastException.java:30) at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:84) at com.github.avrokotlin.avro4k.io.DefaultAvroOutputStream.write(DefaultAvroOutputStream.kt:32) at com.github.avrokotlin.avro4k.io.AvroOutputStream$DefaultImpls.write(AvroOutputStream.kt:22) at com.github.avrokotlin.avro4k.io.DefaultAvroOutputStream.write(DefaultAvroOutputStream.kt:15)

@Serializable
data class Owner(
    val brokerId: String,
    val type: Int,
    val name: List<DatedValue<String>>,
    val quantity: List<DatedValue<Double>>
) {
    companion object
}

@Serializable
data class DatedValue<T>(
    @Serializable(with = LocalDateSerializer::class)
    val asOfDate: LocalDate,
    val value: T
) {
    companion object
}

Schema

        {
          "name": "owners",
          "type":
            {
              "type": "array",
              "items":
                {
                  "type": "record",
                  "name": "Owner",
                  "namespace": "com.mk.trading.broker.models.contracts.fundamentals",
                  "fields":
                    [
                      { "name": "brokerId", "type": "string" },
                      { "name": "type", "type": "int" },
                      {
                        "name": "name",
                        "type":
                          {
                            "type": "array",
                            "items":
                              {
                                "type": "record",
                                "name": "DatedValue",
                                "fields":
                                  [
                                    { "name": "asOfDate", "type": "string" },
                                    { "name": "value", "type": "string" },
                                  ],
                              },
                          },
                      },
                      {
                        "name": "quantity",
                        "type": { "type": "array", "items": "DatedValue" },
                      },
                    ],
                },
            },
        },
thake commented 10 months ago

Hey @upMKuhn, this seems to be a bug in avro4k. Thanks for bringing it up. Can you provide a branch with a Junit-Testcase? This helps a lot to track down the cause.

upMKuhn commented 10 months ago

Done, I added a schema test :)

fyi: @thake

https://github.com/avro-kotlin/avro4k/pull/154

thake commented 10 months ago

Thanks, I will have a look into it!

thake commented 10 months ago

Hey @upMKuhn, I've dug a little bit deeper and found an Avro-Issue that states that Avro, as of now, does not support parameterized types. Until this issue is resolved, I wouldn't know how to correctly generate a schema for a parameterized type.

As a workaround, you could do the following:

```kotlin
@Serializable
data class Owner(
  val brokerId: String,
  val type: Int,
  val name: List<StringDatedValue>,
  val quantity: List<DoubleDatedValue>
)
interface DatedValue<T> {  
  val asOfDate: LocalDate
  val value:  T
}
@Serializable
data class StringDatedValue(
  @Serializable(with = LocalDateSerializer::class)
  override val asOfDate: LocalDate,
  override val value: String
) : DatedValue<String>

@Serializable
data class DoubleDatedValue(
  @Serializable(with = LocalDateSerializer::class)
  override val asOfDate: LocalDate,
  override val value: Double
) : DatedValue<Double>
upMKuhn commented 10 months ago

Nice thank you!

Chuckame commented 10 months ago

But in parallel, kotlinx serialization should handle nested generics, another feature to take into account in #148

thake commented 10 months ago

Closing this issue for now. If parameterized types are supported in avro, we'll open a new issue.