Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Serialize Discriminator properties as Strings
Browse files Browse the repository at this point in the history
Previously, Discriminator properties caused problems for ActiveSupport
to_json, as their values are generally DataMapper::Model instances,
which implement as_json as an array of all the models of that type.
This caused an ActiveSupport::JSON::Encoding::CircularReferenceError.

[#1421 state:resolved]
  • Loading branch information
gcampbell committed Sep 30, 2010
1 parent 64464a0 commit 3201e94
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/dm-serializer/to_json.rb
Expand Up @@ -21,7 +21,13 @@ def as_json(options = {})

properties_to_serialize(options).each do |property|
property_name = property.name
result[property_name] = __send__(property_name)
value = __send__(property_name)
result[property_name] = case property
when DataMapper::Property::Discriminator
value.to_s
else
value
end
end

# add methods
Expand Down
14 changes: 14 additions & 0 deletions spec/fixtures/vehicle.rb
@@ -0,0 +1,14 @@
class Vehicle
include DataMapper::Resource
property :id, Serial
property :name, String
property :type, Discriminator
end

class Car < Vehicle

end

class Motorcycle < Vehicle

end
4 changes: 4 additions & 0 deletions spec/public/to_json_spec.rb
Expand Up @@ -68,4 +68,8 @@ def deserialize(result)
it "handles nil for options" do
expect { Cow.new.as_json(nil) }.to_not raise_error
end

it "serializes Discriminator types as strings" do
Motorcycle.new.as_json[:type].should == "Motorcycle"
end
end

0 comments on commit 3201e94

Please sign in to comment.