Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for xml #1133

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 7 additions & 1 deletion lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ module Serialization

include ActionController::Renderers

RENDERER_METHODS = [
:_render_option_json,
:_render_with_renderer_json,
:_render_option_xml,
:_render_with_renderer_xml
]
# Deprecated
ADAPTER_OPTION_KEYS = ActiveModel::SerializableResource::ADAPTER_OPTION_KEYS

Expand Down Expand Up @@ -45,7 +51,7 @@ def use_adapter?
true
end

[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
RENDERER_METHODS.each do |renderer_method|
define_method renderer_method do |resource, options|
options.fetch(:context) { options[:context] = request }
serializable_resource = get_serializer(resource, options)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def as_json(options = nil)
hash
end

def to_xml(options = nil)
as_json(options).to_xml
end

def fragment_cache(*args)
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
end
Expand Down
15 changes: 15 additions & 0 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ def render_fragment_changed_object_with_relationship
render json: like
end

def render_object_xml
profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
with_adapter :flatten_json do
render xml: profile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would work nicely, name-wise, with #1117

end
end

private

def generate_cached_serializer(obj)
Expand Down Expand Up @@ -420,6 +427,14 @@ def use_adapter?
controller.get_serializer(Profile.new)
})
end

def test_render_object_xml
get :render_object_xml

hash = Hash.from_xml(@response.body).each_value.first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_equal('application/xml', @response.content_type)
assert_equal({ 'name' => 'Name 1', 'description' => 'Description 1' }, hash)
end
end
end
end