From adfb92628ae5838adef87a85795c0506c057466b Mon Sep 17 00:00:00 2001 From: Paul de Vrieze Date: Wed, 19 Jun 2024 11:19:36 +0100 Subject: [PATCH] Fix #220, deserializing contextual values in certain cases. --- Changelog.md | 6 +- serialization/build.gradle.kts | 1 + .../xmlutil/serialization/XMLDecoder.kt | 3 + .../ContextualWithTypeSerializer220.kt | 67 +++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 serialization/src/commonTest/kotlin/nl/adaptivity/xml/serialization/regressions/ContextualWithTypeSerializer220.kt diff --git a/Changelog.md b/Changelog.md index 6641c68cc..e98c113c8 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,8 +1,12 @@ -#0.90.2 +#0.90.2-SNAPSHOT +Changes: +- Add "work in progress" xml schema module to the dev branch. This + is not yet ready for release (but will parse most xml schemas) Fixes: - Don't make the companion of `XmlDeclMode` internal (#219). This is a workaround for a regresion in 2.0 that doesn't allow resolving enum constants in this case. +- Fix deserialization with contextual serializer #220 # 0.90.0 2.0 will go *(June 13, 2024) diff --git a/serialization/build.gradle.kts b/serialization/build.gradle.kts index bfcf86f39..d54b1c130 100644 --- a/serialization/build.gradle.kts +++ b/serialization/build.gradle.kts @@ -126,6 +126,7 @@ kotlin { implementation(projects.serialutil) implementation(projects.testutil) implementation(libs.serialization.json) + implementation(libs.datetime) implementation(kotlin("test-common")) implementation(kotlin("test-annotations-common")) diff --git a/serialization/src/commonMain/kotlin/nl/adaptivity/xmlutil/serialization/XMLDecoder.kt b/serialization/src/commonMain/kotlin/nl/adaptivity/xmlutil/serialization/XMLDecoder.kt index 256ba719e..2ad303115 100644 --- a/serialization/src/commonMain/kotlin/nl/adaptivity/xmlutil/serialization/XMLDecoder.kt +++ b/serialization/src/commonMain/kotlin/nl/adaptivity/xmlutil/serialization/XMLDecoder.kt @@ -256,6 +256,9 @@ internal open class XmlDecoderBase internal constructor( * But only if decodeInline was called previously. */ val desc = when { + xmlDescriptor is XmlContextualDescriptor -> + xmlDescriptor.resolve(deserializer.descriptor, config, serializersModule) + triggerInline && xmlDescriptor is XmlInlineDescriptor -> xmlDescriptor.getElementDescriptor(0) diff --git a/serialization/src/commonTest/kotlin/nl/adaptivity/xml/serialization/regressions/ContextualWithTypeSerializer220.kt b/serialization/src/commonTest/kotlin/nl/adaptivity/xml/serialization/regressions/ContextualWithTypeSerializer220.kt new file mode 100644 index 000000000..292d3235d --- /dev/null +++ b/serialization/src/commonTest/kotlin/nl/adaptivity/xml/serialization/regressions/ContextualWithTypeSerializer220.kt @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024. + * + * This file is part of xmlutil. + * + * This file is licenced to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You should have received a copy of the license with the source distribution. + * Alternatively, you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package nl.adaptivity.xml.serialization.regressions + +import io.github.pdvrieze.xmlutil.testutil.assertXmlEquals +import kotlinx.datetime.Instant +import kotlinx.serialization.Contextual +import kotlinx.serialization.Serializable +import kotlinx.serialization.decodeFromString +import nl.adaptivity.xmlutil.serialization.XML +import nl.adaptivity.xmlutil.serialization.XmlElement +import kotlin.test.Test +import kotlin.test.assertEquals + +/** Test for handling Instant serialization when it is resolved contextually #220 */ +class ContextualWithTypeSerializer220 { + @Serializable + data class Box( + @Contextual + @XmlElement val i: Instant + ) + + @Test + fun deserializeTest() { + val box: Box = XML.defaultInstance.decodeFromString( + """ + + 2023-11-02T15:56:49.364+01:00 + """.trimIndent() + ) + + val expected = Instant.fromEpochMilliseconds(1698937009364L) + + assertEquals(expected, box.i) + } + + @Test + fun serializeTest() { + val expected = + """ + 2023-11-02T14:56:49.364Z + """.trimIndent() + + + val actual = XML.encodeToString(Box(Instant.fromEpochMilliseconds(1698937009364L))) + + assertXmlEquals(expected, actual) + } +}