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

AS::Duration should serialize empty values correctly. #25656

Merged
merged 3 commits into from Jul 11, 2016

Conversation

@paul
Copy link
Contributor

@paul paul commented Jul 2, 2016

Summary

The current implementation of ActiveSupport::Duration::ISO8601Serializer serializes zero-length durations incorrectly (it serializes as "-P"), and cannot un-serialize itself:

[1] pry(main)> ActiveSupport::Duration.parse(0.minutes.iso8601)
ActiveSupport::Duration::ISO8601Parser::ParsingError: Invalid ISO 8601 duration: "-P" is empty duration
from /Users/rando/.gem/ruby/2.3.1/gems/activesupport-5.0.0/lib/active_support/duration/iso8601_parser.rb:96:in `raise_parsing_error'

Postgres empty intervals are serialized as "PT0S", which is also parseable by the Duration deserializer, so I've modified the ISO8601Serializer to do the same.

Additionally, the #normalize function returned a negative sign if parts was blank (all zero). Even though this fix does not rely on the sign, I've gone ahead and corrected that, too, in case a future refactoring of #serialize uses it.

The current implementation serializes zero-length durations incorrectly (it serializes as `"-P"`), and cannot un-serialize itself:

```
[1] pry(main)> ActiveSupport::Duration.parse(0.minutes.iso8601)
ActiveSupport::Duration::ISO8601Parser::ParsingError: Invalid ISO 8601 duration: "-P" is empty duration
from /Users/rando/.gem/ruby/2.3.1/gems/activesupport-5.0.0/lib/active_support/duration/iso8601_parser.rb:96:in `raise_parsing_error'
```

Postgres empty intervals are serialized as `"PT0S"`, which is also parseable by the Duration deserializer, so I've modified the `ISO8601Serializer` to do the same.

Additionally, the `#normalize` function returned a negative sign if `parts` was blank (all zero). Even though this fix does not rely on the sign, I've gone ahead and corrected that, too, in case a future refactoring of `#serialize` uses it.
@rails-bot
Copy link

@rails-bot rails-bot commented Jul 2, 2016

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@paul
Copy link
Contributor Author

@paul paul commented Jul 11, 2016

Is there anything missing from this PR that is preventing it from getting feedback or being merged?

@@ -14,6 +14,7 @@ def initialize(duration, precision: nil)
def serialize
output = 'P'
parts, sign = normalize
return output << "T0S" if parts.empty?

This comment has been minimized.

@sgrif

sgrif Jul 11, 2016
Contributor

This combination of return, side effects, and guard clause all at once really bothers me. I'd prefer it if we could structure it as:

parts, sign = normalize
return "PT0S".freeze if parts.empty?

output = 'P'
# ...

This comment has been minimized.

@paul

paul Jul 11, 2016
Author Contributor

Good idea, I like that better.

@@ -40,7 +41,7 @@ def normalize
end
# If all parts are negative - let's make a negative duration
sign = ''
if parts.values.all? { |v| v < 0 }
if !parts.empty? && parts.values.all? { |v| v < 0 }

This comment has been minimized.

@sgrif

sgrif Jul 11, 2016
Contributor

Do we actually need this change?

This comment has been minimized.

@paul

paul Jul 11, 2016
Author Contributor

As I mentioned in the last paragraph of the PR description, {}.values.all? #=> true. I made the change here because it would be nice if #normalize didn't return sign - if the value of the duration is 0, even if the current implementation of #serialize doesn't use it.

This comment has been minimized.

@sgrif

sgrif Jul 11, 2016
Contributor

I don't think it matters whether we return a sign or not, since this is not public API. I'd prefer to avoid the unnecessary git churn.

This comment has been minimized.

@paul

paul Jul 11, 2016
Author Contributor

I disagree, I think that leaving this change in reduces cognitive overhead for whomever comes next in refactoring the #serialize method, they'll have to deal with this bug in #normalize, too. I'm also skeptical of the cost of "git churn" as a metric.

But, since I am unlikely to be the one to refactor the #serialize method, and would really like to get this bugfix in so I can remove the monkeypatch from my app at some point, I'll revert this change.

paul added 2 commits Jul 11, 2016
@sgrif sgrif merged commit 629dde2 into rails:master Jul 11, 2016
1 check was pending
1 check was pending
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
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.

None yet

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