From 6434ed122cba91b584b80e1c211cb232d84085eb Mon Sep 17 00:00:00 2001 From: Taleh Zaliyev Date: Mon, 7 Jul 2025 19:03:23 +0300 Subject: [PATCH] [BAC-274] Cast ActionController::Parameters into hash during parsing json --- lib/active_model/entity/attribute.rb | 10 +++++++--- lib/active_model/entity/equality.rb | 4 ++-- spec/active_model/entity/parsers/json_spec.rb | 5 +++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/active_model/entity/attribute.rb b/lib/active_model/entity/attribute.rb index 88699d2..6734a1f 100644 --- a/lib/active_model/entity/attribute.rb +++ b/lib/active_model/entity/attribute.rb @@ -2,10 +2,14 @@ # Patch for ActiveModel::Type::Value ActiveModel::Type::Value.class_eval do - # Add alias to `cast` method for casting JSON values in ActiveModel::Attribute::FromJSON class - alias_method :cast_json, :cast + # Add wrapper to `cast` method for casting JSON values in ActiveModel::Attribute::FromJSON class + def cast_json(value) + value = value.to_unsafe_h if value.is_a?(ActionController::Parameters) - # Serialize value with options, for base ActiveModel::Type::* classes it will delegates to serialize + cast(value) + end + + # Serialize value with options, for base ActiveModel::Type::* classes it will call `serialize` def serialize_with_options(value, _options = {}) serialize(value) end diff --git a/lib/active_model/entity/equality.rb b/lib/active_model/entity/equality.rb index 54fa3bb..7ab2924 100644 --- a/lib/active_model/entity/equality.rb +++ b/lib/active_model/entity/equality.rb @@ -7,7 +7,7 @@ module Equality extend ActiveSupport::Concern def eql?(other) - attributes.eql?(other.attributes) + attributes.as_json.eql?(other.attributes.as_json) end def ==(other) @@ -15,7 +15,7 @@ def ==(other) end def hash - [self.class, *attributes.keys, *attributes.keys].hash + [self.class, *attributes.keys, *attributes.values].hash end end end diff --git a/spec/active_model/entity/parsers/json_spec.rb b/spec/active_model/entity/parsers/json_spec.rb index 36b5138..b70d8fe 100644 --- a/spec/active_model/entity/parsers/json_spec.rb +++ b/spec/active_model/entity/parsers/json_spec.rb @@ -74,6 +74,11 @@ class Person field_integers: [1, 3, 7] }) end + + it "converts ActionController::Parameters to hash" do + person = ParsersTest::Person.from_json(source) + expect(person.field_obj).to eq({ "x" => 1 }) + end end context "parsing json" do