Skip to content

Exclude path params from attributes hash (rebased) #286

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/json_api_client/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,15 @@ def association_for(name)
end
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
Expand Down
19 changes: 19 additions & 0 deletions test/unit/serializing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class LimitedField < TestResource
self.read_only_attributes += ['foo']
end

class NestedResource < TestResource
belongs_to :bar
end

class CustomSerializerAttributes < TestResource

protected
Expand Down Expand Up @@ -317,4 +321,19 @@ def test_underscored_relationship_key_serialization
assert_equal expected, article.as_json_api['relationships']
end
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