diff --git a/src/commonMain/kotlin/com/charleskorn/kaml/YamlConfiguration.kt b/src/commonMain/kotlin/com/charleskorn/kaml/YamlConfiguration.kt index f07b4550..9e8e5e3b 100644 --- a/src/commonMain/kotlin/com/charleskorn/kaml/YamlConfiguration.kt +++ b/src/commonMain/kotlin/com/charleskorn/kaml/YamlConfiguration.kt @@ -60,6 +60,7 @@ public data class YamlConfiguration( internal val codePointLimit: Int? = null, @ExperimentalSerializationApi internal val decodeEnumCaseInsensitive: Boolean = false, + internal val singleValueAsList: Boolean = false ) public enum class PolymorphismStyle { diff --git a/src/commonMain/kotlin/com/charleskorn/kaml/YamlInput.kt b/src/commonMain/kotlin/com/charleskorn/kaml/YamlInput.kt index 1da8750f..99b3c231 100644 --- a/src/commonMain/kotlin/com/charleskorn/kaml/YamlInput.kt +++ b/src/commonMain/kotlin/com/charleskorn/kaml/YamlInput.kt @@ -73,6 +73,10 @@ public sealed class YamlInput( throw MissingTypeTagException(node.path) } } + + configuration.singleValueAsList -> { + return createFor(YamlList(listOf(node), node.path), yaml, context, configuration, descriptor) + } else -> throw IncorrectTypeException( "Expected ${descriptor.kind.friendlyDescription}, but got a scalar value", diff --git a/src/commonTest/kotlin/com/charleskorn/kaml/YamlReadingTest.kt b/src/commonTest/kotlin/com/charleskorn/kaml/YamlReadingTest.kt index c17c9286..c0266dc3 100644 --- a/src/commonTest/kotlin/com/charleskorn/kaml/YamlReadingTest.kt +++ b/src/commonTest/kotlin/com/charleskorn/kaml/YamlReadingTest.kt @@ -764,6 +764,39 @@ class YamlReadingTest : FlatFunSpec({ } } + context("given some input represent an object with some single values") { + val input = """ + one: ["a", "b", "c"] + two: "d" + three: "e" + four: ["f", "g"] + """.trimIndent() + + context("parsing that input") { + test("throws exception when disable accepting single value as list") { + val exception = shouldThrow { + Yaml.default.decodeFromString(MapSerializer(String.serializer(), ListSerializer(String.serializer())), input) + } + + exception.asClue { + it.message shouldBe "Value for 'two' is invalid: Expected a list, but got a scalar value" + } + } + + test("deserializes it as a Map with a string list") { + val yaml = Yaml(configuration = YamlConfiguration(singleValueAsList = true)) + val result = yaml.decodeFromString(MapSerializer(String.serializer(), ListSerializer(String.serializer())), input) + + result shouldBe mapOf( + "one" to listOf("a", "b", "c"), + "two" to listOf("d"), + "three" to listOf("e"), + "four" to listOf("f", "g") + ) + } + } + } + context("given some input representing a generic map") { val input = """ SOME_ENV_VAR: somevalue