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

Fix incorrect behavior while deserializing maps to sealed classes #2052

Merged
merged 7 commits into from Oct 13, 2022
Merged

Fix incorrect behavior while deserializing maps to sealed classes #2052

merged 7 commits into from Oct 13, 2022

Conversation

rodrigovedovato
Copy link
Contributor

This is an initial attempt at fixing an incompatibility between the deserialization of sealed classes when using json vs map

fix: #2035

By reusing some of the stuff from StreamingJsonDecoder I was able to succesfully deserialize a map that has the same structure as the test json.

Am I in the right path?

@rodrigovedovato rodrigovedovato marked this pull request as draft October 10, 2022 15:41
Copy link
Member

@sandwwraith sandwwraith left a comment

Choose a reason for hiding this comment

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

Hi, thanks for the PR. Yeah, I think that's the right way to solve the problem; it just needs more polishing.

Please also try to fix this problem for serializing to map, so this functionality won't be half-baked.

@rodrigovedovato
Copy link
Contributor Author

Hey, @sandwwraith! Thanks for the comments

Yeah, it definitely needs more polishing. I'll work on those things you mentioned and I'll let you know when it's ready for revision 👍

@rodrigovedovato rodrigovedovato marked this pull request as ready for review October 11, 2022 01:42
@rodrigovedovato
Copy link
Contributor Author

rodrigovedovato commented Oct 11, 2022

Hey, @sandwwraith! I think I managed to do everything you suggested, but I have a doubt concerning the serialization process.

While it was possible to make it work by creating another method especifically for that, I think the best option would be using the built-in encodeSerializableValue. However, AbstractPolymorphicSerializer's upper bound is stricter than the one encodeSerializableValue, which allows null values, making it impossible to call findPolymorphicSerializerOrNull using the value directly. I tried changing encodeSerializableValue to work exclusively with non-nullable values, but it didn't go well.

Do you think there's a better approach to solve this?

@sandwwraith
Copy link
Member

It seems that JSON just ignores nullability:

val actualSerializer = casted.findPolymorphicSerializer(this, value as Any)

It makes sense, because AbstractPolymorphicSerializer does not support nullable values (they're handled mainly via NullableSerializer). So, if deserializer is AbstractPolymorphicSerializer, you can safely use value as Any

@rodrigovedovato
Copy link
Contributor Author

rodrigovedovato commented Oct 12, 2022

I think it's done, @sandwwraith.

Copy link
Member

@sandwwraith sandwwraith left a comment

Choose a reason for hiding this comment

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

Thanks a lot for your contribution!

@sandwwraith sandwwraith merged commit 0f35682 into Kotlin:dev Oct 13, 2022
@rodrigovedovato rodrigovedovato deleted the incorrect-map-sealed-deserialize branch October 13, 2022 15:19
@rodrigovedovato
Copy link
Contributor Author

Glad I could help, @sandwwraith!

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.

None yet

2 participants