Add utf8, utf16, utf32 as accepted Encoding.GetEncoding aliases - #133710
Merged
tarekgh merged 1 commit intoSep 11, 2026
Merged
Conversation
Map the hyphen-less names utf8, utf16, and utf32 to code pages 65001, 1200, and 12000 in the CoreLib encoding name table, matching the aliases already accepted by Java, Python, ICU, glibc, and the WHATWG standard. Fixes the ArgumentException thrown when a server returns a Content-Type charset such as "utf8" (dotnet#133319).
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-encoding |
Member
Author
|
CC @kilasuit |
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Update the generator source so the autogenerated alias changes are not overwritten.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds utf8, utf16, and utf32 aliases to Encoding.GetEncoding(string) with regression tests.
Changes:
- Maps aliases to UTF-8, UTF-16, and UTF-32 code pages.
- Extends encoding lookup tests.
- Requires updating the generator source for the autogenerated lookup file.
File summaries
| File | Summary |
|---|---|
src/libraries/System.Runtime/tests/System.Text.Encoding.Tests/Encoding/EncodingGetEncodingTests.cs |
Tests the new alias mappings. |
src/libraries/System.Private.CoreLib/src/System/Text/EncodingData.cs |
Adds alias lookup entries and indices; generated source requires corresponding generator updates. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
Member
Author
|
/ba-g the failures are unrelated |
This was referenced Sep 11, 2026
5 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds the hyphen-less encoding names
utf8,utf16, andutf32as accepted aliases inEncoding.GetEncoding(string), mapping them to code pages 65001, 1200, and 12000 respectively.Today
Encoding.GetEncoding("utf8")throws:These hyphen-less spellings are already accepted by essentially every other major platform (Java, Python, ICU, glibc/iconv, Node.js) and are required labels for UTF-8 by the WHATWG Encoding Standard. .NET is the outlier in rejecting them. This surfaces in practice when a server returns a
Content-Typeheader such asapplication/json; charset=utf8andHttpClientfails to decode the body (see #133319).Change
Only the CoreLib name-to-code-page lookup table is affected (
System.Private.CoreLib):EncodingData.cs: addedutf16,utf32,utf8to theEncodingNamesstring in ordinal-sorted position, recomputedEncodingNameIndices, and added the matchingCodePagesByNameentries.System.Text.Encoding.CodePagesneeds no change: it owns only legacy code pages and does not participate in resolving Unicode encoding names.Notes
Encoding.GetEncodings()andEncodingInfo.Name/WebNameare driven by a separate table (MappedCodePages) and are unchanged, so enumeration returns no new or duplicated entries and still reports the canonical names (utf-8,utf-16,utf-32).unicode,latin1, andascii.Testing
EncodingGetEncodingTestsso the name-to-code-page andEncodingNametests coverutf8/utf16/utf32.System.Text.Encoding.Testssuite passes (14,695 tests, 0 failed).Fixes #133319