Skip to content

Commit

Permalink
Merge pull request #19 from vplrt/bugfix/dont-share-paremeters
Browse files Browse the repository at this point in the history
Don't share parameters through the parent class
  • Loading branch information
estepnv authored Aug 10, 2022
2 parents 1620dbc + 39b503b commit b5885dc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/fast_serializer/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ def deep_copy
end

def serialize_resource(resource, params = {}, context = self)
Utils.ref_merge(self.params, params)
_params_dup = FastSerializer::Utils.symbolize_keys(self.params)
Utils.ref_merge(_params_dup, params)
self.params.delete(:meta)

meta = _params_dup.delete(:meta)

is_collection = if _params_dup.key?(:is_collection)
Expand Down
2 changes: 1 addition & 1 deletion lib/fast_serializer/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Utils
def self.symbolize_keys(hash)
res = {}
hash.each { |key, value| res[key.to_sym] = value }
hash
res
end

def self.ref_merge(hash_a, hash_b)
Expand Down
2 changes: 1 addition & 1 deletion lib/fast_serializer/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module FastSerializer
VERSION = '0.6.8'
VERSION = '0.6.9'
end
21 changes: 21 additions & 0 deletions spec/fast_serializer/inheritance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@ class ResourceSerializer
include FastSerializer::Schema::Mixin
attribute(:email)
attribute(:foo) { foo }
attribute(:shared) { params[:shared_context] }

def foo
'foo'
end
end

class AssociatedResourceSerializer < ResourceSerializer
attribute(:id)
attribute(:shared) { params[:shared_context] }
end

class InheritedResourceSerializer < ResourceSerializer
attribute(:yet_another_email, :email)
end

class InheritedResourceSerializer2 < ResourceSerializer
attribute(:yet_another_email_2, :email)

has_many :has_many_relationship, serializer: AssociatedResourceSerializer
end
end

Expand All @@ -38,5 +46,18 @@ class InheritedResourceSerializer2 < ResourceSerializer
expect(inherited_resource_serializer_2_h[:email]).to eq(resource.email)
expect(inherited_resource_serializer_2_h[:yet_another_email]).to be_blank
expect(inherited_resource_serializer_2_h[:yet_another_email_2]).to eq(resource.email)
expect(inherited_resource_serializer_h[:shared]).to be_nil
end

it 'does not share parameters through the parent class' do
inherited_resource_serializer_h = InheritedResourceSerializer.new(resource, shared_context: "shared").serializable_hash

expect(inherited_resource_serializer_h[:shared]).to eq("shared")

inherited_resource_serializer_2_h = InheritedResourceSerializer2.new(resource).serializable_hash

expect(inherited_resource_serializer_2_h[:shared]).to be_nil
expect(inherited_resource_serializer_2_h[:has_many_relationship].first[:shared]).to be_nil
expect(inherited_resource_serializer_2_h[:has_many_relationship].last[:shared]).to be_nil
end
end

0 comments on commit b5885dc

Please sign in to comment.