Skip to content

Conversation

ShaneHarvey
Copy link
Member

@ShaneHarvey ShaneHarvey commented Oct 31, 2024

PYTHON-4885 Fix legacy extended JSON encoding of DatetimeMS

Note I opted not to introduce logic to parse previously encoded invalid "$date" data for a few reasons:

  1. It's not clear if that behavior would be useful. No one has reported this as an issue.
  2. We can always introduce it later if we get new info that it's required.
  3. Adding it is problematic since it's invalid. We don't know if the data is genuinly invalid or a result of the encoding bug. For example, does the user intend this '{"dt": { "$date" : "1970"}}' to be the year 1970 or is it 1970 milliseconds after the epoch?

Old behavior:

>>> dumps({'d': DatetimeMS(1)}, json_options=LEGACY_JSON_OPTIONS)
'{"d": {"$date": "1"}}'

New behavior:

>>> dumps({'d': DatetimeMS(1)}, json_options=LEGACY_JSON_OPTIONS)
'{"d": {"$date": 1}}'

This PR also fixes a bug in the parsing logic where we would incorrectly raise an IndexError on invalid $date:

Old behavior:

>>> loads('{"dt": { "$date" : "1"}}')
Traceback (most recent call last):
...
  File "/Users/shane/git/mongo-python-driver/bson/json_util.py", line 624, in _parse_canonical_datetime
    elif dtm[-6] in ("+", "-") and dtm[-3] == ":":
         ~~~^^^^
IndexError: string index out of range

New behavior:

>>> loads('{"dt": { "$date" : "1"}}')
Traceback (most recent call last):
  File "/Users/shane/git/mongo-python-driver/bson/json_util.py", line 625, in _parse_canonical_datetime
    elif dtm[-6] in ("+", "-") and dtm[-3] == ":":
         ~~~^^^^
IndexError: string index out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
...
  File "/Users/shane/git/mongo-python-driver/bson/json_util.py", line 641, in _parse_canonical_datetime
    raise ValueError(f"time data {dtm!r} does not match ISO-8601 datetime format") from exc
ValueError: time data '1' does not match ISO-8601 datetime format

@ShaneHarvey ShaneHarvey requested a review from NoahStapp October 31, 2024 19:53
@ShaneHarvey ShaneHarvey merged commit 32269aa into mongodb:master Oct 31, 2024
34 of 35 checks passed
@ShaneHarvey ShaneHarvey deleted the PYTHON-4885 branch October 31, 2024 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants