Skip to content

Refactor request handling with library-agnostic RequestData#2976

Merged
adamtheturtle merged 1 commit intomainfrom
adamtheturtle/fix-duplicate-target-name
Feb 21, 2026
Merged

Refactor request handling with library-agnostic RequestData#2976
adamtheturtle merged 1 commit intomainfrom
adamtheturtle/fix-duplicate-target-name

Conversation

@adamtheturtle
Copy link
Member

@adamtheturtle adamtheturtle commented Feb 21, 2026

Introduce RequestData dataclass as a frozen, library-agnostic representation of HTTP requests, replacing the requests library's PreparedRequest in the handler layer.

Changes:

  • Add RequestData dataclass with method, path, headers, body fields
  • Update all handler signatures to accept RequestData instead of PreparedRequest
  • Move body normalization (None/str/bytes handling) to adapter boundary in decorators.py
  • Remove duplicate _body_bytes() helpers from both API modules
  • Decouple core business logic from requests library dependency

This enables future adapter support (respx, httpx, etc) and eliminates type-checking conflicts between mypy, pyrefly, and ruff.

Fixes #2974


Note

Medium Risk
Changes the core interface between the responses adapter and all mock route handlers, so incorrect request normalization (body/path/headers) could break request validation or JSON parsing across endpoints.

Overview
Refactors the mock request handling layer to use a new frozen RequestData dataclass (method, path, headers, body) instead of requests.PreparedRequest.

MockVWS._wrap_callback now converts a PreparedRequest into RequestData (including normalizing body to bytes) before invoking route handlers, and both Web Services and Web Query API handlers are updated to consume RequestData, removing duplicated body-parsing helpers and switching from path_url to path.

Written by Cursor Bugbot for commit def934d. This will update automatically on new commits. Configure here.

Introduce RequestData as a frozen dataclass to replace PreparedRequest dependency in handler layer. Convert PreparedRequest to RequestData at the responses adapter boundary, removing type-checking conflicts and centralizing request normalization. This decouples core business logic from the requests library and enables future adapter support (respx, httpx, etc).

- Add RequestData dataclass with method, path, headers, body fields
- Update all handler signatures to accept RequestData instead of PreparedRequest
- Move body normalization (None/str/bytes conversion) to adapter boundary
- Remove duplicate _body_bytes() helpers from both API handler modules
- Update decorators.py to convert at boundary in _wrap_callback

Fixes #2974

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

elif isinstance(raw_body, str):
body_bytes = raw_body.encode(encoding="utf-8")
else:
body_bytes = raw_body
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Query API body normalization behavior silently changed

Low Severity

The PR describes the two removed _body_bytes() helpers as "duplicate," but they had different behavior for str bodies. The old query API version returned b"" for string bodies (treating them same as None), while the services API version encoded them via .encode("utf-8"). The unified adapter in _wrap_callback now always encodes str to UTF-8, silently changing the query API's body handling semantics. If a str body ever reaches the query path, it will now be processed as real content rather than treated as empty.

Fix in Cursor Fix in Web

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavioral change is intentional. The old query `_body_bytes()` silently discarding string bodies (returning `b""`) was arguably a bug — if a string body arrives, encoding it to UTF-8 is strictly more correct than throwing it away.

In practice this is a no-op: the `responses` library always provides `bytes` or `None` for the query endpoint's multipart form body, so the `str` branch is never reached for query requests.

@adamtheturtle adamtheturtle merged commit 1a1a9e2 into main Feb 21, 2026
198 of 200 checks passed
@adamtheturtle adamtheturtle deleted the adamtheturtle/fix-duplicate-target-name branch February 21, 2026 08:54
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.

Replace PreparedRequest with a library-agnostic internal request type

1 participant