~ can not be used as a replacement for null
BloodWorkXGaming opened this issue · comments
I just noticed that if you have a nullable list like
@Serializable
data class TestClass(
val n: List<String>? = null
)
and you try to read it with
val yaml =
"""
n: ~
""".trimIndent()
val obj = Yaml.default.decodeFromString<TestClass>(yaml)
It fails with the exception:
Value for 'n' is invalid: Expected a list, but got a scalar value
com.charleskorn.kaml.InvalidPropertyValueException at n on line 2, column 4: Value for 'n' is invalid: Expected a list, but got a scalar value
Looking further at the spec, I figured out that ~ is only supported in the Core spec, not the JSON spec.
Which spec is this library targeting?
https://yaml.org/spec/1.2/spec.html#Schema
Thanks for the report @BloodWorkXGaming, you're correct - ~
should be treated as a null value. In the JSON schema, null
is the canonical form (so null
should be emitted by default), but this should not impact parsing.
Would you be interested in submitting a PR to fix this issue? https://github.com/charleskorn/kaml/blob/main/src/jvmMain/kotlin/com/charleskorn/kaml/YamlNodeReader.kt#L64 looks like the right place for this fix (+ adding some tests, of course :) )
Yes, thank you, that looks pretty straight forward to fix. Will try to fix it and open a new PR :)