-
Notifications
You must be signed in to change notification settings - Fork 979
feat: add custom JSON serializer support to HTTPClient #1590
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
Merged
antoineeripret
merged 2 commits into
burnash:master
from
Gardner-Programs:feat/custom-json-serializer
Jul 3, 2026
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import datetime | ||
| import json | ||
| from unittest import TestCase | ||
| from unittest.mock import Mock | ||
|
|
||
| from gspread.http_client import HTTPClient | ||
|
|
||
|
|
||
| class HTTPClientSerializerTest(TestCase): | ||
| def _make_client(self): | ||
| session = Mock() | ||
| session.request.return_value.ok = True # so request() returns, doesn't raise | ||
| return HTTPClient(auth=None, session=session), session | ||
|
|
||
| def test_default_uses_json_kwarg(self): | ||
| """Without a serializer, the body goes out via json= (unchanged behavior).""" | ||
| client, session = self._make_client() | ||
| body = {"values": [[1, 2, 3]]} | ||
|
|
||
| client.request("post", "http://example.com", json=body) | ||
|
|
||
| _, kwargs = session.request.call_args | ||
| self.assertEqual(kwargs["json"], body) | ||
| self.assertIsNone(kwargs["data"]) | ||
|
|
||
| def test_custom_serializer_uses_data_and_header(self): | ||
| """With a serializer, the body is serialized into data= with a JSON header.""" | ||
| client, session = self._make_client() | ||
| client.set_serializer(lambda b: json.dumps(b, default=str)) | ||
| body = {"values": [[1, 2, 3]]} | ||
|
|
||
| client.request("post", "http://example.com", json=body) | ||
|
|
||
| _, kwargs = session.request.call_args | ||
| self.assertIsNone(kwargs["json"]) | ||
| self.assertEqual(kwargs["data"], json.dumps(body, default=str)) | ||
| self.assertEqual(kwargs["headers"]["Content-Type"], "application/json") | ||
|
|
||
| def test_custom_serializer_handles_non_native_types(self): | ||
| """The actual use case from the issue: serialize a date the stdlib can't.""" | ||
| client, session = self._make_client() | ||
| client.set_serializer(lambda b: json.dumps(b, default=lambda o: o.isoformat())) | ||
| body = {"values": [[datetime.date(2026, 6, 19)]]} | ||
|
|
||
| client.request("post", "http://example.com", json=body) | ||
|
|
||
| _, kwargs = session.request.call_args | ||
| self.assertIn("2026-06-19", kwargs["data"]) |
Oops, something went wrong.
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.
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.
why is the dict needed here?
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.
I was just mirroring the existing
json=dict(json) if json else Noneline below it. Sincejsonis typed asMapping[str, Any]it isn't guaranteed to be a concretedict, so this makes sure the serializer gets one it can encode.