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 as_json
call on ActiveModel::Type's child classes.
#46535
Conversation
as_json
on ActiveModel::Type's child classes.as_json
call on ActiveModel::Type's child classes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the use case for calling as_json
? Note that Type::Value
and its descendents represent types, not values.
@jonathanhefner I noticed that something changed when we tried to upgrade Rails version in specs for |
@nashby Can you tell me why { attr: "my_attribute", subtype: ActiveRecord::Type.lookup(:boolean) }.as_json
# => {"attr"=>"my_attribute", "subtype"=>{"precision"=>nil, "scale"=>nil, "limit"=>nil}}
{ attr: "my_attribute", subtype: ActiveRecord::Type.lookup(:float) }.as_json
# => {"attr"=>"my_attribute", "subtype"=>{"precision"=>nil, "scale"=>nil, "limit"=>nil}} More to the point, to the best of my knowledge, |
@jonathanhefner honestly I don't really remember a real use-case for it in |
Thank you for the pull request @nashby and nice to see you contributing again to Rails! I don't think this object should respond to to_json, so maybe we should explicitly call |
@rafaelfranca hey hey! If "undefing" is common practice for Rails then it probably makes sense. I can change this PR is it's a way we choose to proceed |
Yes. It is fine to use |
@rafaelfranca I thought about it a bit now and not sure if it's ok to potentially break someone's app with this |
I think the point is that right now the application is already broken, only silently broken given the example that @jonathanhefner gave: { attr: "my_attribute", subtype: ActiveRecord::Type.lookup(:boolean) }.as_json
# => {"attr"=>"my_attribute", "subtype"=>{"precision"=>nil, "scale"=>nil, "limit"=>nil}}
{ attr: "my_attribute", subtype: ActiveRecord::Type.lookup(:float) }.as_json
# => {"attr"=>"my_attribute", "subtype"=>{"precision"=>nil, "scale"=>nil, "limit"=>nil}} I don't think we should be returning anything is this object and the occasional breakage that this will cause to people is actually a good thing. They will realize their code isn't doing anything meaningful and will have to adapt. Of course I'd not backport this to a stable brach. If we need to do something in a stable branch, I'd go with this PR as it is right now. But for main, I think we should explicitly say to people to not try to convert a type to json because it isn't a operation you can roundtrip. |
@rafaelfranca alright, it makes sense, thanks for explanation. I've updated the PR. |
Ah, just saw the tests failed. It makes sense since |
Right now since we have instance variable called `itself_if_serialize_cast_value_compatible` assigned to self when we run `as_json` we get stack too deep error because `as_json` calls `as_json` on every instance variable. And since `@itself_if_serialize_cast_value_compatible` references to `self` we run into recursion. And before that we were returning unpredictable data from `as_json` method so it's better to let it to throw an error and let user know that Value class is not supposed to be converted to json.
@rafaelfranca it's updated now |
This broke after the upgrade to rails 7.1 because of rails/rails#46535
Motivation / Background
Right now since we have instance variable called
itself_if_serialize_cast_value_compatible
assigned to self when we runas_json
we get stack too deep error becauseas_json
callsas_json
on every instance variable. And since@itself_if_serialize_cast_value_compatible
references toself
we run into recursion.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]