Skip to content

Commit

Permalink
Merge pull request #8853 from zmoazeni/3-0-xml-serialization-fix
Browse files Browse the repository at this point in the history
Methods that return nil should not be considered YAML
  • Loading branch information
carlosantoniodasilva committed Jan 9, 2013
2 parents ca8b0bd + 477f0e7 commit 583e5fd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions activemodel/CHANGELOG
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,7 @@
## Rails 3.0.20 (unreleased)

* Fix XML serialization of methods that return nil to not be considered as YAML (GH #8853 and GH #492)

## Rails 3.0.18 ## Rails 3.0.18


## Rails 3.0.17 (Aug 9, 2012) ## Rails 3.0.17 (Aug 9, 2012)
Expand Down
1 change: 1 addition & 0 deletions activemodel/lib/active_model/serializers/xml.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def decorations
protected protected


def compute_type def compute_type
return if value.nil?
type = ActiveSupport::XmlMini::TYPE_NAMES[value.class.name] type = ActiveSupport::XmlMini::TYPE_NAMES[value.class.name]
type ||= :string if value.respond_to?(:to_str) type ||= :string if value.respond_to?(:to_str)
type ||= :yaml type ||= :yaml
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def setup
assert_match %r{<name>aaron stack</name>}, @contact.to_xml assert_match %r{<name>aaron stack</name>}, @contact.to_xml
end end


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

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


def pseudonyms
nil
end

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


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


Expand Down

0 comments on commit 583e5fd

Please sign in to comment.