Skip to content
This repository has been archived by the owner on Aug 17, 2017. It is now read-only.

Calling #reverse_merge on an ActionController::Parameters objects sets #permitted? to false #212

Closed
Hammadk opened this issue Dec 15, 2014 · 3 comments

Comments

@Hammadk
Copy link

Hammadk commented Dec 15, 2014

Calling #reverse_merge on an ActionController::Parameters object sets the #permitted value to false. For example:

x = ActionController::Parameters.new(x: 1).permit(:x)
y = ActionController::Parameters.new(y: 1).permit(:y)

x.permitted? 
=> true
y.permitted?
=> true

x.merge(y).permitted?
=> true
x.reverse_merge(y).permitted?
=> false

y.merge(x).permitted?
=> true 
y.reverse_merge(x).permitted?
=> false

This is because ActionController::Parameters#merge duplicates the entire object via self.dup.update, while #reverse_merge calls ActiveSupport::HashWithIndifferentAccess#reverse_merge which only copies the values of the other hash:

def merge(hash, &block)
  self.dup.update(hash, &block)
end

def reverse_merge(other_hash)
  super(self.class.new_from_hash_copying_default(other_hash))
end

cc: @nickhoffman

@rafaelfranca
Copy link
Member

ActionController::Parameters is going to not inherit from Hash in future versions. When this happen reverse_merge will be not even defined on it (and neither it should be)

Thank you for reporting

@Hammadk
Copy link
Author

Hammadk commented Dec 17, 2014

@rafaelfranca Glad you have this under control. Should we add regression tests in version 0.2.4 to prevent this?

@rafaelfranca
Copy link
Member

The implementation is not under this gem. It is only at the Rails repository and there are tests there.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants