-
Notifications
You must be signed in to change notification settings - Fork 552
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
Skip calling to_hash for nil #602
Conversation
Agreed that this is something that should be fixed in @ashkan18 A word of warning though: this is the type of minor code change that without a comment or test is in serious danger of regressing as someone refactors in the future. Do you think a test to check the behavior would be in order? |
@@ -116,7 +116,7 @@ def as_json(*a) | |||
|
|||
def to_hash | |||
maybe_to_hash = lambda do |value| | |||
value.respond_to?(:to_hash) ? value.to_hash : value | |||
value && value.respond_to?(:to_hash) ? value.to_hash : value |
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.
Maybe change this to !value.nil?
instead of just value
to avoid changing the behavior for non-nil falsey values.
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.
Thanks :) I thought about that, the thing is in case of false assertion of this statement we end up just using the value
and basically not call to_hash
on it which should be fine for any non-nil falsey values but let me know if you can think of any issues 🙌
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.
I always forget that false
and nil
are the only falsey values in Ruby. Should be fine then, as FalseClass
does not have a to_hash
method anyway!
Sure @brandur-stripe, your warning makes total sense, I thought about adding a test but it would be too specific to a scenario where |
@ashkan18 Sorry for the late reply. It looks like your new test is failing on Ruby 2.0.0 because |
Handled in #625. |
Problem
Using
stripe
andrepresentable
where they both overrideto_hash
method, depending on load order of the gems we randomly ran into issues like:When calling
to_hash
on anStripe::Event
object.Solution
While this seems like something that need to be fixed also on
representable
we noticed above issue only happens when trying to callto_hash
onnil
attributes ofStripe::Event
so adding a simple check to see ifvalue
is notnil
then callto_hash
should fix the problem.