Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect handling of polymorphism (sealed class) #48

Open
amirabiri opened this issue Sep 7, 2022 · 1 comment
Open

Incorrect handling of polymorphism (sealed class) #48

amirabiri opened this issue Sep 7, 2022 · 1 comment

Comments

@amirabiri
Copy link

The library appears to be adding a redundant value: to members of a polymorphic collection.

This code:

@Serializable
data class ConfigFile(val items: List<Item>)

@Serializable
sealed class Item

@Serializable
@SerialName("A")
class ItemA(val property: String): Item()

println(Yaml.encodeToString(ConfigFile(listOf(ItemA("foo")))))

Produces the following result:

items:
  - type: A
    value:
      property: foo

This does not appear to be inline with kotlinx.serialization documentation: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#sealed-classes

If using Json the result is as expected:

{
    "items": [
        {
            "type": "A",
            "property": "foo"
        }
    ]
}
@Him188
Copy link
Owner

Him188 commented Sep 8, 2022

Yamlkt's polymorphism was not being tested, as stated in README that yamlkt does not support polymorohism. The current behavior may be some kind of fallback strategy implemented by kotlunx.serialization for custom formats that does not support polymorphism well.
Note that ProtoBuf also uses this format ('type' with proto number 1, 'value' with proto number 2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants