Fix duplicate API version field names for multi-service clients with colliding namespaces#10061
Merged
JoshLove-msft merged 7 commits intomainfrom Mar 17, 2026
Merged
Conversation
…th colliding namespaces Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
…test Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] [CSharp] Fix duplicate field names for multi-service clients
Fix duplicate API version field names for multi-service clients with colliding namespaces
Mar 17, 2026
...rp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientOptionsProvider.cs
Outdated
Show resolved
Hide resolved
…ames per review feedback Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
commit: |
jorgerangel-msft
approved these changes
Mar 17, 2026
...crosoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/ClientProviderTests.cs
Show resolved
Hide resolved
…numProvider and add collision test Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
...client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ApiVersionEnumProvider.cs
Show resolved
Hide resolved
christothes
reviewed
Mar 17, 2026
...client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ApiVersionEnumProvider.cs
Show resolved
Hide resolved
christothes
approved these changes
Mar 17, 2026
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
JoshLove-msft
approved these changes
Mar 17, 2026
jorgerangel-msft
approved these changes
Mar 17, 2026
jsquire
approved these changes
Mar 17, 2026
christothes
approved these changes
Mar 17, 2026
Copilot AI
added a commit
that referenced
this pull request
Mar 20, 2026
… naming Re-adds ClientHelper.BuildNameForService and its tests that were removed in PR #10061. Uses BuildNameForService for the non-collision case in both ApiVersionEnumProvider.BuildName() and ClientOptionsProvider.BuildVersionProperties(), falling back to the full namespace only when last segments collide. Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
github-merge-queue bot
pushed a commit
that referenced
this pull request
Mar 23, 2026
PR #10061 unconditionally used the full namespace for API version enum names in multi-service scenarios. This produces unnecessarily verbose names when services have distinct last namespace segments. This PR changes the logic to only use the full namespace when there's an actual collision, and restores the `ClientHelper.BuildNameForService` helper for proper prefix/suffix-aware naming. The collision-aware naming applies to all multi-service generated members — enum type names, version property names, and latest version field names all use `BuildNameForService` by default, falling back to a segment-by-segment resolution when last segments collide. The enum type naming follows the `{X}ServiceVersion` pattern (e.g., `KeyVaultServiceVersion`) rather than `Service{X}Version`. When last segments collide, the new `ClientHelper.GetShortestUniqueNamespacePrefix` method progressively adds namespace segments from right to left until a unique suffix is found, producing shorter names than the previous full-namespace approach. When multiple enums share the exact same full namespace (e.g., when the emitter remaps both services to the same C# output namespace), the enum's input name is appended for disambiguation to prevent duplicate member names. ### Behavior | Namespaces | Member | Before (PR #10061) | After (no collision) | After (last segment collision) | After (full namespace collision) | |---|---|---|---|---|---| | `Sample.KeyVault`, `Sample.Storage` | Enum type | `SampleKeyVaultVersion` | `KeyVaultServiceVersion` | — | — | | `Sample.KeyVault`, `Sample.Storage` | Version property | `SampleKeyVaultApiVersion` | `KeyVaultApiVersion` | — | — | | `Sample.KeyVault`, `Sample.Storage` | Latest field | `LatestServiceKeyVaultVersion` | `LatestKeyVaultVersion` | — | — | | `Azure.One.Tests`, `Azure.Two.Tests` | Enum type | `AzureOneTestsVersion` | — | `ServiceOneTestsVersion` (shortest unique: 2 segments) | — | | `Azure.One.Tests`, `Azure.Two.Tests` | Version property | `AzureOneTestsApiVersion` | — | `ServiceOneTestsApiVersion` (shortest unique: 2 segments) | — | | Same namespace for both enums | Enum type | — | — | — | `{Namespace}{EnumName}Version` (disambiguated by input name) | ### Changes - **`ClientHelper.cs`** — restored `BuildNameForService` helper (removed in PR #10061) that extracts the last namespace segment and ensures proper prefix/suffix without duplication; added shared `GetLastNamespaceSegment` and `HasLastSegmentCollision` static helpers; added `GetShortestUniqueNamespacePrefix` that progressively adds segments from right to left until finding a unique suffix, falling back to appending the enum input name when namespaces are identical - **`ClientHelperTests.cs`** — restored all 22 tests for `BuildNameForService` - **`ApiVersionEnumProvider.BuildName()`** — uses `ClientHelper.BuildNameForService` with empty prefix and `"ServiceVersion"` suffix for the non-collision case, producing `{X}ServiceVersion` names; uses `GetShortestUniqueNamespacePrefix` for segment-by-segment resolution when last segments collide - **`ClientOptionsProvider.BuildVersionProperties()`** — uses `ClientHelper.BuildNameForService` for version property names in the non-collision case; uses `GetShortestUniqueNamespacePrefix` when last segments collide - **`ClientOptionsProvider.BuildLatestVersionsFields()`** — uses `ClientHelper.BuildNameForService` for latest version field names in the non-collision case; uses `GetShortestUniqueNamespacePrefix` when last segments collide - **Tests** — updated assertions and expected test data files (ClientOptionsProvider, ClientProvider, RestClientProvider) for member name changes; added `MultiServiceClient_WithCollidingLastSegments_UsesShortestUniquePrefix` test with TestData golden file validation; added `MultiServiceClient_SameLastSegment_ProducesUniqueVersionEnums` test; added `MultiServiceClient_UniqueNamespaces_ProducesUniqueVersionEnums` test simulating distinct service namespaces; added `MultiServiceClient_SameNamespace_ProducesUniqueVersionEnums` test for the same-namespace scenario where the emitter remaps both services to the same C# output namespace <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Only Use Full Namespace for Api Version Enum When There are Conflicts</issue_title> > <issue_description>Follow up to #10061. > > This PR introduced a change to avoid collisions in the generated api version enums in the multi-service scenario by using the full namespace of the enum as the name. We should update this to only do this whenever there is a collision in the generated names.</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes #10107 <!-- START COPILOT CODING AGENT TIPS --> --- 📱 Kick off Copilot coding agent tasks wherever you are with [GitHub Mobile](https://gh.io/cca-mobile-docs), available on iOS and Android. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
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.
ClientOptionsProvider.BuildVersionProperties()andApiVersionEnumProvider.BuildName()both derived names frominputEnum.NamespaceviaBuildNameForService(), which extracts only the last namespace segment. When two services have different full namespaces but the same last segment, identical names are generated, crashingClientProvider.BuildMethods()atFields.ToDictionary(). This blocks the Azure management plane generator for multi-service SDKs like Azure Compute.Changes
ApiVersionEnumProvider.cs: For multi-service clients, use the full namespace (namespace.ToIdentifierName() + "Version") instead ofBuildNameForService()to derive service version enum names. This guarantees uniqueness since different services always have different full namespaces.ClientOptionsProvider.cs: For multi-service clients, use the full namespace (inputEnum.Namespace.ToIdentifierName() + "ApiVersion") instead ofBuildNameForService()to derive version property names. Also simplified constructor parameter naming to use the enum name directly (serviceVersionEnum.Name.ToVariableName()).ClientHelper.cs/ClientHelperTests.cs: Removed the now-unusedBuildNameForServicehelper method and its tests, since both call sites were replaced with full-namespace-based naming.ClientProviderTests.cs: Regression test with two services having different full namespaces but the same last segment (Azure.ServiceOne.TestsvsAzure.ServiceTwo.Tests), verifying no crash and distinct field names (_azureServiceOneTestsApiVersion,_azureServiceTwoTestsApiVersion).ClientOptionsProviderTests.cs: Test validating that generated service version enums have no naming collisions when namespaces share the same last segment.ApiVersionEnumProviderTests.cs: Updated assertions to match new full-namespace-based enum naming pattern.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.