Skip to content

Commit 74bb9d6

Browse files
authored
Merge pull request Netflix#339 from mhluska/fix-params-not-being-default
Fix params not being hash by default
2 parents ece607a + d5ea953 commit 74bb9d6

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

lib/fast_jsonapi/object_serializer.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require 'active_support/time'
34
require 'active_support/json'
45
require 'active_support/concern'
56
require 'active_support/inflector'
@@ -72,6 +73,7 @@ def serialized_json
7273

7374
def process_options(options)
7475
@fieldsets = deep_symbolize(options[:fields].presence || {})
76+
@params = {}
7577

7678
return if options.blank?
7779

spec/lib/object_serializer_attribute_param_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def viewed?(user)
1515

1616
class MovieSerializer
1717
attribute :viewed do |movie, params|
18-
params ? movie.viewed?(params[:user]) : false
18+
params[:user] ? movie.viewed?(params[:user]) : false
1919
end
2020

2121
attribute :no_param_attribute do |movie|

spec/lib/object_serializer_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,10 @@ class BlahSerializer
504504
end
505505
end
506506
end
507+
508+
context 'when attribute contents are determined by params data' do
509+
it 'does not throw an error with no params are passed' do
510+
expect { MovieOptionalAttributeContentsWithParamsSerializer.new(movie).serialized_json }.not_to raise_error
511+
end
512+
end
507513
end

spec/shared/contexts/movie_context.rb

+16-4
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ class AwardSerializer
240240
include FastJsonapi::ObjectSerializer
241241
attributes :id, :title
242242
attribute :year, if: Proc.new { |record, params|
243-
params[:include_award_year].present? ?
244-
params[:include_award_year] :
243+
params[:include_award_year].present? ?
244+
params[:include_award_year] :
245245
false
246246
}
247247
belongs_to :actor
@@ -313,7 +313,7 @@ class MovieOptionalParamsDataSerializer
313313
include FastJsonapi::ObjectSerializer
314314
set_type :movie
315315
attributes :name
316-
attribute :director, if: Proc.new { |record, params| params && params[:admin] == true }
316+
attribute :director, if: Proc.new { |record, params| params[:admin] == true }
317317
end
318318

319319
class MovieOptionalRelationshipSerializer
@@ -327,7 +327,19 @@ class MovieOptionalRelationshipWithParamsSerializer
327327
include FastJsonapi::ObjectSerializer
328328
set_type :movie
329329
attributes :name
330-
belongs_to :owner, record_type: :user, if: Proc.new { |record, params| params && params[:admin] == true }
330+
belongs_to :owner, record_type: :user, if: Proc.new { |record, params| params[:admin] == true }
331+
end
332+
333+
class MovieOptionalAttributeContentsWithParamsSerializer
334+
include FastJsonapi::ObjectSerializer
335+
set_type :movie
336+
attributes :name
337+
attribute :director do |record, params|
338+
data = {}
339+
data[:first_name] = 'steven'
340+
data[:last_name] = 'spielberg' if params[:admin]
341+
data
342+
end
331343
end
332344
end
333345

0 commit comments

Comments
 (0)