From 78b82258269e623632ebc616f16561463a463b94 Mon Sep 17 00:00:00 2001 From: Jean-Francis Bastien Date: Fri, 22 Aug 2025 09:08:21 -0400 Subject: [PATCH 1/2] --- lib/bright_serializer/serializer.rb | 2 +- .../serializer_params_spec.rb | 22 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/bright_serializer/serializer.rb b/lib/bright_serializer/serializer.rb index 6801839..168a71c 100644 --- a/lib/bright_serializer/serializer.rb +++ b/lib/bright_serializer/serializer.rb @@ -22,7 +22,7 @@ def self.included(base) def initialize(object, **options) @object = object - @params = options.delete(:params) + @params = options.delete(:params) || {} @fields = options.delete(:fields) end diff --git a/spec/bright_serializer/serializer_params_spec.rb b/spec/bright_serializer/serializer_params_spec.rb index f17b971..c85d064 100644 --- a/spec/bright_serializer/serializer_params_spec.rb +++ b/spec/bright_serializer/serializer_params_spec.rb @@ -15,7 +15,7 @@ "#{object.first_name} #{object.last_name}" end attribute :params do |object, params| - "#{object.first_name} #{object.last_name} #{params}" + "#{object.first_name} #{object.last_name} #{params[:suffix]}" end attribute :params_upcase do |object, params| @@ -23,12 +23,12 @@ end def upcase(object, params) - "#{object.first_name} #{object.last_name} #{params}".upcase + "#{object.first_name} #{object.last_name} #{params[:suffix]}".upcase end end end - let(:instance) { serializer_class.new(user, params: param) } + let(:instance) { serializer_class.new(user, params: { suffix: param }) } let(:param) { Faker::Lorem.word } let(:result) do @@ -44,5 +44,21 @@ def upcase(object, params) it 'serialize params' do expect(instance.to_hash).to eq(result) end + + context 'when not passing params' do + # The anonymous serializer use params in a way that it take for granted that params is always a hash + # and call params[:suffix], so if params is nil it will raise an error + # This test ensure that when params is not passed it will be an empty hash and no error is raised. + it 'passes an empty hash' do + instance = serializer_class.new(user) + expect(instance.to_hash).to eq( + first_name: user.first_name, + last_name: user.last_name, + name: "#{user.first_name} #{user.last_name}", + params: "#{user.first_name} #{user.last_name} ", + params_upcase: "#{user.first_name} #{user.last_name} ".upcase + ) + end + end end end From 78966dadee9f05b79666191f472bd4c1d3275d7e Mon Sep 17 00:00:00 2001 From: Jean-Francis Bastien Date: Wed, 3 Sep 2025 08:29:11 -0400 Subject: [PATCH 2/2] rework --- spec/bright_serializer/serializer_params_spec.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spec/bright_serializer/serializer_params_spec.rb b/spec/bright_serializer/serializer_params_spec.rb index c85d064..a3f13a3 100644 --- a/spec/bright_serializer/serializer_params_spec.rb +++ b/spec/bright_serializer/serializer_params_spec.rb @@ -15,7 +15,7 @@ "#{object.first_name} #{object.last_name}" end attribute :params do |object, params| - "#{object.first_name} #{object.last_name} #{params[:suffix]}" + "#{object.first_name} #{object.last_name} #{params[:suffix]}".strip end attribute :params_upcase do |object, params| @@ -23,7 +23,7 @@ end def upcase(object, params) - "#{object.first_name} #{object.last_name} #{params[:suffix]}".upcase + "#{object.first_name} #{object.last_name} #{params[:suffix]}".upcase.strip end end end @@ -46,17 +46,16 @@ def upcase(object, params) end context 'when not passing params' do - # The anonymous serializer use params in a way that it take for granted that params is always a hash - # and call params[:suffix], so if params is nil it will raise an error - # This test ensure that when params is not passed it will be an empty hash and no error is raised. + # The anonymous serializer assumes params is always a hash and calls params[:suffix] directly. + # This test ensures that when params is not passed, it is treated as an empty hash without raising an error. it 'passes an empty hash' do instance = serializer_class.new(user) expect(instance.to_hash).to eq( first_name: user.first_name, last_name: user.last_name, name: "#{user.first_name} #{user.last_name}", - params: "#{user.first_name} #{user.last_name} ", - params_upcase: "#{user.first_name} #{user.last_name} ".upcase + params: "#{user.first_name} #{user.last_name}", + params_upcase: "#{user.first_name} #{user.last_name}".upcase ) end end