Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@
* [#2902](https://github.com/ruby-grape/grape/pull/2902): Make `Grape::ErrorFormatter.formatter_for` a lookup that answers `nil` for an unregistered format, like `Grape::Parser.parser_for`, and move the `default_error_formatter` / `Grape::ErrorFormatter::Txt` fallback to `Grape::Middleware::Error`, which owns it; `default_error_formatter` naming nothing registered now raises `Grape::Exceptions::UnknownErrorFormatter` instead of silently storing `Txt` (see UPGRADING) - [@ericproulx](https://github.com/ericproulx).
* [#2904](https://github.com/ruby-grape/grape/pull/2904): Document and spec route `requirements` given as a Mustermann capture type, which constrains the match and hands the endpoint the converted value - [@ericproulx](https://github.com/ericproulx).
* [#2905](https://github.com/ruby-grape/grape/pull/2905): Nest any `route_param` requirement under the param name, not just a Regexp, and reject the `requirements` shapes that have no param to attach to where they are written rather than on the first request - [@ericproulx](https://github.com/ericproulx).
* [#2908](https://github.com/ruby-grape/grape/pull/2908): Stop reassigning method parameters across `lib`, so a parameter keeps the value its caller passed for the whole method; `Grape::Middleware::Formatter#ensure_content_type` and the `oneof` collection in `Grape::Validations::ParamsScope` no longer write into the Hash they were given - [@ericproulx](https://github.com/ericproulx).
* [#2908](https://github.com/ruby-grape/grape/pull/2908): Stop reassigning method parameters across `lib`, so a parameter keeps the value its caller passed for the whole method; the `oneof` collection in `Grape::Validations::ParamsScope` no longer writes into the Hash it was given - [@ericproulx](https://github.com/ericproulx).
* [#2910](https://github.com/ruby-grape/grape/pull/2910): Restore `Grape::Middleware::Formatter`'s in-place content-type negotiation as `ensure_content_type!`, which #2908 had turned into a copy of the response headers on every response - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

### 3.3.5 (2026-07-30)
Expand Down
29 changes: 15 additions & 14 deletions lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,25 @@ def after
private

def build_formatted_response(status, headers, bodies)
typed_headers = ensure_content_type(headers)
ensure_content_type!(headers)

if bodies.is_a?(Grape::ServeStream::StreamResponse)
Grape::ServeStream::SendfileResponse.new([], status, typed_headers) do |resp|
Grape::ServeStream::SendfileResponse.new([], status, headers) do |resp|
resp.body = bodies.stream
end
else
# Allow content-type to be explicitly overwritten
formatter = fetch_formatter(typed_headers)
formatter = fetch_formatter(headers)
bodymap = instrument_format_response(formatter) do
bodies.map { |body| formatter.call(body, env) }
end
# A bare Rack tuple rather than a Rack::Response: +typed_headers+ is already
# A bare Rack tuple rather than a Rack::Response: +headers+ is already
# a Grape::Util::Header (a Rack::Headers on Rack 3), so wrapping only
# re-normalizes the same keys into a second Headers hash that
# +Middleware::Base#call+ unwraps again with +to_a+ on the way out.
# The 204/304 bodies Rack::Response#finish would blank are returned
# above, before this point.
[status, typed_headers, bodymap]
[status, headers, bodymap]
end
rescue Grape::Exceptions::InvalidFormatter => e
throw :error, Grape::Exceptions::ErrorResponse.new(status: 500, message: e.message, backtrace: e.backtrace, original_exception: e)
Expand All @@ -96,15 +96,16 @@ def fetch_formatter(headers)

# Set the content type header for the API format if it is not already present.
#
# @param headers [Hash]
# @return [Hash]
def ensure_content_type(headers)
return headers if headers[Rack::CONTENT_TYPE]

# Merged rather than written in place: +headers+ belongs to the response
# the app returned, and negotiating a content type for it is not a reason
# to reach back into it.
headers.merge(Rack::CONTENT_TYPE => content_type_for(env[Grape::Env::API_FORMAT]))
# Written into +headers+ rather than returned as a copy, hence the +!+:
# this runs on every response, and copying the hash costs an allocation
# per request for a header the caller is about to send anyway.
#
# @param headers [Hash] the response headers, mutated in place
# @return [void]
def ensure_content_type!(headers)
return if headers[Rack::CONTENT_TYPE]

headers[Rack::CONTENT_TYPE] = content_type_for(env[Grape::Env::API_FORMAT])
end

def read_body_input
Expand Down
Loading