Skip to content

has_many_association: Deal with empty 204 status code#540

Open
pedrolucasp wants to merge 1 commit intoremi:masterfrom
pedrolucasp:master
Open

has_many_association: Deal with empty 204 status code#540
pedrolucasp wants to merge 1 commit intoremi:masterfrom
pedrolucasp:master

Conversation

@pedrolucasp
Copy link
Copy Markdown

If you register a relationship as a has_many and you this resource
endpoint return a 204 we should handle this gracefully, by returning a
empty array. This patch leverages the default return type defined on the
relationship (@opts[:default]) to construct which sort of response it
would expect (get_collection or a regular get).

The default parser, is dumb and therefore cannot distinguish if when it
receives a 204 it should transform that into a hash or an array, and
always convert the response into a empty hash. So, on the
attributes.rb method called instantiate_collection when we stumble
upon a empty hash we always convert into an empty array.

If you register a relationship as a `has_many` and you this resource
endpoint return a 204 we should handle this gracefully, by returning a
empty array. This patch leverages the default return type defined on the
relationship (`@opts[:default]`) to construct which sort of response it
would expect (`get_collection` or a regular `get`).

The default parser, is dumb and therefore cannot distinguish if when it
receives a 204 it should transform that into a hash or an array, and
always convert the response into a empty hash. So, on the
attributes.rb method called `instantiate_collection` when we stumble
upon a empty hash we always convert into an empty array.
@pedrolucasp
Copy link
Copy Markdown
Author

Hello, just a quick ping on this PR for review @remi @zacharywelch :)

voltechs added a commit to TwilightCoders/her that referenced this pull request Mar 31, 2026
Ref: remi#540

Route has_many fetches through get_collection instead of get, and
guard instantiate_collection against blank response data. This
prevents crashes when an API returns 204 No Content for an
association endpoint.
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