Fix raw TypeError/NoMethodError when reopening array or null scope with a block - #619
Open
chatman-media wants to merge 1 commit into
Open
Fix raw TypeError/NoMethodError when reopening array or null scope with a block#619chatman-media wants to merge 1 commit into
chatman-media wants to merge 1 commit into
Conversation
…th a block
json.foo { ... } on a scope that's currently an array (after array!) or
null (after null!) was leaking a raw Ruby TypeError or NoMethodError
instead of the library's own ArrayError/NullError, because _merge_block
called @attributes.fetch before checking what @attributes actually was.
The non-block form (json.foo "bar") already handled this correctly via
_set_value, so brought _merge_block in line with it. Added tests for
both cases.
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.
Ran into this while poking at edge cases:
json.array! [1,2,3]followed byjson.foo { json.bar "x" }blows up with a rawTypeError: no implicit conversion of String into Integerinstead of the library's ownJbuilder::ArrayError. Same story afterjson.null!— you getNoMethodError: undefined method 'fetch' for nilinstead ofJbuilder::NullError.The non-block form (
json.foo "bar") already gets this right through_set_value, which checks whether@attributesis an array or nil before touching it._merge_block(used for the block form) skipped straight to@attributes.fetch(...), so it never got a chance to raise the friendly error - Ruby's own array/nil methods raised first. Added the same checks there, in the same order_set_valueuses, and added two tests covering both cases.