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

Serialized methods that return nil should not be considered YAML #200

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions activemodel/lib/active_model/serializers/xml.rb
Expand Up @@ -33,6 +33,7 @@ def decorations
protected

def compute_type
return if value.nil?
type = ActiveSupport::XmlMini::TYPE_NAMES[value.class.name]
type ||= :string if value.respond_to?(:to_str)
type ||= :yaml
Expand Down
Expand Up @@ -92,6 +92,10 @@ def setup
test "should serialize string" do
assert_match %r{<name>aaron stack</name>}, @contact.to_xml
end

test "should serialize nil" do
assert_match %r{<pseudonyms nil=\"true\"></pseudonyms>}, @contact.to_xml(:methods => :pseudonyms)
end

test "should serialize integer" do
assert_match %r{<age type="integer">25</age>}, @contact.to_xml
Expand Down
4 changes: 4 additions & 0 deletions activemodel/test/models/contact.rb
Expand Up @@ -16,6 +16,10 @@ def initialize(options = {})
options.each { |name, value| send("#{name}=", value) }
end

def pseudonyms
nil
end

def persisted?
id
end
Expand Down
5 changes: 1 addition & 4 deletions activerecord/test/cases/xml_serialization_test.rb
Expand Up @@ -143,10 +143,7 @@ def test_should_serialize_boolean
end

def test_should_serialize_yaml
assert %r{<preferences(.*)></preferences>}.match(@xml)
attributes = $1
assert_match %r{type="yaml"}, attributes
assert_match %r{nil="true"}, attributes
assert_match %r{<preferences nil=\"true\"></preferences>}, @xml
end
end

Expand Down