jbuilder
jbuilder copied to clipboard
Partial on empty associated objects returns [] instead of null
When I'm trying to use partials to render associated object and the association is nil the response object is an empty array [] instead of null (like on standard attribute fields). This is inconsistent, and actually it proved to make it very hard for clients using strong typed programming languages to interface with our API backend.
json.assignee ticket.assignee, partial: 'api/v1/users/user_short', as: :user
If ticket.assignee is empty view returns: [] instead of: null
I've been using this workaround to make it work as expected:
ticket.assignee.present? ? json.assignee(ticket.assignee, partial: 'api/v1/users/user_short', as: :user) : json.user(nil)
However this is not the pretties solution and it requires defining everywhere nil association is to be expected.
Also please note that is only present with the partial option. Returning associated objects directly works as expected (object or null value if not present), however on very huge associated objects use of partials is a must.
Is there any configuration option or other way to have jbuilder return null values on empty association objects using partials?
Sadly I can't help you, but I just checked with my own code and face the same problem. Even array! returns an empty list instead of omitting the field (using ignore_nil!).
Is that the specified behavior?
you can get around this with
json.assignee do
json.partial! 'api/v1/users/user_short', user: ticket.assignee
end
Still, this is a bug.
Any resolution to this matter? Clearly this is incorrect behavior.
...crickets...
I have mobile app developers consuming my APIs making fun of me because of this bug. Maybe I'll dive in and see if I can fix.
@denishaskin funny thing we found about this also because mobile app dev were complaining about this :)
I was looking into submitting a bug fix for this but it looks like the behaviour is by design. There's a test for it in jbuilder_template_test.rb:
test "render as empty array if partials as a nil value" do
result = jbuild <<-JBUILDER
json.posts nil, partial: "blog_post", as: :blog_post
JBUILDER
assert_equal [], result["posts"]
end
I'm happy to continue with a branch that changes the behaviour but it'd be nice to get some more opinions on why this approach may be better or worse.
Did some sleuthing and more context is available in the original PR that the changes were introduced in https://github.com/rails/jbuilder/pull/148
This behaviour makes sense for arrays but not for associations with a single object :/
Has anyone found a working workaround? The workaround from @justin808 did not work for me since it tried to call the partial which failed because the object was nil.
+1 This is still an issue
@justin808's solution doesn't work for me. I get a undefined method 'id' for nil:NilClass within the partial. I worked around this by adding a conditional in the template, which is a hack. Rendering an empty array for a null object is clearly not correct behavior.
Still a bug in 2020
Still a bug in 2022, has anyone found a patch that could correct this behaviour ?
Is this a workaround?
json.assignee do
ticket.assignee.blank? ? json.null! : json.partial! 'api/v1/users/user_short', user: ticket.assignee
end
Still a bug in 2023.