Skip to content

class with XmlValue and another Contextual fails #238

@hfhbd

Description

@hfhbd
@Serializable
data class Foo(
  @XmlValue
  val foo: String,

  @Contextual
  val id: kotlin.uuid.Uuid? = null,
)

@Test
fun a() {
  XML(
    serializersModule = SerializersModule {
      contextual(Uuid::class, Uuid.serializer())
    },
  ) {}.encodeToString(Foo.serializer(), Foo("foo"))
}

fails with

nl.adaptivity.xmlutil.serialization.XmlSerialException: Types with an @XmlValue member may not contain other child elements (ContextDescriptor(kClass: class kotlin.uuid.Uuid, original: kotlinx.serialization.ContextualSerializer())?
	at nl.adaptivity.xmlutil.serialization.structure.XmlCompositeDescriptor$children$2.invoke(XmlDescriptor.kt:735)
	at nl.adaptivity.xmlutil.serialization.structure.XmlCompositeDescriptor$children$2.invoke(XmlDescriptor.kt:720)
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
	at nl.adaptivity.xmlutil.serialization.structure.XmlCompositeDescriptor.getChildren(XmlDescriptor.kt:720)
	at nl.adaptivity.xmlutil.serialization.structure.XmlCompositeDescriptor.getElementDescriptor(XmlDescriptor.kt:788)
	at nl.adaptivity.xmlutil.serialization.XmlEncoderBase$TagEncoder.encodeStringElement(XMLEncoder.kt:619)
	at SerializationTest$Foo.write$Self$core_test(SerializationTest.kt:761)
	at SerializationTest$Foo$$serializer.serialize(SerializationTest.kt:761)
	at SerializationTest$Foo$$serializer.serialize(SerializationTest.kt:761)
	at nl.adaptivity.xmlutil.serialization.XML.encodeToWriter(XML.kt:240)
	at nl.adaptivity.xmlutil.serialization.XML.encodeToWriter(XML.kt:168)
	at nl.adaptivity.xmlutil.serialization.XML.encodeToString(XML.kt:113)
	at nl.adaptivity.xmlutil.serialization.XML.encodeToString(XML.kt:100)
	at SerializationTest.a(SerializationTest.kt:778)

Usecase:
I want to use the new Uuid class but until Kotlin 2.1, there is no plugin generated serializer support, so you have to use @Contextual.

Kotlin version: 2.0.20
Kx serialization: 1.7.2
xml: 0.86.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions