Skip to content

Partial on empty associated objects returns [] instead of null #350

Open
@develop-test1

Description

@develop-test1

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions