Skip to content

Commit 9c8713d

Browse files
committed
Merge pull request #1185 from tylerdooling/use-custom-formatters
Use formatters for custom vendored content types.
2 parents 9d25e5d + 724cba7 commit 9c8713d

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#### Fixes
1717

18+
* [#1185](https://github.com/ruby-grape/grape/pull/1185): Use formatters for custom vendored content types - [@tylerdooling](https://github.com/tylerdooling).
1819
* [#1156](https://github.com/ruby-grape/grape/pull/1156): Fixed `no implicit conversion of Symbol into Integer` with nested `values` validation - [@quickpay](https://github.com/quickpay).
1920
* [#1153](https://github.com/ruby-grape/grape/pull/1153): Fixes boolean declaration in an external file - [@towanda](https://github.com/towanda).
2021
* [#1142](https://github.com/ruby-grape/grape/pull/1142): Makes #declared unavailable to before filters - [@jrforrest](https://github.com/jrforrest).

lib/grape/middleware/formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def mime_array
167167

168168
accept.scan(accept_into_mime_and_quality)
169169
.sort_by { |_, quality_preference| -quality_preference.to_f }
170-
.map { |mime, _| mime.sub(vendor_prefix_pattern, '') }
170+
.flat_map { |mime, _| [mime, mime.sub(vendor_prefix_pattern, '')] }
171171
end
172172
end
173173
end

spec/grape/middleware/formatter_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ def to_xml
132132
expect(subject.env['api.format']).to eq(:xml)
133133
end
134134

135+
context 'with custom vendored content types' do
136+
before do
137+
subject.options[:content_types] = {}
138+
subject.options[:content_types][:custom] = 'application/vnd.test+json'
139+
end
140+
141+
it 'it uses the custom type' do
142+
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
143+
expect(subject.env['api.format']).to eq(:custom)
144+
end
145+
end
146+
135147
it 'parses headers with symbols as hash keys' do
136148
subject.call('PATH_INFO' => '/info', 'http_accept' => 'application/xml', system_time: '091293')
137149
expect(subject.env[:system_time]).to eq('091293')
@@ -157,6 +169,16 @@ def to_xml
157169
_, headers, = subject.call('PATH_INFO' => '/info.custom')
158170
expect(headers['Content-type']).to eq('application/x-custom')
159171
end
172+
it 'is set for vendored with registered type' do
173+
subject.options[:content_types] = {}
174+
subject.options[:content_types][:custom] = 'application/vnd.test+json'
175+
_, headers, = subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
176+
expect(headers['Content-type']).to eq('application/vnd.test+json')
177+
end
178+
it 'is set to closest generic for custom vendored/versioned without registered type' do
179+
_, headers, = subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json')
180+
expect(headers['Content-type']).to eq('application/json')
181+
end
160182
end
161183

162184
context 'format' do

0 commit comments

Comments
 (0)