avro-kotlin / avro4k

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

Kotlin Pair<A, B> schema is generated as Union<A, B> instead of record #166

Closed Chuckame closed 2 months ago

Chuckame commented 10 months ago

Inside tests, there is some Pair tests disabled. They are checking that:

   @Serializable
   data class StringDoubleTest(val p: Pair<String, Double>)

should generate a schema of :

{
    "type": "record",
    "name": "StringDoubleTest",
    "namespace": "com.github.avrokotlin.avro4k.schema.PairSchemaTest",
    "fields": [
        {
            "name": "p",
            "type": [
                "string",
                "double"
            ]
        }
    ]
}

But a pair is not an union like Either in scala (surely the misunderstanding during the conversion of avro4s to avro4k).

We should fix it to just generate record with string and double fields (the default native descriptor):

{
    "type": "record",
    "name": "StringDoubleTest",
    "namespace": "com.github.avrokotlin.avro4k.schema.PairSchemaTest",
    "fields": [
        {
            "name": "p",
            "type": {
                "type": "record",
                "name": "Pair",
                "namespace": "kotlin",
                "fields": [
                    {
                        "name": "first",
                        "type": "string"
                    },
                    {
                        "name": "second",
                        "type": "double"
                    }
                ]
            }
        }
    ]
}

And if the user wants to handle it in another way, then just serialize it using a standard contextual serializer.

Chuckame commented 2 months ago

Done in #174