Skip to content

Commit

Permalink
Fix #220, deserializing contextual values in certain cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdvrieze committed Jun 19, 2024
1 parent a7fe167 commit adfb926
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions serialization/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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(
"""<?xml version="1.0" encoding="UTF-8"?>
<Box>
<i>2023-11-02T15:56:49.364+01:00</i>
</Box>""".trimIndent()
)

val expected = Instant.fromEpochMilliseconds(1698937009364L)

assertEquals(expected, box.i)
}

@Test
fun serializeTest() {
val expected =
"""<Box>
<i>2023-11-02T14:56:49.364Z</i>
</Box>""".trimIndent()


val actual = XML.encodeToString(Box(Instant.fromEpochMilliseconds(1698937009364L)))

assertXmlEquals(expected, actual)
}
}

0 comments on commit adfb926

Please sign in to comment.