Skip to content

Commit

Permalink
Fix descriptor overrides for nullable values (using a synthetic marker
Browse files Browse the repository at this point in the history
annotation and exposing the delegate on a negative index).
  • Loading branch information
pdvrieze committed Dec 29, 2023
1 parent 0f395c6 commit ddda274
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
15 changes: 13 additions & 2 deletions core/src/commonMain/kotlin/nl/adaptivity/xmlutil/XmlSerializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public fun SerialDescriptor.xml(
return XmlSerialDescriptorImpl(this, xmlDescriptor, serialQName)
}

/**
* Marker to signify that the descriptor is an xmlSerialDescriptor and the delegate
* can be retrieved through getElementDescriptor with negative value.
*/
@XmlUtilInternal
public annotation class XmlSerialDescriptorMarker

/**
* Serial Descriptor delegate that supports special casing by the XML format. This means
* that the descriptor can be different for non-xml and xml serialization. (Used by the QName
Expand All @@ -73,7 +80,10 @@ public interface XmlSerialDescriptor : SerialDescriptor {
override fun getElementAnnotations(index: Int): List<Annotation> = delegate.getElementAnnotations(index)

@ExperimentalSerializationApi
override fun getElementDescriptor(index: Int): SerialDescriptor = delegate.getElementDescriptor(index)
override fun getElementDescriptor(index: Int): SerialDescriptor = when {
index < 0 -> xmlDescriptor
else -> delegate.getElementDescriptor(index)
}

@ExperimentalSerializationApi
override fun getElementIndex(name: String): Int = delegate.getElementIndex(name)
Expand All @@ -87,7 +97,8 @@ public interface XmlSerialDescriptor : SerialDescriptor {
}

@ExperimentalSerializationApi
override val annotations: List<Annotation> get() = delegate.annotations
override val annotations: List<Annotation> get() =
listOf(XmlSerialDescriptorMarker()) + delegate.annotations

override val isInline: Boolean get() = delegate.isInline

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020.
* Copyright (c) 2023.
*
* This file is part of xmlutil.
*
Expand All @@ -22,6 +22,7 @@ package nl.adaptivity.xmlutil.serialization
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.builtins.nullable
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.modules.SerializersModule
import nl.adaptivity.xmlutil.*
Expand Down Expand Up @@ -654,13 +655,11 @@ private constructor(
tagParent: SafeParentInfo
): KSerializer<*>? =
when (serializerParent.elementSerialDescriptor.serialName) {
/*
"javax.xml.namespace.QName?",
"javax.xml.namespace.QName" -> when {
serializerParent.elementSerialDescriptor.isNullable -> QNameSerializer.nullable
else -> QNameSerializer
}
*/

else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1653,4 +1653,8 @@ internal fun <A : Appendable> A.appendIndent(count: Int) = apply {
}
}

internal fun SerialDescriptor.getXmlOverride() = (this as? XmlSerialDescriptor)?.xmlDescriptor ?: this
internal fun SerialDescriptor.getXmlOverride() = when {
this is XmlSerialDescriptor -> xmlDescriptor
isNullable && annotations.any { it is XmlSerialDescriptorMarker } -> getElementDescriptor(-1).nullable
else -> this
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021.
* Copyright (c) 2023.
*
* This file is part of xmlutil.
*
Expand Down Expand Up @@ -53,7 +53,7 @@ class QNameContentContainerTest : PlatformTestBase<QNameContentContainerTest.Con
val name: QName,
@XmlElement
@XmlSerialName("value", namespace = "urn:example.org", prefix = "otherns")
val value: QName
val value: QName?
)

}

0 comments on commit ddda274

Please sign in to comment.