-
Notifications
You must be signed in to change notification settings - Fork 204
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 fallback argument for Psych.load #339
Conversation
Add more test cases for the fallback argument of Psych.load_file and fix an error in the docs.
This allows calling Psych.load with a fallback argument, similar to Psych.load_file. Before, for Psych.load this caused a "NoMethodError: undefined method `to_ruby'".
Note that my change was backward compatible unlike this change. This interface change (wrapping Psych::FALLBACK implicitly) is breaking. If you really want this kind of breaking change (probably acceptable as major upgrade if we yank 3.0.0), my suggestion is using keyword arguments for fallback too instead. It would be scalable for future extension. |
I thought about this some more, and am even more convinced that the way the new option was implemented is wrong. It should not make any difference whether a yaml file is loaded using This PR does not break backwards compatibility, it fixes this bug. On the other hand, keeping the new option (symbolize_names or symbolize_keys) the way it is would effectively cement this wrong behaviour and prevent fixing this bug for a long time, and additionally increase the differences between the two methods even further (both methods should have---or not have---that option). |
@@ -260,7 +260,7 @@ module Psych | |||
# Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"} | |||
# | |||
def self.load yaml, filename = nil, fallback = false, symbolize_names: false |
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.
How about changing "fallback = false" to "fallback: false"? With that change, users can easily notice fallback's behavior is fixed/changed.
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.
Or another idea is having "legacy_fallback = false, fallback: false" and keeping legacy_fallback's behavior for migration, deprecating legacy_fallback usage.
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.
fallback: false sounds good to me, if the compatibility break is acceptable and if this doesn't cause problems in any of the other methods that possibly should be considered, too (safe_load, load_stream, and so on).
legacy_fallback doesn't convince me, I would guess it has the exact same problem with hash arguments; also I think fallback is not widely used currently.
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.
fallback: false sounds good to me, if the compatibility break is acceptable and if this doesn't cause problems in any of the other methods that possibly should be considered, too (safe_load, load_stream, and so on).
I agree with it. That should depend on maintainer's decision anyway.
Since the fallback argument was in the meantime changed to a keyword argument, closing in favour of
|
[WIP: depends on a solution to #340, see also below]
@hsbt I think this should be solved in the upcoming 3.0 release.
There is an error in the implementation of the fallback argument for Psych.load which causes an exception; it only works with Psych.load_file currently.
Before the fix:
However, it shouldn't make a difference whether the loaded yaml is read from a file or a string, so
load
andload_file
should behave in the same way and both (or none) accept a fallback argument.After the fix:
Problem: This will not work for hashes currently, because of the (newly introduced)
symbolize_names
keyword argument for Psych.load. The keyword argument does not play well with the optional arguments. The provided hash is not parsed as the value for the optionalfallback
argument, unless the keyword argument is provided, too (see #340).(This is the reason for the current test fails.)
BTW. @tenderlove mentioned in #149 and #264 that a default return value of
nil
might make more sense, but this could only be changed with a major release. 3.0?