jbuilder icon indicating copy to clipboard operation
jbuilder copied to clipboard

Partial on empty associated objects returns [] instead of null

Open develop-test1 opened this issue 9 years ago • 14 comments

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?

develop-test1 avatar Aug 29 '16 14:08 develop-test1

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?

aried3r avatar Oct 03 '16 17:10 aried3r

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.

justin808 avatar Dec 26 '17 08:12 justin808

Any resolution to this matter? Clearly this is incorrect behavior.

JohnMerlino2 avatar Jan 15 '18 23:01 JohnMerlino2

...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 avatar Feb 19 '18 18:02 denishaskin

@denishaskin funny thing we found about this also because mobile app dev were complaining about this :)

develop-test1 avatar Feb 21 '18 13:02 develop-test1

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.

Rabid-Dan avatar Mar 08 '18 20:03 Rabid-Dan

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

Rabid-Dan avatar Mar 08 '18 21:03 Rabid-Dan

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.

sr189 avatar Jun 04 '18 15:06 sr189

+1 This is still an issue

kiddrew avatar Aug 14 '18 04:08 kiddrew

@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.

kiddrew avatar Aug 14 '18 04:08 kiddrew

Still a bug in 2020

toobulkeh avatar Dec 07 '20 15:12 toobulkeh

Still a bug in 2022, has anyone found a patch that could correct this behaviour ?

Ninotd avatar Mar 16 '22 10:03 Ninotd

Is this a workaround?

json.assignee do
  ticket.assignee.blank? ? json.null! : json.partial! 'api/v1/users/user_short', user: ticket.assignee
end 

tanb avatar Jul 27 '22 13:07 tanb

Still a bug in 2023.

dan avatar Sep 11 '23 18:09 dan