pdvrieze/xmlutil

Unable to parse lists of nullable entries with nilAttributes enabled

brockj opened this issue · 1 comments

brockj commented

We are integrating with a back end that returns lists of nullable values that are marked as null using an xsi:nil attribute

nl.adaptivity.xmlutil.XmlException: local name ExpiryDateTime does not match expected "ExpiryDateTimeArray"
	at nl.adaptivity.xmlutil.XmlReader$DefaultImpls.require(XmlReader.kt:78)
	at nl.adaptivity.xmlutil.XmlBufferedReaderBase.require(XmlBufferedReaderBase.kt:27)
	at nl.adaptivity.xmlutil.XmlReader$DefaultImpls.require(XmlReader.kt:84)
	at nl.adaptivity.xmlutil.XmlBufferedReaderBase.require(XmlBufferedReaderBase.kt:27)
	at nl.adaptivity.xmlutil.serialization.XmlDecoderBase$TagDecoder.endStructure(XMLDecoder.kt:814)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:39)
	at nl.adaptivity.xmlutil.serialization.XmlDecoderBase$TagDecoder.decodeSerializableElement(XMLDecoder.kt:506)

The following test shows that we can encode the list of nullable values, but can not decode the same value

@Serializable
@XmlSerialName("Root")
class TestRoot(
    @XmlElement
    @XmlSerialName("ExpiryDateTimeArray")
    @XmlChildrenName("ExpiryDateTime")
    val values: List<String?>
)

class NullableListSpec : FeatureSpec({

    feature("List of nullable") {
        val xmlConfiguration = XML {
            nilAttribute = qname("http://www.w3.org/2001/XMLSchema-instance", "nil", "xsi") to "true"

            defaultPolicy {
                pedantic = true
            }
        }
        val expectedXml =
            "<Root><ExpiryDateTimeArray><ExpiryDateTime xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/></ExpiryDateTimeArray></Root>"

        scenario("encode") {
            val xml = xmlConfiguration.encodeToString(TestRoot(listOf(null)))
            xml shouldBe expectedXml
        }

        scenario("decode") {

            val testObject = xmlConfiguration.decodeFromString<TestRoot>(expectedXml)
            testObject.values.shouldHaveSingleElement(null)
        }
    }
})

This should be fixed in dev/the snapshots (after they have automatically build).