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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#### Fixes

* [#2588](https://github.com/ruby-grape/grape/pull/2588): Fix defaut format regression on */* - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

### 2.4.0 (2025-06-18)
Expand Down
4 changes: 3 additions & 1 deletion lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Formatter < Base
default_format: :txt
}.freeze

ALL_MEDIA_TYPES = '*/*'

def before
negotiate_content_type
read_body_input
Expand Down Expand Up @@ -138,7 +140,7 @@ def format_from_extension

def format_from_header
accept_header = env['HTTP_ACCEPT'].try(:scrub)
return if accept_header.blank?
return if accept_header.blank? || accept_header == ALL_MEDIA_TYPES

media_type = Rack::Utils.best_q_match(accept_header, mime_types.keys)
mime_types[media_type] if media_type
Expand Down
8 changes: 6 additions & 2 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
context 'default format' do
let(:body) { ['foo'] }
let(:env) do
{ Rack::PATH_INFO => '/somewhere', 'HTTP_ACCEPT' => 'application/json' }
{ Rack::PATH_INFO => '/somewhere', 'HTTP_ACCEPT' => '*/*' }
end

it 'calls #to_json since default format is json' do
before do
subject.options[:default_format] = :json
end

it 'returns JSON' do
body.instance_eval do
def to_json(*_args)
'"bar"'
Expand Down