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

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

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 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

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

BTW, thanks @davidcelis for working on this

@davidcelis
Copy link
Contributor Author

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

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

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

@rafaelfranca
Copy link
Member

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

@davidcelis
Copy link
Contributor Author

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

@@ -3,20 +3,6 @@
require 'yaml'
Copy link
Member

Choose a reason for hiding this comment

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

We can remove this require.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woops, thought I had; nice catch.

Rails currently provides an extension to BigDecimal that redefines how
it is serialized to YAML. However, as noted in rails#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 rails#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 remove-bigdecimal-serialization branch February 2, 2014 01:12
@davidcelis
Copy link
Contributor Author

Thanks!

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.

ActiveSupport 4 breaks BigDecimal#to_yaml
3 participants