Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline types give exception: Expected an object, but got a scalar value #227

Closed
Virtlink opened this issue Jan 13, 2022 · 1 comment
Closed

Comments

@Virtlink
Copy link

Describe the bug

When using an inline type that is serializable, Kaml throws an exception: Expected an object, but got a scalar value.

Reproduction repo

Here is a test that demonstrates the issue:

import com.charleskorn.kaml.Yaml
import kotlinx.serialization.Serializable
import kotlin.test.Test
import kotlin.test.assertEquals

class KamlTest {

    @Test
    fun `should write and read inline types`() {
        val user = User(
            "Byron-King, A. A.",
            Password("Lovelace")
        )
        val yamlStr = Yaml.default.encodeToString(User.serializer(), user)
        println(yamlStr)

        val result = Yaml.default.decodeFromString(User.serializer(), yamlStr)
        assertEquals(user, result)
    }

    @Serializable
    data class User(
        val username: String,
        val password: Password,
    )

    @Serializable
    @JvmInline value class Password(val value: String)
}

Steps to reproduce

Create a @Serializable @JvmInline value class and use its type in a data class. When writing the type to YAML, it works fine, but when reading the type from YAML, the exception is thrown.

Expected behaviour

No exception to be thrown.

Actual behaviour

An exception is thrown:

com.charleskorn.kaml.InvalidPropertyValueException at password on line 2, column 11: Value for 'password' is invalid: Expected an object, but got a scalar value
	at app//com.charleskorn.kaml.YamlObjectInput.decodeElementIndex(YamlObjectInput.kt:58)
	at app//KamlTest$User$$serializer.deserialize(KamlTest.kt:23)
Caused by: com.charleskorn.kaml.IncorrectTypeException at password on line 2, column 11: Expected an object, but got a scalar value
	at app//com.charleskorn.kaml.YamlInput$Companion.createFor$kaml(YamlInput.kt:51)
	at app//com.charleskorn.kaml.YamlObjectInput.decodeElementIndex(YamlObjectInput.kt:51)
	... 91 more

Version information

Kaml 0.39.0

Any other information

No response

@charleskorn
Copy link
Owner

Thanks for the bug report @Virtlink. I've fixed this issue and will publish a new version shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants