diff --git a/README.md b/README.md index bb7d88713..13afdc6f0 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,20 @@ end If you find that your project is already relying on the old rails to_json change `render :json` to `render json: @your_object.to_json`. +# Custom MIME Types + +If you're dealing in a custom MIME Type (say, `application/vnd.api+json` or +similar) and you want `ActiveModel::Serializers` to respond with the correct +Content-Type header, just pass the `Mime::Type` object to +`ActiveModel::Serializer.register_mime_type`. If you're registering custom MIME +Types in a Rails initializer, you'll want to make sure the type is registered +first, then send it to `ActiveModel::Serializers`. + +```ruby +Mime::Type.register('application/flergle+json', :flergle) +ActiveModel::Serializer.register_mime_type(Mime::FLERGLE) +``` + # Attributes and Associations Once you have a serializer, you can specify which attributes and associations diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index e2a6228b6..d9ce7af44 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -289,6 +289,19 @@ def build_json(controller, resource, options) serializer.new(resource, options) end + + def register_mime_type(mime_type) + ActionController.add_renderer(mime_type.symbol) do |resource, options| + serialized = ActiveModel::Serializer.build_json(self, resource, options) + + if serialized + self.content_type ||= mime_type + serialized.to_json + else + _render_option_json(resource, options) + end + end + end end attr_reader :object, :options diff --git a/test/serialization_test.rb b/test/serialization_test.rb index 6fe5075c7..a8a8f2dd2 100644 --- a/test/serialization_test.rb +++ b/test/serialization_test.rb @@ -186,6 +186,10 @@ def render_json_empty_array render json: [] end + def render_custom_mime_type_empty_array + render flergle: [] + end + def render_json_array_with_custom_array_serializer render json: [], serializer: CustomArraySerializer end @@ -373,6 +377,15 @@ def test_render_json_empty_array assert_equal '{"test":[]}', @response.body end + def test_render_custom_mime_type_empty_array + Mime::Type.register('application/flergle+json', :flergle) + ActiveModel::Serializer.register_mime_type(Mime::FLERGLE) + + get :render_custom_mime_type_empty_array + assert_equal '{"test":[]}', @response.body + assert_equal 'application/flergle+json', @response.content_type + end + def test_render_json_empty_array_checking_default_root get :render_json_empty_array, check_default_root: true assert_equal '{"awesome":[]}', @response.body