-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Conversation
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.
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. |
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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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'
# ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I like that better.
Summary
The current implementation of
ActiveSupport::Duration::ISO8601Serializer
serializes zero-length durations incorrectly (it serializes as"-P"
), and cannot un-serialize itself:Postgres empty intervals are serialized as
"PT0S"
, which is also parseable by the Duration deserializer, so I've modified theISO8601Serializer
to do the same.Additionally, the
#normalize
function returned a negative sign ifparts
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.