Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* [#2754](https://github.com/ruby-grape/grape/pull/2754): Merge routing args in place in `Router#process_route` instead of allocating a new Hash via `merge` - [@ericproulx](https://github.com/ericproulx).
* [#2753](https://github.com/ruby-grape/grape/pull/2753): Lazy-allocate `Grape::Validations::ParamScopeTracker`'s identity-keyed hashes so validating requests that never use the index / qualifying-params trackers allocate no hash - [@ericproulx](https://github.com/ericproulx).
* [#2752](https://github.com/ruby-grape/grape/pull/2752): Skip per-request `ActiveSupport::Notifications` payload and dispatch when no subscriber is listening, via private `instrument_<event>` guards on `Endpoint`/`Middleware::Formatter` - [@ericproulx](https://github.com/ericproulx).
* [#2757](https://github.com/ruby-grape/grape/pull/2757): Build the `Grape::Cookies` jar only when a cookie is read or written (via a new `Grape::Request#cookies?` predicate gating response-cookie flushing), and drop the jar's now-redundant lazy-parse `Proc` - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
8 changes: 2 additions & 6 deletions lib/grape/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Cookies
def_delegators :cookies, :[], :each

def initialize(rack_cookies)
@cookies = rack_cookies
@cookies = ActiveSupport::HashWithIndifferentAccess.new(rack_cookies)
@send_cookies = nil
end

Expand All @@ -37,11 +37,7 @@ def delete(name, **opts)

private

def cookies
return @cookies unless @cookies.is_a?(Proc)

@cookies = ActiveSupport::HashWithIndifferentAccess.new(@cookies.call)
end
attr_reader :cookies

def send_cookies
@send_cookies ||= Set.new
Expand Down
2 changes: 2 additions & 0 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ def build_helpers
end

def build_response_cookies
return unless request.cookies?

response_cookies do |name, value|
cookie_value = value.is_a?(Hash) ? value : { value: }
Rack::Utils.set_cookie_header! header, name, cookie_value
Expand Down
10 changes: 9 additions & 1 deletion lib/grape/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ def headers
end

def cookies
@cookies ||= Grape::Cookies.new(-> { rack_cookies })
@cookies ||= Grape::Cookies.new(rack_cookies)
end

# True once the cookie jar has been materialized (a cookie was read or
# written this request). Lets the endpoint skip response-cookie flushing,
# and the Grape::Cookies allocation it triggers, when no cookie was
# touched on the request.
def cookies?
!@cookies.nil?
end

private
Expand Down
Loading