Skip to content

Match request bodies with equivalent datetime timezone notations#3750

Draft
fpighi wants to merge 1 commit into
masterfrom
fpighi/python-vcr-datetime-tz-matcher
Draft

Match request bodies with equivalent datetime timezone notations#3750
fpighi wants to merge 1 commit into
masterfrom
fpighi/python-vcr-datetime-tz-matcher

Conversation

@fpighi

@fpighi fpighi commented Jul 7, 2026

Copy link
Copy Markdown
Member

Motivation

Replay-based BDD scenarios that send a datetime in the request body can fail to match their recorded cassette, even when the request is correct. The Python client serializes timezone-aware datetimes with datetime.isoformat(), which emits a +00:00 UTC offset, while cassettes record the same instant as Z. These are equivalent RFC 3339 representations, but the VCR body matcher compared them as differing strings and reported a body mismatch.

This surfaced on the new DORA ListDORADeployments "date-time timestamps" scenario generated for datadog-api-spec#6101 (generated PR #3746), whose test / test jobs fail with a body - assertion failure. It is a general gap in the harness rather than anything specific to that endpoint: any replay-only scenario that puts a datetime in the request body is exposed. A contributing factor is that the matcher only treated text/json as JSON, so the usual application/json bodies bypassed JSON-aware comparison and fell back to a raw byte compare.

Overview of changes

Updates the VCR request-body matcher in the test harness so that it recognizes application/json in addition to text/json, and canonicalizes ISO-8601 datetime strings (normalizing timezone-aware values to UTC) before comparing bodies, so equivalent timezone notations compare equal. Non-datetime strings and JSON numbers are left unchanged, and a body that is not valid JSON falls back to a raw comparison.

Validation

Ran the full v1 and v2 replay scenario suites (RECORD=false): all pass (v2 1330 passed, v1 309 passed, 0 failed). Confirmed the previously-failing DORA date-time scenario now matches its cassette, and that genuinely different request bodies still fail to match.

The Python client serializes tz-aware datetimes via datetime.isoformat(),
producing a "+00:00" UTC offset, while recorded cassettes use "Z". Both denote
the same instant, but the VCR replay body matcher compared them as differing
strings, so replay-only scenarios that send a datetime in the request body
(e.g. DORA ListDORADeployments) fail to match their cassette. The matcher also
only recognized "text/json", so "application/json" bodies skipped JSON-aware
comparison entirely and fell back to a raw byte compare.
@fpighi fpighi force-pushed the fpighi/python-vcr-datetime-tz-matcher branch from 2d1d169 to a84b26c Compare July 7, 2026 09:22
@fpighi fpighi requested a review from nogates July 7, 2026 09:27
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.

1 participant