Make method_missing private#614
Merged
Merged
Conversation
`alias_method :method_missing, :set!` was placed after the `private`
directive, but `alias_method` does not respect the modifier, it copies
the visibility of the source method. Since `#set!` is public, the alias
silently made `method_missing` public, overriding the private one
inherited from `BasicObject`.
Add an explicit `private :method_missing` after the alias (above the
`private` directive, for clarity) so that `#method_missing` is properly
private.
As a side effect, `json.method_missing "hello"` now reaches `#set!` with
both a key and a value `set!(:method_missing, "hello")`, like any other
DSL call, and produces `{"method_missing": "hello"}`. Before this patch
the public alias was called directly as `set!("hello")`, a single-arg
call that was silently dropped because `_set_value` short-circuits on a
`BLANK` value, so the key never appeared in the output at all.
Discovered by accident while migrating a Rails app to Jbuilder, where an
object happened to expose `method_missing` as a key and produced
unexpected output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
alias_method :method_missing, :set!was placed after theprivatedirective, butalias_methoddoes not respect the modifier, it copies the visibility of the source method. Since#set!is public, the alias silently mademethod_missingpublic, overriding the private one inherited fromBasicObject.Add an explicit
private :method_missingafter the alias (above theprivatedirective, for clarity) so that#method_missingis properly private.As a side effect,
json.method_missing "hello"now reaches#set!with both a key and a valueset!(:method_missing, "hello"), like any other DSL call, and produces{"method_missing": "hello"}. Before this patch the public alias was called directly asset!("hello"), a single-arg call that was silently dropped because_set_valueshort-circuits on aBLANKvalue, so the key never appeared in the output at all.Discovered by accident while migrating a Rails app to Jbuilder, where an object happened to expose
method_missingas a key and produced unexpected output.