[Mgmt CodeGen] Support customizing resource names via @@clientOption resource-name-mappings#59282
[Mgmt CodeGen] Support customizing resource names via @@clientOption resource-name-mappings#59282haiyuazhang wants to merge 3 commits into
Conversation
Adds a generic mechanism to customize generated resource names via the @@clientoption(model, 'resource-name-mappings', #{ ... }) decorator. Each entry maps the currently generated resourceName (LHS) to the desired resourceName (RHS). This is applied as a final pure name-only transform after all resource building (expansion, parent inference, scope assignment, post-processing) is complete, so it works uniformly for normal, singleton, and expandable (dynamic {parentType}) resources. Closes #59210. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Demonstrates the new @@clientoption('resource-name-mappings') decorator end-to-end on the local Mgmt-TypeSpec test project's RoutedOperations example, mapping the auto-generated expanded resource names to friendlier ones (drops the duplicated EventGrid prefix). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds a new TypeSpec @@clientOption key ("resource-name-mappings") to the C# mgmt emitter to allow late-stage, name-only overrides of generated ARM resource wrapper names (including {parentType} expanded resources), and updates the local Mgmt-TypeSpec test project to demonstrate the feature end-to-end.
Changes:
- Added parsing/apply logic for
@@clientOption(model, "resource-name-mappings", #{ ... }, "csharp")in the mgmt emitter, wired into both legacy andresolveArmResourcescode paths. - Added a TypeSpec usage example in the local Mgmt-TypeSpec test project and regenerated outputs to reflect renamed expanded resources.
- Updated generated C# types/methods and the
tspCodeModel.jsonsnapshot to match the new resource names.
Reviewed changes
Copilot reviewed 5 out of 16 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| eng/packages/http-client-csharp-mgmt/emitter/src/resource-metadata.ts | Introduces extraction + application of resource-name-mappings, including unused-key and collision diagnostics. |
| eng/packages/http-client-csharp-mgmt/emitter/src/resource-detection.ts | Applies overrides as a final step in the legacy buildArmProviderSchema path. |
| eng/packages/http-client-csharp-mgmt/emitter/src/resolve-arm-resources-converter.ts | Applies overrides as a final step in the resolveArmResources-based path. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/routedoperations.tsp | Demonstrates resource-name-mappings on an expandable resource model. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/tspCodeModel.json | Updates the code model snapshot to include the new clientOption and renamed resourceName metadata. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Models/AzureGeneratorMgmtTypeSpecTestsContext.cs | Updates model registration attributes to reference renamed resource types. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Extensions/MockableAzureGeneratorMgmtTypeSpecTestsArmClient.cs | Renames ArmClient helper methods to return renamed resource wrapper types. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Extensions/AzureGeneratorMgmtTypeSpecTestsExtensions.cs | Renames extension methods and cref targets to match renamed resource wrappers. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/EventGridTopicResource.cs | Updates child-collection accessors to use renamed private endpoint connection wrappers. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/EventGridTopicPrivateEndpointConnectionResource.Serialization.cs | Regenerated serialization partial for the renamed topic child resource wrapper. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/EventGridTopicPrivateEndpointConnectionResource.cs | Renames the topic private endpoint connection resource wrapper and related diagnostics strings. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/EventGridTopicPrivateEndpointConnectionCollection.cs | Renames the topic private endpoint connection collection wrapper and related diagnostics strings. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/EventGridDomainResource.cs | Updates child-collection accessors to use renamed private endpoint connection wrappers. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/EventGridDomainPrivateEndpointConnectionResource.Serialization.cs | Regenerated serialization partial for the renamed domain child resource wrapper. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/EventGridDomainPrivateEndpointConnectionResource.cs | Renames the domain private endpoint connection resource wrapper and related diagnostics strings. |
| eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/EventGridDomainPrivateEndpointConnectionCollection.cs | Renames the domain private endpoint connection collection wrapper and related diagnostics strings. |
| export function applyResourceNameOverrides( | ||
| resources: ArmResourceSchema[], | ||
| options: { | ||
| resolveModel: (resource: ArmResourceSchema) => DecoratedType | undefined; | ||
| diagnosticReporter?: (message: string) => void; | ||
| } |
| diagnosticReporter?.( | ||
| `Multiple resources have the same resource name '${name}' after applying resource-name-mappings overrides.` | ||
| ); |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
I think the config should be apply to the Read operation instead of the model considering the existence of multi-path resources. |
Resolves #59210.
Problem
In the Swagger world,
equest-path-to-resource-name lets users override the generated resource name for path-templated resources (the canonical example being DNS, where
…/dnsZones/{zoneName}/{recordType}/{relativeRecordSetName}|Microsoft.Network/dnsZones/A: DnsARecordproducesDnsARecordinstead of an auto-derived name).The TypeSpec mgmt emitter already expands a single resource with a dynamic
{parentType}segment into one concrete resource per enum value (seeexpandDynamicParentResourcesInSchemainresource-metadata.ts), but the names are always built asCapitalize(singular(enumValue)) + baseResourceNamewith no customization knob. The issue asks for that knob.Approach
Add a generic
@@clientOption(model, ""resource-name-mappings"", #{ … })decorator key. The value is a record mapping the currently generatedresourceName(LHS) to the desiredresourceName(RHS).The rename is applied as a pure, name-only, late transformation – after expansion, parent inference, scope assignment, post-processing, and api-version resolution. Nothing downstream in the emitter depends on
resourceNamefor correctness (parent lookup is byresourceModelId+ path-prefix, scope by path), so the rename is a safe last step.Because it's generic (keyed by final generated name + the model), it works uniformly for:
{parentType}resources (the DNS / issue motivation),without any special-casing inside the expansion code.
What's in this PR
Commit 1 – emitter feature (
eng/packages/http-client-csharp-mgmt/emitter):"resource-name-mappings"key +extractResourceNameMappingsandapplyResourceNameOverrideshelpers inresource-metadata.ts.return:resource-detection.ts:buildArmProviderSchema(legacy path)resolve-arm-resources-converter.ts:resolveArmResources(modernresolveArmResourcespath)Commit 2 – end-to-end demonstration on
generator/TestProjects/Local/Mgmt-TypeSpec:routedoperations.tspadds the decorator to the existingEventGridPrivateEndpointConnectionexpandable resource.EventGridTopicEventGridPrivateEndpointConnection*→EventGridTopicPrivateEndpointConnection*, similarly for Domain).tspCodeModel.json) updated to reference the new names.dotnet buildpasses with 0 warnings / 0 errors.Example usage
``tsp
// One model shared by N expanded resources
@@clientoption(EventGridPrivateEndpointConnection,
""resource-name-mappings"",
#{
EventGridTopicEventGridPrivateEndpointConnection: ""EventGridTopicPrivateEndpointConnection"",
EventGridDomainEventGridPrivateEndpointConnection: ""EventGridDomainPrivateEndpointConnection"",
},
""csharp"");
// Plain resource rename
@@clientoption(RecordSet, ""resource-name-mappings"", #{ RecordSet: ""DnsRecordSet"" }, ""csharp"");
``
Follow-ups (not in this PR)
applyResourceNameOverridesmirroring the existingresource-name-constrainttests inresource-detection.test.ts(single resource rename, partial expansion override, unused-key diagnostic, collision diagnostic).eng/packages/http-client-csharp-mgmt/docslisting the supported@@clientOptionkeys.Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com