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

Prepare fix for contextual deserialization of polymorphic scalars #618

Merged
merged 1 commit into from
Oct 29, 2024

Conversation

Jojo4GH
Copy link
Contributor

@Jojo4GH Jojo4GH commented Sep 16, 2024

Second bug from #607 (as preparation for the PR)

Previously it was not possible to implement a content based polymorphic serializer in the case that one of the types involved is a scalar. Different from the JSON serialization library it was therefore impossible to implement something like:

myCats:
  myFirstCat: "Bella"
  mySecondCat:
    name: "Daisy"
    age: 2
typealias MyCats = Map<String, CatDetails>

@Serializable(with = MyContentBasedSerializer::class)
sealed interface CatDetails {

    @Serializable
    value class InlineName(val name: String) : CatDetails

    @Serializable
    data class More(
        val name: String,
        val age: String?
    ) : CatDetails
}

object MyContentBasedSerializer : YamlContentPolymorphicSerializer<CatDetails>(CatDetails::class) {
    override fun selectDeserializer(node: YamlNode) = when (node) {
        is YamlScalar -> CatDetails.InlineName.serializer()
        is YamlMap -> CatDetails.More.serializer()
        else -> error("Unsupported node type ${node::class.simpleName}")
    }
}

Copy link
Owner

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me why this change is necessary - why can't a contextual polymorphic serializer look at the value of node?

@Jojo4GH
Copy link
Contributor Author

Jojo4GH commented Sep 22, 2024

It's not clear to me why this change is necessary - why can't a contextual polymorphic serializer look at the value of node?

Since YamlContentPolymorphicSerializer uses PolymorphicKind.SEALED (just like the JsonContentPolymorphicSerializer) when selecting an existing serializer that expects a YamlScalar the IllegalStateException("Must call beginStructure() and use returned Decoder") gets thrown erroneously.
Remember that fun selectDeserializer(node: YamlNode): DeserializationStrategy<T> returns not a value but a deserializer.

@charleskorn
Copy link
Owner

Sorry for the delay, thanks @Jojo4GH!

@charleskorn charleskorn merged commit d1dcbb3 into charleskorn:main Oct 29, 2024
Copy link

🎉 This PR is included in version 0.62.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants