Skip to content

Value class not being flattened in polymorphic scope #280

Closed
@boswelja

Description

@boswelja

In an attempt to model some XML resembling

<outer>
  Hello, world!
  <inner>This is some mixed content!</inner>
  Here's some more text!
</outer>

Where the text and inner can be in any arbitrary order, of zero or more items.

I have created a class hierarchy that looks like

@Serializable
data class Outer(
  @XmlValue
  val content: List<Content>
) {
  @Serializable
  sealed interface Content;

  @Serializable
  value class Text(val content: String): Content

  @Serializable
  data class Inner(
    @XmlValue
    val innerContent: String
  ): Content
}

However, the serializer spits back `Invalid XML value at position: 11:30: Serializer for subclass 'kotlin.String' is not found in the polymorphic scope of 'Content'.

Based on https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/value-classes.md, I would expect the value class to be "flattened" into a String automatically, but it seems like this is not happening? I'm no expert in XML in general, so maybe I've missed something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    indevThe issue is fixed/implemented in the dev branch

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions