Skip to content

Commit f96c682

Browse files
authored
Merge pull request #286 from senid231/coreyward-f/serialized-attribs
Exclude path params from attributes hash
2 parents 3f36f74 + 5fecef5 commit f96c682

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/json_api_client/resource.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,15 @@ def association_for(name)
494494
end
495495
end
496496

497+
def non_serializing_attributes
498+
[
499+
self.class.read_only_attributes,
500+
self.class.prefix_params.map(&:to_s)
501+
].flatten
502+
end
503+
497504
def attributes_for_serialization
498-
attributes.except(*self.class.read_only_attributes).slice(*changed)
505+
attributes.except(*non_serializing_attributes).slice(*changed)
499506
end
500507

501508
def relationships_for_serialization

test/unit/serializing_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class LimitedField < TestResource
66
self.read_only_attributes += ['foo']
77
end
88

9+
class NestedResource < TestResource
10+
belongs_to :bar
11+
end
12+
913
class CustomSerializerAttributes < TestResource
1014

1115
protected
@@ -317,4 +321,19 @@ def test_underscored_relationship_key_serialization
317321
assert_equal expected, article.as_json_api['relationships']
318322
end
319323
end
324+
325+
def test_ensure_nested_path_params_not_serialized
326+
resource = NestedResource.new(foo: 'bar', id: 1, bar_id: 99)
327+
328+
expected = {
329+
'id' => 1,
330+
'type' => "nested_resources",
331+
'attributes' => {
332+
'foo' => 'bar'
333+
}
334+
}
335+
336+
assert_equal expected, resource.as_json_api
337+
end
338+
320339
end

0 commit comments

Comments
 (0)