Skip to content

[Mgmt CodeGen] Support customizing resource names via @@clientOption resource-name-mappings#59282

Open
haiyuazhang wants to merge 3 commits into
mainfrom
haiyzhan/mgmt-resource-name-mappings
Open

[Mgmt CodeGen] Support customizing resource names via @@clientOption resource-name-mappings#59282
haiyuazhang wants to merge 3 commits into
mainfrom
haiyzhan/mgmt-resource-name-mappings

Conversation

@haiyuazhang
Copy link
Copy Markdown
Member

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: DnsARecord produces DnsARecord instead 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 (see expandDynamicParentResourcesInSchema in resource-metadata.ts), but the names are always built as Capitalize(singular(enumValue)) + baseResourceName with 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 generated resourceName (LHS) to the desired resourceName (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 resourceName for correctness (parent lookup is by resourceModelId + 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:

  • normal resources (rename the SDK class without renaming the underlying model),
  • singleton resources,
  • expanded {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):

  • New "resource-name-mappings" key + extractResourceNameMappings and applyResourceNameOverrides helpers in resource-metadata.ts.
  • Wired into both code paths just before the final return:
    • resource-detection.ts:buildArmProviderSchema (legacy path)
    • resolve-arm-resources-converter.ts:resolveArmResources (modern resolveArmResources path)
  • Diagnostics for unused mapping entries (catches typos / generator-name drift) and for post-rename name collisions.

Commit 2 – end-to-end demonstration on generator/TestProjects/Local/Mgmt-TypeSpec:

  • routedoperations.tsp adds the decorator to the existing EventGridPrivateEndpointConnection expandable resource.
  • Six generated C# files renamed (EventGridTopicEventGridPrivateEndpointConnection*EventGridTopicPrivateEndpointConnection*, similarly for Domain).
  • Surrounding generated files (parent resources, extensions, context, tspCodeModel.json) updated to reference the new names. dotnet build passes 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)

  • Unit tests for applyResourceNameOverrides mirroring the existing resource-name-constraint tests in resource-detection.test.ts (single resource rename, partial expansion override, unused-key diagnostic, collision diagnostic).
  • Doc page under eng/packages/http-client-csharp-mgmt/docs listing the supported @@clientOption keys.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

haiyzhan and others added 2 commits May 16, 2026 01:44
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>
Copilot AI review requested due to automatic review settings May 15, 2026 17:54
@github-actions github-actions Bot added CodeGen Issues that relate to code generation Mgmt This issue is related to a management package. labels May 15, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 and resolveArmResources code 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.json snapshot 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.

Comment on lines +461 to +466
export function applyResourceNameOverrides(
resources: ArmResourceSchema[],
options: {
resolveModel: (resource: ArmResourceSchema) => DecoratedType | undefined;
diagnosticReporter?: (message: string) => void;
}
Comment on lines +514 to +516
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>
@ArcturusZhang
Copy link
Copy Markdown
Member

ArcturusZhang commented May 18, 2026

I think the config should be apply to the Read operation instead of the model considering the existence of multi-path resources.
If we decide to add this to the Read operation, the resource id in the config looks redundant, therefore maybe this could be a simple name, or a map from resource type to resource name when it is expandable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CodeGen Issues that relate to code generation Mgmt This issue is related to a management package.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Mgmt CodeGen] Support customization of resource names for expanded resources in TypeSpec codegen

3 participants