ruby-grape / ruby-grape/grape

Access response_object/@body return in after filter

Open
#1,695 11 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

I am attempting to implement API versioning ala Stripe (https://stripe.com/blog/api-versioning).

I currently have a working prototype that hooks into Grape before/after hooks – but to modify the response I have to reparse and regenerate the JSON from response.body – when obviously I would prefer to hook in earlier and muck with the hash before it's jsonified – it also means I have to correct the headers e.g. Content-Length, if I want those to be correct.

I see in https://github.com/ruby-grape/grape/blob/master/lib/grape/endpoint.rb#L262-L265 you now assign response_object to @body but not until after the after hooks have fired.

Is there a way for me to get at this?
Is there a reason I shouldn't?
Is there a reason @body shouldn't be assigned sooner?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/grape/endpoint.rb around lines 262-265 and trace how response_object, @body, and the before/after hooks are ordered. Determine whether the after hook can expose the pre-JSONified response safely, then verify that the resulting response body and headers remain consistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.