Skip to content

Fix URI type adapter to preserve identity for non-ASCII URIs - #3067

Merged
eamonnmcmanus merged 3 commits into
google:mainfrom
proudhare:fix/ph-issue-1587
Jul 23, 2026
Merged

eamonnmcmanus merged 3 commits into
google:mainfrom
proudhare:fix/ph-issue-1587

Conversation

@anishesg

Copy link
Copy Markdown
Contributor

The URI type adapter was calling URI.toASCIIString() in its write() method, which percent-encodes non-ASCII characters and applies Unicode normalization. The read() method does not reverse this percent-encoding, causing URIs with international characters to lose their original identity during round-trip serialization. For example, s3://bucket/path/Kankyō.png would fail an equality check after being serialized and deserialized.

The fix replaces toASCIIString() with toString() in line 733 of TypeAdapters.java. This preserves the original URI representation without normalization while remaining JSON-spec compliant, as JSON allows UTF-8 non-ASCII characters in strings. The change is backward compatible because pure-ASCII URIs produce identical output with both methods.

A regression test was added to verify that URIs with non-ASCII characters maintain equality after round-trip serialization.

Fixes #1587

The URI type adapter was calling `URI.toASCIIString()` in its `write()` method, which percent-encodes non-ASCII characters and applies Unicode normalization. The `read()` method does not reverse this percent-encoding, causing URIs with international characters to lose their original identity during round-trip serialization. For example, `s3://bucket/path/Kankyō.png` would fail an equality check after being serialized and deserialized.

Signed-off-by: anish <anishesg@users.noreply.github.com>
@@ -187,6 +187,15 @@ public void testUriDeserialization() {
assertThat(target.toASCIIString()).isEqualTo(uriValue);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For consistency use toString() here now as well? (for that specific URI it does not make a difference though)

Suggested change
assertThat(target.toASCIIString()).isEqualTo(uriValue);
assertThat(target.toString()).isEqualTo(uriValue);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good catch, updated

anishesg and others added 2 commits July 22, 2026 23:50

@eamonnmcmanus eamonnmcmanus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I ran this against all of Google's internal tests and did not find any issues. That suggests that the change in behaviour is unlikely to be a problem in practice.

@eamonnmcmanus
eamonnmcmanus merged commit 961517d into google:main Jul 23, 2026
21 checks passed
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.

URI type adapter does not preserve object identity

3 participants