Skip to content

[http-client-csharp] Add preserveUnderscores option to ToIdentifierName and ToVariableName#10062

Merged
JoshLove-msft merged 2 commits intomainfrom
copilot/http-client-csharp-update-toidentifiername
Mar 17, 2026
Merged

[http-client-csharp] Add preserveUnderscores option to ToIdentifierName and ToVariableName#10062
JoshLove-msft merged 2 commits intomainfrom
copilot/http-client-csharp-update-toidentifiername

Conversation

Copy link
Contributor

Copilot AI commented Mar 17, 2026

ToIdentifierName unconditionally treats underscores as word separators, stripping them and PascalCasing adjacent segments (Tls_1_0Tls10). Enum members and other identifiers that intentionally contain underscores need a way to opt out.

Changes

  • IsWordSeparator: accepts preserveUnderscores param; when true, _ is no longer a word separator
  • ToIdentifierName: new optional bool preserveUnderscores = false — fully backward compatible
  • ToVariableName: forwards the same parameter
  • Tests: 24 test cases covering default behavior and underscore preservation

Usage

// Default — existing behavior unchanged
"Tls_1_0".ToIdentifierName()                              // "Tls10"

// Preserve underscores for enum members, client names, etc.
"Tls_1_0".ToIdentifierName(preserveUnderscores: true)     // "Tls_1_0"
"TLS_1_0".ToIdentifierName(preserveUnderscores: true)     // "TLS_1_0"
Original prompt

This section details on the original issue you should resolve

<issue_title>[http-client-csharp] ToIdentifierName is too strict</issue_title>
<issue_description>We have a ToIdentifierName and ToVariableName which converts the input names from typespec into our C# identifiers in generated code.
Now, these two methods will trim off underscores in the name.
But in some cases, we need underscore to stay in the identifier, the most common case is the name of an enum member, such as Tls_1_0.

Can we update this method to make it accept an extra argument so that from downstream we could allow to add clientName to enum members to rename them to a name with underscore?</issue_description>

Comments on the Issue (you are @copilot in this section)


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

…iableName

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Mar 17, 2026
Copilot AI changed the title [WIP] [http-client-csharp] Update ToIdentifierName to allow underscores [http-client-csharp] Add preserveUnderscores option to ToIdentifierName and ToVariableName Mar 17, 2026
Copilot AI requested a review from JoshLove-msft March 17, 2026 15:19
@JoshLove-msft JoshLove-msft marked this pull request as ready for review March 17, 2026 15:20
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 17, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@10062

commit: ff3ae16

@JoshLove-msft JoshLove-msft added this pull request to the merge queue Mar 17, 2026
Merged via the queue into main with commit 9670c8e Mar 17, 2026
25 checks passed
@JoshLove-msft JoshLove-msft deleted the copilot/http-client-csharp-update-toidentifiername branch March 17, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[http-client-csharp] ToIdentifierName is too strict

3 participants