nil-collection should be treated as empty array in template #148
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.
I noticed that when I passed in a nil as an empty collection in a template, it did not treat as an empty collection, although a nil-collection was already treated as an empty array in one place already in JBuilder:
Without this change, if you were to have
json.array! @foos, partial: 'path/to/partial/foo', as: :foo
in index view andjson.(foo, ...)
in partial view, it would use the partial template that will return an error like the following, which is non-obvious; I had no idea that when the collection was nil it would still try to render the partial, so I thought it had to be something else:Instead of treating a nil collection as an empty array, it could raise
ArgumentError
or similar with a more specific error, but the following seems to go against that a little:_map_collection
treating nil collection as array here and test here.set!
(method_missing handling to allow json.* calls in template) seeming to make an assumption here that something that doesn't respond tomap
is a single value and not array. So, I changed to look for:as
option also.Thanks for considering this change. We can work around it by having nil checks in each template, but I don't think that is as obvious.