Skip to content

Commit

Permalink
Fix issue where decoding a polymorphic type that uses the tag polymor…
Browse files Browse the repository at this point in the history
…phism style could hang if no value was provided after the tag.

Fixes #179.
  • Loading branch information
charleskorn committed Sep 5, 2021
1 parent 82e30c1 commit e18785d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/commonMain/kotlin/com/charleskorn/kaml/YamlInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import kotlinx.serialization.descriptors.StructureKind
import kotlinx.serialization.descriptors.elementNames
import kotlinx.serialization.encoding.AbstractDecoder
import kotlinx.serialization.encoding.CompositeDecoder
import kotlinx.serialization.encoding.CompositeDecoder.Companion.DECODE_DONE
import kotlinx.serialization.encoding.CompositeDecoder.Companion.UNKNOWN_NAME
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.SerializersModuleCollector
Expand Down Expand Up @@ -168,7 +169,7 @@ private class YamlNullInput(val nullValue: YamlNode, context: SerializersModule,
override fun getCurrentLocation(): Location = nullValue.location
override fun getCurrentPath(): YamlPath = nullValue.path

override fun decodeElementIndex(descriptor: SerialDescriptor): Int = 0
override fun decodeElementIndex(descriptor: SerialDescriptor): Int = DECODE_DONE
}

@OptIn(ExperimentalSerializationApi::class)
Expand Down
19 changes: 19 additions & 0 deletions src/commonTest/kotlin/com/charleskorn/kaml/YamlReadingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,25 @@ object YamlReadingTest : Spek({
}
}

// See https://github.com/charleskorn/kaml/issues/179.
context("given some input where a tag is provided but no value is provided") {
val input = """
!<sealedString>
""".trimIndent()

context("parsing that input") {
it("throws an appropriate exception") {
expect({ polymorphicYaml.decodeFromString(TestSealedStructure.serializer(), input) }).toThrow<MissingRequiredPropertyException> {
message { toBe("Property 'value' is required but it is missing.") }
line { toBe(1) }
column { toBe(1) }
propertyName { toBe("value") }
path { toBe(YamlPath.root) }
}
}
}
}

context("given some input where the value is a literal") {
val input = """
!<simpleString> "asdfg"
Expand Down

0 comments on commit e18785d

Please sign in to comment.