Fix API version enum collisions in multservice clients#10108
Fix API version enum collisions in multservice clients#10108jorgerangel-msft merged 10 commits intomainfrom
Conversation
…lisions Use last namespace segment for api version enum names in multi-service scenarios by default. Fall back to full namespace only when two or more services share the same last namespace segment, avoiding unnecessary prefix noise while still guaranteeing uniqueness. Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
...rp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientOptionsProvider.cs
Outdated
Show resolved
Hide resolved
… 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>
...rp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientOptionsProvider.cs
Outdated
Show resolved
Hide resolved
...crosoft.TypeSpec.Generator.ClientModel/test/Providers/ClientProviders/ClientProviderTests.cs
Show resolved
Hide resolved
...tor/Microsoft.TypeSpec.Generator/test/Providers/EnumProviders/ApiVersionEnumProviderTests.cs
Outdated
Show resolved
Hide resolved
|
@copilot we also need to ensure we regenerate all the test projects. |
…ct helpers, add TestData validation - Revert ClientOptionsProvider.BuildVersionProperties() to use full namespace (field names unchanged) - Revert ClientProviderTests assertions to original field names - Move GetLastNamespaceSegment and HasLastSegmentCollision to ClientHelper as shared static helpers - Update ApiVersionEnumProvider.BuildName() to use shared ClientHelper methods - Add TestData golden file validation for collision test - Update test data files: only enum type names change, property/field names stay the same Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Ran Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
…enum type, version properties, fields) Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/4bb660ba-ec46-42ad-8074-4e13314801b6
Refactored to apply Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
…erviceVersion Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/1abc462d-6e01-4045-857d-007426a4d797
Changed the enum naming from |
|
@copilot it looks that the original bug wasn't fully fixed per this comment. Validate the fix by adding unit tests to simulate the spec defined in the [original issue[(https://github.com//issues/10055#issue-4086914483). |
…ing; add original issue #10055 tests Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/af6998c3-f2e0-408c-a60e-efe47635b696
Added two unit tests simulating the original issue #10055 spec, and fixed the same-namespace collision case:
The fix adds a third tier to the naming logic: when even the full namespace produces duplicates (same namespace), it appends the enum input name (e.g., |
...rp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientOptionsProvider.cs
Outdated
Show resolved
Hide resolved
...erator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientOptionsProviderTests.cs
Outdated
Show resolved
Hide resolved
...erator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientOptionsProviderTests.cs
Outdated
Show resolved
Hide resolved
...erator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientOptionsProviderTests.cs
Outdated
Show resolved
Hide resolved
...erator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ClientOptionsProviderTests.cs
Outdated
Show resolved
Hide resolved
...rp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ClientOptionsProvider.cs
Outdated
Show resolved
Hide resolved
…ames on collision Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/9150f63a-0dcd-4821-a91c-67215de3eeba
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.BuildNameForServicehelper 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 useBuildNameForServiceby default, falling back to a segment-by-segment resolution when last segments collide. The enum type naming follows the{X}ServiceVersionpattern (e.g.,KeyVaultServiceVersion) rather thanService{X}Version.When last segments collide, the new
ClientHelper.GetShortestUniqueNamespacePrefixmethod 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
Sample.KeyVault,Sample.StorageSampleKeyVaultVersionKeyVaultServiceVersionSample.KeyVault,Sample.StorageSampleKeyVaultApiVersionKeyVaultApiVersionSample.KeyVault,Sample.StorageLatestServiceKeyVaultVersionLatestKeyVaultVersionAzure.One.Tests,Azure.Two.TestsAzureOneTestsVersionServiceOneTestsVersion(shortest unique: 2 segments)Azure.One.Tests,Azure.Two.TestsAzureOneTestsApiVersionServiceOneTestsApiVersion(shortest unique: 2 segments){Namespace}{EnumName}Version(disambiguated by input name)Changes
ClientHelper.cs— restoredBuildNameForServicehelper (removed in PR Fix duplicate API version field names for multi-service clients with colliding namespaces #10061) that extracts the last namespace segment and ensures proper prefix/suffix without duplication; added sharedGetLastNamespaceSegmentandHasLastSegmentCollisionstatic helpers; addedGetShortestUniqueNamespacePrefixthat progressively adds segments from right to left until finding a unique suffix, falling back to appending the enum input name when namespaces are identicalClientHelperTests.cs— restored all 22 tests forBuildNameForServiceApiVersionEnumProvider.BuildName()— usesClientHelper.BuildNameForServicewith empty prefix and"ServiceVersion"suffix for the non-collision case, producing{X}ServiceVersionnames; usesGetShortestUniqueNamespacePrefixfor segment-by-segment resolution when last segments collideClientOptionsProvider.BuildVersionProperties()— usesClientHelper.BuildNameForServicefor version property names in the non-collision case; usesGetShortestUniqueNamespacePrefixwhen last segments collideClientOptionsProvider.BuildLatestVersionsFields()— usesClientHelper.BuildNameForServicefor latest version field names in the non-collision case; usesGetShortestUniqueNamespacePrefixwhen last segments collideMultiServiceClient_WithCollidingLastSegments_UsesShortestUniquePrefixtest with TestData golden file validation; addedMultiServiceClient_SameLastSegment_ProducesUniqueVersionEnumstest; addedMultiServiceClient_UniqueNamespaces_ProducesUniqueVersionEnumstest simulating distinct service namespaces; addedMultiServiceClient_SameNamespace_ProducesUniqueVersionEnumstest for the same-namespace scenario where the emitter remaps both services to the same C# output namespaceOriginal prompt
📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.