Skip to content

Commit 0465994

Browse files
Azdarothfotinakis
authored andcommitted
accept only formatted attributes' names for fields (#88)
1 parent 656bc61 commit 0465994

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

lib/jsonapi-serializers/serializer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,11 @@ def should_include_attr?(attribute_name, attr_data)
205205
# Allow "if: :show_title?" and "unless: :hide_title?" attribute options.
206206
if_method_name = attr_data[:options][:if]
207207
unless_method_name = attr_data[:options][:unless]
208+
formatted_attribute_name = format_name(attribute_name).to_sym
208209
show_attr = true
209210
show_attr &&= send(if_method_name) if if_method_name
210211
show_attr &&= !send(unless_method_name) if unless_method_name
211-
show_attr &&= @_fields[type.to_s].include?(attribute_name) if @_fields[type.to_s]
212+
show_attr &&= @_fields[type.to_s].include?(formatted_attribute_name) if @_fields[type.to_s]
212213
show_attr
213214
end
214215
protected :should_include_attr?

spec/serializer_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ def read_attribute_for_validation(attr)
886886
long_comments = [first_comment, second_comment]
887887
post = create(:post, :with_author, long_comments: long_comments)
888888

889-
fields = {posts: 'title,author,long_comments'}
889+
fields = {posts: 'title,author,long-comments'}
890890
serialized_data = JSONAPI::Serializer.serialize(post, fields: fields)
891891
expect(serialized_data['data']['relationships']).to eq ({
892892
'author' => {

0 commit comments

Comments
 (0)