Skip to content

Comments

Deprecate AC::Parameters#== with Hash#23733

Merged
rafaelfranca merged 3 commits intorails:masterfrom
bquorning:actioncontroller-parameters-equality
Feb 19, 2016
Merged

Deprecate AC::Parameters#== with Hash#23733
rafaelfranca merged 3 commits intorails:masterfrom
bquorning:actioncontroller-parameters-equality

Conversation

@bquorning
Copy link
Contributor

As @schneems mentioned in #23711: “ActionController::Parameters is not a hash, so we should not expect it to be equal to one.” I think it would be best to deprecate that behavior before Rails 5 is released.

This also fixes #22820 – comparison between AC::Parameters instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since I cannot access other’s @parameters ivar in a clean way, I had to resort to calling to_h on both objects. Is this sufficient for equality comparison?

@senny
Copy link
Member

senny commented Feb 17, 2016

@bquorning looks like the build is broken.

@bquorning bquorning force-pushed the actioncontroller-parameters-equality branch from b9b9f26 to 4d1890a Compare February 17, 2016 13:49
@bquorning
Copy link
Contributor Author

I split the one commit into three, and hopefully fixed the build too. Time will tell.

Copy link

Choose a reason for hiding this comment

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

"if you" * 2 ✂️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed & pushed

@bquorning bquorning force-pushed the actioncontroller-parameters-equality branch from 4d1890a to 49ee962 Compare February 17, 2016 14:19
@maclover7
Copy link
Contributor

cc @sikachu

Copy link
Contributor

Choose a reason for hiding this comment

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

could also make protected attr_reader :parameters so other instances can access.

or just do permitted == other.permitted && @parameters == other.instance_variable_get("@parameters"), more dirty but saves on object allocation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A protected getter – brilliant idea. I’ve pushed the changes.

@bquorning bquorning force-pushed the actioncontroller-parameters-equality branch from 49ee962 to 97df883 Compare February 18, 2016 07:47
@rafaelfranca rafaelfranca added this to the 5.0.0 milestone Feb 18, 2016
@sikachu
Copy link
Member

sikachu commented Feb 18, 2016

I think this is good.

@rafaelfranca @matthewd would you mind merging this in?

Copy link
Member

Choose a reason for hiding this comment

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

Do we need to check if the parameter is permitted? I think we only care if the internal parameters are the same.

Copy link
Member

Choose a reason for hiding this comment

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

I think we do. Old code called super, which means it checks both @parameters and @permitted ivars.

Copy link
Member

Choose a reason for hiding this comment

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

And why didn't we continue to call super? If I get the comment above right super never checked the ivars.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Old code called super, which means it checks both @parameters and @permitted ivars.

The #== method and its super call didn’t exist in Rails 4.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There’s also a @converted_arrays ivar – do we need to check that as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed & pushed.

Copy link
Member

Choose a reason for hiding this comment

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

@jacobat

params = ActionController::Parameters.new(a: 1).permit(:a)
assert_equal { a: 1 }, params.to_h

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I need some time to think about this. Please don’t merge just yet.

Copy link
Member

Choose a reason for hiding this comment

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

After a second thought we should not recommend people to change their tests to assert(params1 == ActionController::Parameters.new(a: 1)). In that case they should use to_h. So yeah, let's keep permitted? there so we assert the object is the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jacobat You’ve convinced me, too (again). People can use to_h and even to_unsafe_h for comparing with unpermitted instances.

I’ve updated the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

this could also become attr_writer :permitted :D

@bquorning bquorning force-pushed the actioncontroller-parameters-equality branch from 6e2a65b to 3044036 Compare February 19, 2016 08:18
rafaelfranca added a commit that referenced this pull request Feb 19, 2016
…equality

Deprecate AC::Parameters#== with Hash
@rafaelfranca rafaelfranca merged commit e0c6156 into rails:master Feb 19, 2016
bquorning added a commit to bquorning/rails that referenced this pull request Feb 21, 2016
A protected getter for @parameters was introduced in rails#23733. Thus, we can stop reading the instance variable directly.
@bquorning bquorning deleted the actioncontroller-parameters-equality branch February 21, 2016 10:10
tenderlove added a commit that referenced this pull request Mar 31, 2022
PR #23733 was supposed to deprecate and remove the ability to compare
Hash objects with AC::Parameters objects.  Unfortunately it seems that
we still accidentally support that.

This PR adds a deprecation warning so that we can remove it in the
future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AC::Parameters doesn't compare (==) in a sane way

9 participants