Skip to content

Fix raw TypeError/NoMethodError when reopening array or null scope with a block - #619

Open
chatman-media wants to merge 1 commit into
rails:mainfrom
chatman-media:fix-merge-block-array-null-error
Open

Fix raw TypeError/NoMethodError when reopening array or null scope with a block#619
chatman-media wants to merge 1 commit into
rails:mainfrom
chatman-media:fix-merge-block-array-null-error

Conversation

@chatman-media

Copy link
Copy Markdown

Ran into this while poking at edge cases: json.array! [1,2,3] followed by json.foo { json.bar "x" } blows up with a raw TypeError: no implicit conversion of String into Integer instead of the library's own Jbuilder::ArrayError. Same story after json.null! — you get NoMethodError: undefined method 'fetch' for nil instead of Jbuilder::NullError.

The non-block form (json.foo "bar") already gets this right through _set_value, which checks whether @attributes is 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_value uses, and added two tests covering both cases.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant