diff --git a/CHANGELOG.md b/CHANGELOG.md index 34c36baf1..e9ca5aff6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ #### Fixes +* [#1185](https://github.com/ruby-grape/grape/pull/1185): Use formatters for custom vendored content types - [@tylerdooling](https://github.com/tylerdooling). * [#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). * [#1153](https://github.com/ruby-grape/grape/pull/1153): Fixes boolean declaration in an external file - [@towanda](https://github.com/towanda). * [#1142](https://github.com/ruby-grape/grape/pull/1142): Makes #declared unavailable to before filters - [@jrforrest](https://github.com/jrforrest). diff --git a/lib/grape/middleware/formatter.rb b/lib/grape/middleware/formatter.rb index 444a1518d..9b0f32739 100644 --- a/lib/grape/middleware/formatter.rb +++ b/lib/grape/middleware/formatter.rb @@ -167,7 +167,7 @@ def mime_array accept.scan(accept_into_mime_and_quality) .sort_by { |_, quality_preference| -quality_preference.to_f } - .map { |mime, _| mime.sub(vendor_prefix_pattern, '') } + .flat_map { |mime, _| [mime, mime.sub(vendor_prefix_pattern, '')] } end end end diff --git a/spec/grape/middleware/formatter_spec.rb b/spec/grape/middleware/formatter_spec.rb index fd5e40769..313c23631 100644 --- a/spec/grape/middleware/formatter_spec.rb +++ b/spec/grape/middleware/formatter_spec.rb @@ -132,6 +132,18 @@ def to_xml expect(subject.env['api.format']).to eq(:xml) end + context 'with custom vendored content types' do + before do + subject.options[:content_types] = {} + subject.options[:content_types][:custom] = 'application/vnd.test+json' + end + + it 'it uses the custom type' do + subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json') + expect(subject.env['api.format']).to eq(:custom) + end + end + it 'parses headers with symbols as hash keys' do subject.call('PATH_INFO' => '/info', 'http_accept' => 'application/xml', system_time: '091293') expect(subject.env[:system_time]).to eq('091293') @@ -157,6 +169,16 @@ def to_xml _, headers, = subject.call('PATH_INFO' => '/info.custom') expect(headers['Content-type']).to eq('application/x-custom') end + it 'is set for vendored with registered type' do + subject.options[:content_types] = {} + subject.options[:content_types][:custom] = 'application/vnd.test+json' + _, headers, = subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json') + expect(headers['Content-type']).to eq('application/vnd.test+json') + end + it 'is set to closest generic for custom vendored/versioned without registered type' do + _, headers, = subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test+json') + expect(headers['Content-type']).to eq('application/json') + end end context 'format' do