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

Fixup for JSON::Serializable on certain recursively defined types #13430

Merged
merged 2 commits into from
May 5, 2023

Conversation

kostya
Copy link
Contributor

@kostya kostya commented May 4, 2023

i don't know, correct this or not, but if fixes, and all specs seems pass. fixes: #13429

@Blacksmoke16 Blacksmoke16 added kind:bug topic:stdlib:serialization kind:regression Something that used to correctly work but no longer works labels May 4, 2023
@straight-shoota
Copy link
Member

This essentially reverts part of #13344 which apparently wasn't necessary? I suppose the relevant part of that PR was casting the type of the default nil value?

It's not exactly clear why this fixes things. The compiler is doing some weird stuff here.

@kostya
Copy link
Contributor Author

kostya commented May 4, 2023

yes looks like compiler bug when using typeof. I will add for yaml too, if its ok.

Copy link
Contributor

@HertzDevil HertzDevil left a comment

Choose a reason for hiding this comment

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

My guess is typeof(@source).new(pull) somehow instantiates Base+.new which breaks things, while Union(typeof(@source)) devirtualizes the metaclass type.

YAML would be nice.

spec/std/json/serializable_spec.cr Show resolved Hide resolved
@@ -202,9 +203,9 @@ module JSON
# recursively defined serializable types
{% for name, value in properties %}
%var{name} = {% if value[:has_default] || value[:nilable] %}
nil.as(::Nil | typeof(@{{name}}))
nil.as(::Nil | Union({{value[:type]}}))
Copy link
Contributor

@HertzDevil HertzDevil May 4, 2023

Choose a reason for hiding this comment

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

Suggested change
nil.as(::Nil | Union({{value[:type]}}))
nil.as(::Union(::Nil, typeof(@{{name}})))

For the others simply wrap the typeof alone inside ::Union (also note the leading ::)

HertzDevil
HertzDevil approved these changes May 4, 2023
@straight-shoota straight-shoota modified the milestones: 1.9.0, 1.8.2 May 4, 2023
@oprypin oprypin changed the title fix json recursive bug, fixes #13429 Fix json recursive bug May 4, 2023
@oprypin
Copy link
Member

oprypin commented May 4, 2023

I am also observing some JSON bug, possibly slightly different, in Crystal 1.8.1.
https://github.com/oprypin/crystal-imgui/actions/runs/4771237772/jobs/8483033672
And I confirm that this PR fixes it.

@straight-shoota straight-shoota changed the title Fix json recursive bug Fixup for JSON::Serializable on certain recursively defined types May 5, 2023
@straight-shoota straight-shoota merged commit 404726e into crystal-lang:master May 5, 2023
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug kind:regression Something that used to correctly work but no longer works topic:stdlib:serialization
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Another recursive json bug, after crystal 1.8
5 participants