diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index 915cd668..978f6fb3 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -447,8 +447,15 @@ def association_for(name) self.class.associations.detect{|association| association.attr_name == name} end + def non_serializing_attributes + [ + self.class.read_only_attributes, + self.class.prefix_params.map(&:to_s) + ].flatten + end + def attributes_for_serialization - attributes.except(*self.class.read_only_attributes).slice(*changed) + attributes.except(*non_serializing_attributes).slice(*changed) end def relationships_for_serialization diff --git a/test/unit/serializing_test.rb b/test/unit/serializing_test.rb index a5a77287..6956ff26 100644 --- a/test/unit/serializing_test.rb +++ b/test/unit/serializing_test.rb @@ -6,6 +6,10 @@ class LimitedField < TestResource self.read_only_attributes += ['foo'] end + class NestedResource < TestResource + belongs_to :bar + end + class CustomSerializerAttributes < TestResource protected @@ -228,4 +232,18 @@ def test_inherited_attributes_for_serialization assert_equal expected, resource.as_json_api end + def test_ensure_nested_path_params_not_serialized + resource = NestedResource.new(foo: 'bar', id: 1, bar_id: 99) + + expected = { + 'id' => 1, + 'type' => "nested_resources", + 'attributes' => { + 'foo' => 'bar' + } + } + + assert_equal expected, resource.as_json_api + end + end \ No newline at end of file