Skip to content

Commit

Permalink
Fix issue where inline types can't be deserialized.
Browse files Browse the repository at this point in the history
Fixes #227.
  • Loading branch information
charleskorn committed Jan 14, 2022
1 parent bb5abab commit a561292
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/commonMain/kotlin/com/charleskorn/kaml/YamlInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public sealed class YamlInput(
else -> YamlNullInput(node, context, configuration)
}

is YamlScalar -> when (descriptor.kind) {
is PrimitiveKind, SerialKind.ENUM -> YamlScalarInput(node, context, configuration)
is SerialKind.CONTEXTUAL -> YamlContextualInput(node, context, configuration)
is PolymorphicKind -> throw MissingTypeTagException(node.path)
is YamlScalar -> when {
descriptor.kind is PrimitiveKind || descriptor.kind is SerialKind.ENUM || descriptor.isInline -> YamlScalarInput(node, context, configuration)
descriptor.kind is SerialKind.CONTEXTUAL -> YamlContextualInput(node, context, configuration)
descriptor.kind is PolymorphicKind -> throw MissingTypeTagException(node.path)
else -> throw IncorrectTypeException("Expected ${descriptor.kind.friendlyDescription}, but got a scalar value", node.path)
}

Expand Down
13 changes: 13 additions & 0 deletions src/commonTest/kotlin/com/charleskorn/kaml/YamlReadingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.modules.serializersModuleOf
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.jvm.JvmInline

object YamlReadingTest : Spek({
describe("a YAML parser") {
Expand Down Expand Up @@ -90,6 +91,14 @@ object YamlReadingTest : Spek({
}
}
}

context("parsing that input as a value type") {
val result = Yaml.default.decodeFromString(StringValue.serializer(), input)

it("deserializes it to the expected object") {
expect(result).toEqual(StringValue("hello"))
}
}
}

context("given the input '123'") {
Expand Down Expand Up @@ -2366,3 +2375,7 @@ private data class Container(@Contextual val inner: Inner)

@Serializable
private data class ObjectWithNestedContextualSerializer(@Serializable(with = ContextualSerializer::class) val thing: String)

@Serializable
@JvmInline
value class StringValue(val value: String)

0 comments on commit a561292

Please sign in to comment.