New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Parameters support legacy YAML encodings. #26017

Merged
merged 1 commit into from Aug 2, 2016

Conversation

Projects
None yet
2 participants
@kaspth
Member

kaspth commented Aug 1, 2016

By changing ActionController::Parameter's superclass, Rails 5 also changed
the YAML serialization format.

Since YAML doesn't know how to handle parameters it would fallback to its
routine for the superclass, which in Rails 4.2 was Hash while just Object
in Rails 5. As evident in the tags YAML would spit out:

4.2: !ruby/hash-with-ivars:ActionController::Parameters
5.0: !ruby/object:ActionController::Parameters

Thus when loading parameters YAML from 4.2 in Rails 5, it would parse a
hash dump as it would an Object class.

To fix this we have to provide our own init_with to be aware of the past
format as well as the new one. Then we add a load_tags mapping, such that
when the YAML parser sees !ruby/hash-with-ivars:ActionController::Parameters,
it knows to call our init_with function and not try to instantiate it as
a normal hash subclass.

Make Parameters support legacy YAML encodings.
By changing ActionController::Parameter's superclass, Rails 5 also changed
the YAML serialization format.

Since YAML doesn't know how to handle parameters it would fallback to its
routine for the superclass, which in Rails 4.2 was Hash while just Object
in Rails 5. As evident in the tags YAML would spit out:

4.2: !ruby/hash-with-ivars:ActionController::Parameters
5.0: !ruby/object:ActionController::Parameters

Thus when loading parameters YAML from 4.2 in Rails 5, it would parse a
hash dump as it would an Object class.

To fix this we have to provide our own `init_with` to be aware of the past
format as well as the new one. Then we add a `load_tags` mapping, such that
when the YAML parser sees `!ruby/hash-with-ivars:ActionController::Parameters`,
it knows to call our `init_with` function and not try to instantiate it as
a normal hash subclass.

@kaspth kaspth added the actionpack label Aug 1, 2016

@kaspth kaspth added this to the 5.0.1 milestone Aug 1, 2016

@kaspth kaspth self-assigned this Aug 1, 2016

@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Aug 1, 2016

👍

@kaspth kaspth merged commit 6b44155 into rails:master Aug 2, 2016

2 checks passed

codeclimate Code Climate didn't find any new or fixed issues.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@kaspth kaspth deleted the kaspth:parameters-yaml-format-backwardscompatibility branch Aug 2, 2016

kaspth added a commit to kaspth/rails that referenced this pull request Aug 2, 2016

Merge pull request rails#26017 from kaspth/parameters-yaml-format-bac…
…kwardscompatibility

Make Parameters support legacy YAML encodings.
@kaspth

This comment has been minimized.

Member

kaspth commented Aug 2, 2016

Backported to 5-0-stable: 77d0f12

@kaspth

This comment has been minimized.

Member

kaspth commented Aug 2, 2016

This seems to only be a problem for those of us who are on Psych 2.0.9 or greater. If we were to serialize an ActionController::Parameters on Psych 2.0.8, we'd get:

--- !ruby/hash:ActionController::Parameters
key: :value

That, coupled with 2.0.8 calling new instead of allocate means it would work just fine.

However, if you had a 2.0.8 serialized instance, then later upgraded to 2.0.9 or greater and then upgraded to Rails 5 it would blow up because of @parameters not being assigned through initialize never having been called.

@kaspth

This comment has been minimized.

Member

kaspth commented Aug 2, 2016

Pushed a commit to fix this in our app. On master 6eb9782 and 5-0-stable bd1e437.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment