fix(abstractions): raise ValueError for out-of-range timedelta strings - #760
Open
Max Azatian (HardMax71) wants to merge 1 commit into
Open
Max Azatian (HardMax71) wants to merge 1 commit into
Max Azatian (HardMax71) wants to merge 1 commit into
Conversation
parse_timedelta_string and parse_timedelta_from_iso_format let OverflowError through when a duration is past timedelta.max, such as "50001140846:00021" or "P1000000000D". The callers only catch ValueError, so a typed timedelta property in JSON, form additional data and the JSON writer's string check raised OverflowError instead of treating the value as invalid. Both functions now raise ValueError from the OverflowError. Fixes microsoft#480.
|
Contributor
Author
|
try-catch-rethrow is tbh quite an antipattern. I'd say in general refactoring of this stuff is needed, but as a separate PR, unrelated to #480 ready for review btw |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Overview
parse_timedelta_stringandparse_timedelta_from_iso_formatlet OverflowError through when a duration is pasttimedelta.max, like "50001140846:00021" from the issue or "P1000000000D". The callers only catch ValueError. So after #664 the OverflowError still escaped from three places: a typed timedelta property in JSON (it crashed deserialization of the whole response), the form serializer's additional data, and the JSON writer's string check. Both functions now raise ValueError from the OverflowError. These values are handled like any other invalid duration:get_timedelta_valuereturns None, additional data keeps the raw string, and the writer raises its own "Invalid timedelta string value found" error.Related Issue
Fixes #480
Notes
The json and form packages get this through abstractions, so it reaches them with the next abstractions release. I didn't touch their
microsoft-kiota-abstractions>=1.11.1lower bound. The text serializer'sget_timedelta_valuedoesn't catch anything, so there it now raises ValueError, as it already does for any other invalid string.Testing Instructions
cd packages/abstractions && pytest tests/test_date_utils.py -k out_of_range: out-of-range ISO durations (one of them parses to float infinity) and an out-of-range hh:mm:ss string raise ValueError.cd packages/serialization/json && pytest -k out_of_range:get_timedelta_valuereturns None andwrite_timedelta_valueraises "Invalid timedelta string value found for property diff".cd packages/serialization/form && pytest -k out_of_range:get_object_valuekeeps the value as a string in additional data.