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 JSON::Serializable on certain recursively defined types #13344

Merged

Conversation

HertzDevil
Copy link
Contributor

Fixes #13337. Does not affect #13238 (comment).

I couldn't reproduce the same error with YAML

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:serialization kind:regression Something that used to correctly work but no longer works labels Apr 18, 2023
Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

I think this removes all uses of value[:type]. So we can remove that from the preparation macro loop.

And even though the issue does not reproduce with YAML, I'd still like to add the same spec there to make sure of it.

src/json/serialization.cr Outdated Show resolved Hide resolved
@straight-shoota straight-shoota added this to the 1.8.1 milestone Apr 18, 2023
class JSONSomething
include JSON::Serializable

property value : JSONAttrValue(Set(JSONSomethingElse)?)?
Copy link
Contributor

Choose a reason for hiding this comment

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

JSONAttrValue() wrapper is not required here for the issue to manifest. Why it was added? Doesn't that add possibility to hide the issue in the future (for some other bugs)?

Copy link
Member

Choose a reason for hiding this comment

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

Good catch. The spec setup can actually be simplified a bit:

--- i/spec/std/json/serializable_spec.cr
+++ w/spec/std/json/serializable_spec.cr
@@ -468,13 +468,7 @@ end
 class JSONSomething
   include JSON::Serializable

-  property value : JSONAttrValue(Set(JSONSomethingElse)?)?
-end
-
-class JSONSomethingElse
-  include JSON::Serializable
-
-  property value : JSONAttrValue(Set(JSONSomethingElse)?)?
+  property value : JSONSomething?
 end

 describe "JSON mapping" do
@@ -1122,6 +1116,6 @@ describe "JSON mapping" do

   it "fixes #13337" do
     JSONSomething.from_json(%({"value":{}})).value.should_not be_nil
-    JSONSomethingElse.from_json(%({"value":{}})).value.should_not be_nil
+    JSONAttrValue(JSONSomething).from_json(%({"value":{}})).value.should_not be_nil
   end
 end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IIRC this wouldn't reproduce the bug without this patch

Copy link
Member

Choose a reason for hiding this comment

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

No I checked that it actually does reproduce.

Copy link
Member

Choose a reason for hiding this comment

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

I'm still going to merge this as it to move along with 1.8.1.
We can refactor the specs afterwards.

Copy link
Member

Choose a reason for hiding this comment

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

@straight-shoota straight-shoota merged commit d3f5d75 into crystal-lang:master Apr 19, 2023
@HertzDevil HertzDevil deleted the bug/json-serializable-recursive branch April 20, 2023 19:52
{% for name, value in properties %}
%var{name} = {% if value[:has_default] || value[:nilable] %} nil {% else %} uninitialized ::Union({{value[:type]}}) {% end %}
%var{name} = {% if value[:has_default] || value[:nilable] %}
Copy link
Contributor

@kostya kostya Apr 21, 2023

Choose a reason for hiding this comment

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

btw, I think condition :nilable here can be deleted, and only remains :has_default, not checked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. 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.

Crystal 1.8.0 JSON serializer is acting crazy - properties are silently ignored / set to nil
4 participants