Skip to content
This repository was archived by the owner on Nov 30, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/commonMain/kotlin/com/charleskorn/kaml/YamlInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 33 additions & 0 deletions src/commonTest/kotlin/com/charleskorn/kaml/YamlReadingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<InvalidPropertyValueException> {
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
Expand Down