Skip to content
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

Deprecate custom BigDecimal serialization #13911

Merged

Conversation

@davidcelis
Copy link
Contributor

@davidcelis davidcelis commented Feb 1, 2014

Rails currently provides an extension to BigDecimal that redefines how
it is serialized to YAML. However, as noted in #12467, this does not
work as expected. When ActiveSupport is required, BigDecimal YAML
serialization does not maintain the object type. It instead ends up
serializing the number represented by the BigDecimal itself which, when
loaded by YAML later, becomes a Float:

require 'yaml'
require 'bigdecimal'

yaml = BigDecimal('13.37').to_yaml
# => "--- !ruby/object:BigDecimal 18:0.1337E2\n...\n"
YAML.load(yaml).class
# => BigDecimal

require 'active_support/all'

yaml = BigDecimal('13.37').to_yaml
# => "--- 13.37\n...\n"
YAML.load(yaml).class
# => Float

@tenderlove posits that we should deprecate the custom BigDecimal
serialization and let Ruby handle it. For the time being, users who
require this serialization for backwards compatibility can manually
require 'active_support/core_ext/big_decimal/conversions'. I would
agree. So, remove active_support/core_ext/big_decimal so that this
extension is no longer required along with active_support/all.

This will close #12467 and deprecate the custom BigDecimal#to_yaml.

Signed-off-by: David Celis me@davidcel.is

@davidcelis
Copy link
Contributor Author

@davidcelis davidcelis commented Feb 1, 2014

I haven't included tests in this PR because I'm not sure how you guys typically approach tests for a mere deprecation. Just let me know if there's something on that front I should do.

@zzak
Copy link
Member

@zzak zzak commented Feb 1, 2014

require 'active_support/core_ext/big_decimal/conversions' seems like a lot of work, I will see if we can get this merged in upstream BigDecimal. It should be possible given that yaml is supported by stdlib.

@rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented Feb 1, 2014

This broke the whole test suite. Can you take a look?

Also I think we should only deprecate the yaml conversion. Right now we are removing more than we need to.

@rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented Feb 1, 2014

BTW, thanks @davidcelis for working on this

@davidcelis
Copy link
Contributor Author

@davidcelis davidcelis commented Feb 1, 2014

I had removed the whole file due to the discussion in the linked Issue, but I can remove only the YAML serialization if that's what people decide is a better idea.

@rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented Feb 1, 2014

We still need the custom to_s implementation so we can't remove it:

lib/active_record/connection_adapters/abstract/quoting.rb
32:        when BigDecimal then value.to_s('F')
70:        when BigDecimal then value.to_s('F')

So I think we should split that file in 2, deprecate the yaml serialization and leave the custom to_s implementation.

@davidcelis
Copy link
Contributor Author

@davidcelis davidcelis commented Feb 1, 2014

Gotcha. I'm assuming I should leave the to_d method there as well?

@rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented Feb 1, 2014

We can remove the to_d since it was only to Ruby 1.8 compatibility

@davidcelis
Copy link
Contributor Author

@davidcelis davidcelis commented Feb 1, 2014

Great. I'll add a separate commit for that; I can squash it later if you want.

@rafaelfranca
rafaelfranca reviewed Feb 1, 2014
View changes
activesupport/lib/active_support/core_ext/big_decimal/conversions.rb Outdated
@@ -3,20 +3,6 @@
require 'yaml'

This comment has been minimized.

@rafaelfranca

rafaelfranca Feb 1, 2014
Member

We can remove this require.

This comment has been minimized.

@davidcelis

davidcelis Feb 1, 2014
Author Contributor

Woops, thought I had; nice catch.

davidcelis added 2 commits Feb 1, 2014
Rails currently provides an extension to BigDecimal that redefines how
it is serialized to YAML. However, as noted in #12467, this does not
work as expected. When ActiveSupport is required, BigDecimal YAML
serialization does not maintain the object type. It instead ends up
serializing the number represented by the BigDecimal itself which, when
loaded by YAML later, becomes a Float:

```ruby
require 'yaml'
require 'bigdecimal'

yaml = BigDecimal('13.37').to_yaml
YAML.load(yaml).class

require 'active_support/all'

yaml = BigDecimal('13.37').to_yaml
YAML.load(yaml).class
```

@tenderlove posits that we should deprecate the custom BigDecimal
serialization and let Ruby handle it. For the time being, users who
require this serialization for backwards compatibility can manually
`require 'active_support/core_ext/big_decimal/yaml_conversions'`.

This will close #12467 and deprecate the custom BigDecimal#to_yaml.

Signed-off-by: David Celis <me@davidcel.is>
This was backported for Ruby 1.8 support and is no longer needed.

Signed-off-by: David Celis <me@davidcel.is>
rafaelfranca added a commit that referenced this pull request Feb 1, 2014
…tion

Deprecate custom BigDecimal serialization

Conflicts:
	activesupport/CHANGELOG.md
@rafaelfranca rafaelfranca merged commit c87b27e into rails:master Feb 1, 2014
@davidcelis davidcelis deleted the davidcelis:remove-bigdecimal-serialization branch Feb 2, 2014
@davidcelis
Copy link
Contributor Author

@davidcelis davidcelis commented Feb 2, 2014

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants
You can’t perform that action at this time.