Skip to content

Commit

Permalink
More work on compact fragment (and general XmlDeserializationStrategy…
Browse files Browse the repository at this point in the history
… madness in relation to value children).
  • Loading branch information
pdvrieze committed Feb 29, 2024
1 parent 5f6ed2c commit 8f91fda
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,11 @@ internal open class XmlDecoderBase internal constructor(

val effectiveDeserializer = childXmlDescriptor.effectiveDeserializationStrategy(deserializer)

val isValueChild = xmlDescriptor.getValueChild() == index

@Suppress("DEPRECATION")
if (((effectiveDeserializer as DeserializationStrategy<*>) == CompactFragmentSerializer) &&
(xmlDescriptor.getValueChild() == index)
if (false && ((effectiveDeserializer as DeserializationStrategy<*>) == CompactFragmentSerializer) &&
isValueChild
) {
// handle missing compact fragments
@Suppress("UNCHECKED_CAST")
Expand Down Expand Up @@ -753,8 +755,15 @@ internal open class XmlDecoderBase internal constructor(

else -> input
}
effectiveDeserializer
val r = effectiveDeserializer
.deserializeXML(decoder, i, previousValue, xmlDescriptor.getValueChild() == index)

// Make sure that the (end tag is not consumed) - it will be consumed by the endStructure function
if (input.eventType == EventType.END_ELEMENT && /*isValueChild && */input.depth < tagDepth) {
input.pushBackCurrent()
}

r
}

is AbstractCollectionSerializer<*, T, *> ->
Expand Down Expand Up @@ -1028,7 +1037,7 @@ internal open class XmlDecoderBase internal constructor(
val valueChild = descriptor.getValueChild()
// Handle the case of an empty tag for a value child. This is not a nullable item (so shouldn't be
// treated as such).
if (valueChild >= 0 && input.peek() is XmlEvent.EndElementEvent && !seenItems[valueChild]) {
if (valueChild >= 0 && /*input.peek() is XmlEvent.EndElementEvent &&*/ !seenItems[valueChild]) {
val valueChildDesc = xmlDescriptor.getElementDescriptor(valueChild)
// Lists/maps need to be empty (treated as null/missing)
if ((! valueChildDesc.isNullable) && valueChildDesc.kind !is StructureKind.LIST && valueChildDesc.kind !is StructureKind.MAP) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021.
* Copyright (c) 2024.
*
* This file is part of xmlutil.
*
Expand All @@ -21,7 +21,7 @@
package nl.adaptivity.xml.serialization

import kotlinx.serialization.Serializable
import nl.adaptivity.xmlutil.serialization.CompactFragmentSerializer
import nl.adaptivity.xmlutil.XmlEvent
import nl.adaptivity.xmlutil.serialization.XML
import nl.adaptivity.xmlutil.serialization.XmlElement
import nl.adaptivity.xmlutil.serialization.XmlValue
Expand All @@ -32,23 +32,23 @@ import kotlin.test.assertEquals
class TestCompactFragmentSerializer {
@Test
fun testSerializeCompactFragment() {
val f = FragmentContainer(CompactFragment("""<a><b>"hello"</b></a>"""), "bar")
val expected = "<FragmentContainer c=\"bar\">${f.fragment.contentString}</FragmentContainer>"
val f = FragmentContainer(CompactFragment(listOf(XmlEvent.NamespaceImpl("ns", "urn:ns")), """<ns:a><ns:b>"hello"</ns:b></ns:a>"""), "bar")
val expected = "<FragmentContainer xmlns:ns=\"urn:ns\" c=\"bar\">${f.fragment.contentString}</FragmentContainer>"
val actual = XML.Companion.encodeToString(f)
assertEquals(expected, actual)
}

@Test
fun testDeserializeCompactFragment() {
val expected = FragmentContainer(CompactFragment("""<a><b>"hello"</b></a>"""), "bar")
val input = "<FragmentContainer c=\"bar\">${expected.fragment.contentString}</FragmentContainer>"
val expected = FragmentContainer(CompactFragment(listOf(XmlEvent.NamespaceImpl("ns", "urn:ns")), """<ns:a><ns:b>"hello"</ns:b></ns:a>"""), "bar")
val input = "<FragmentContainer xmlns:ns=\"urn:ns\" c=\"bar\">${expected.fragment.contentString}</FragmentContainer>"
val actual = XML.decodeFromString<FragmentContainer>(input)
assertEquals(expected, actual)
}

@Serializable
data class FragmentContainer(
@XmlValue(true) @Serializable(CompactFragmentSerializer::class) val fragment: CompactFragment,
@XmlValue(true) val fragment: CompactFragment,
@XmlElement(false)
val c: String
)
Expand Down

0 comments on commit 8f91fda

Please sign in to comment.