Skip to content

[BAC-274] Cast ActionController::Parameters into hash during parsing … - #16

Merged
taleh007 merged 1 commit into
masterfrom
feature/BAC-274
Jul 8, 2025
Merged

[BAC-274] Cast ActionController::Parameters into hash during parsing …#16
taleh007 merged 1 commit into
masterfrom
feature/BAC-274

Conversation

@taleh007

@taleh007 taleh007 commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

…json

@taleh007
taleh007 requested review from HolyWalley, Copilot and yard July 7, 2025 16:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR ensures that ActionController::Parameters objects are converted into plain Hashes during JSON parsing in ActiveModel entities, and adjusts equality and hashing logic to operate on JSON-serializable representations.

  • Adds a spec to verify conversion of ActionController::Parameters to a Hash when parsing JSON.
  • Updates Equality#eql? to compare attributes.as_json and revises hash to include values instead of duplicate keys.
  • Introduces cast_json wrapper in ActiveModel::Type::Value to call to_unsafe_h on parameter objects before casting.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
spec/active_model/entity/parsers/json_spec.rb Added test for converting ActionController::Parameters to Hash
lib/active_model/entity/equality.rb Use attributes.as_json in eql? and fix hash to include attributes.values
lib/active_model/entity/attribute.rb Define cast_json to convert params via to_unsafe_h before calling cast
Comments suppressed due to low confidence (1)

lib/active_model/entity/attribute.rb:6

  • There’s no existing spec for the new cast_json method to ensure it correctly converts ActionController::Parameters to a hash before casting. Consider adding a unit test in spec/active_model/entity/attribute_spec.rb to cover this behavior.
  def cast_json(value)


def hash
[self.class, *attributes.keys, *attributes.keys].hash
[self.class, *attributes.keys, *attributes.values].hash

Copilot AI Jul 7, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hash method uses attributes.keys and attributes.values in insertion order, which may lead to inconsistent hash values for logically equal objects if key ordering differs. Consider sorting the keys and pairing each key with its corresponding value to ensure a stable hash for equal attribute sets.

Suggested change
[self.class, *attributes.keys, *attributes.values].hash
sorted_attributes = attributes.keys.sort.map { |key| [key, attributes[key]] }
[self.class, *sorted_attributes].hash

Copilot uses AI. Check for mistakes.
@taleh007
taleh007 merged commit 91b1282 into master Jul 8, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants