[BAC-274] Cast ActionController::Parameters into hash during parsing … - #16
Merged
Conversation
There was a problem hiding this comment.
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::Parametersto a Hash when parsing JSON. - Updates
Equality#eql?to compareattributes.as_jsonand reviseshashto include values instead of duplicate keys. - Introduces
cast_jsonwrapper inActiveModel::Type::Valueto callto_unsafe_hon 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_jsonmethod to ensure it correctly convertsActionController::Parametersto a hash before casting. Consider adding a unit test inspec/active_model/entity/attribute_spec.rbto cover this behavior.
def cast_json(value)
|
|
||
| def hash | ||
| [self.class, *attributes.keys, *attributes.keys].hash | ||
| [self.class, *attributes.keys, *attributes.values].hash |
There was a problem hiding this comment.
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 |
HolyWalley
approved these changes
Jul 8, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…json