Skip to content

fix: read static discovery documents as UTF-8 - #2820

Open
MohammedAlkindi wants to merge 2 commits into
googleapis:mainfrom
MohammedAlkindi:fix/discovery-cache-utf8
Open

MohammedAlkindi wants to merge 2 commits into
googleapis:mainfrom
MohammedAlkindi:fix/discovery-cache-utf8

Conversation

@MohammedAlkindi

Copy link
Copy Markdown

get_static_doc opens the packaged discovery documents with no encoding, so they decode with the host locale codec instead of UTF-8.

On a default Windows install (cp1252) that is not cosmetic. Of the 599 documents get_static_doc can open, one raises and one decodes silently into the wrong characters:

run.v1beta1.json  UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 21862
jobs.v2.json      no error, but "C么te d'Ivoire" reads back as "C脙麓te d'Ivoire"

So build("run", "v1beta1") fails outright, and build("jobs", "v2") returns corrupted descriptions with nothing to signal it. A third packaged file, index.json, also fails to decode, but it is a directory index that get_static_doc never opens.

The fix passes encoding="utf-8". The added test writes a document containing U+201C and U+201D, points DISCOVERY_DOC_DIR at it, and asserts the round trip.

Verification. Windows, Python 3.13.13, locale cp1252. nox is not installed here, so I ran pytest directly. tests/test_discovery_cache.py: 1 passed, 1 skipped (FileCacheTest, oauth2client absent), and the new test fails on the pre-fix source with the charmap error above. tests/test_discovery.py: 168 passed, 2 skipped, 3 failed (test_tests_should_be_run_with_strict_positional_enforcement and two Universe mTLS tests), identical with this change reverted.

The packaged discovery documents are UTF-8, but get_static_doc opened
them with no encoding, so they were decoded with the host's locale
encoding instead.

On a default Windows install (cp1252) this makes
build("run", "v1beta1") raise UnicodeDecodeError, and jobs.v2.json
decodes to mojibake with no error at all.
@MohammedAlkindi
MohammedAlkindi requested a review from a team as a code owner September 18, 2026 22:38
@product-auto-label product-auto-label Bot added the size: s Pull request size is small. label Sep 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the get_static_doc function in googleapiclient/discovery_cache/init.py to explicitly use UTF-8 encoding when opening and reading static discovery documents, ensuring consistent behavior across different host locales. Additionally, a new unit test GetStaticDocTest has been added to tests/test_discovery_cache.py to verify this behavior using a temporary directory and mock patch. There are no review comments to address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: s Pull request size is small.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant