Skip to content

[Mgmt][CodeGen] Fix KeyNotFoundException in LRO generation when multiple resources share one ResourceData type#59367

Merged
live1206 merged 2 commits into
mainfrom
copilot/fix-lro-generation-keynotfound
May 28, 2026
Merged

[Mgmt][CodeGen] Fix KeyNotFoundException in LRO generation when multiple resources share one ResourceData type#59367
live1206 merged 2 commits into
mainfrom
copilot/fix-lro-generation-keynotfound

Conversation

Copilot AI commented May 20, 2026

Copy link
Copy Markdown
Contributor

Description

ResourceOperationMethodProvider.BuildLroHandling crashes with KeyNotFoundException when N≥2 ResourceClientProviders share a single ResourceData type and at least one owns an LRO (e.g. Billing role assignment / role definition MPG migration, where BillingRoleAssignmentData backs 6 distinct resources across different parent scopes).

Root cause is an asymmetry between populator and reader of OperationSourceDict:

  • Populator (ManagementOutputLibrary.ProcessLroMethod) registers N entries keyed by each resourceProvider.Type.
  • Reader (BuildLroHandling via TryGetResourceClientProvider) intentionally bails out (Count != 1) and falls through to the non-resource branch, which looks up the raw data type key — never registered → crash.

Changes

  • ManagementOutputLibrary.ProcessLroMethod — when more than one resource provider matches the LRO return type, additionally register a fallback OperationSourceProvider keyed by the raw data type. This mirrors TryGetResourceClientProvider's "skip wrapping" intent and makes the dictionary symmetric with what BuildLroHandling reads in the non-wrap branch.
if (resourceProviders.Count > 0)
{
    foreach (var resourceProvider in resourceProviders)
    {
        operationSources.TryAdd(resourceProvider.Type, new OperationSourceProvider(resourceProvider));
    }

    // Mirror TryGetResourceClientProvider: when multiple resources share the same data type
    // the reader treats the response as a non-resource (raw data type).
    if (resourceProviders.Count > 1)
    {
        operationSources.TryAdd(returnCSharpType, new OperationSourceProvider(returnCSharpType));
    }
}
  • OperationSourceProviderTests.Verify_MultipleResourcesSharingDataModel_GeneratesSeparateOperationSources — updated to assert the new fallback entry (SiteDataOperationSource) is present alongside the two resource-typed entries (count now 3).

This is Option A from the issue — the minimal fix matching the existing intent in TryGetResourceClientProvider. Option B (deterministic resource selection in the reader) is left for a future change if a friendlier ArmOperation<TResource> shape is desired.


This checklist is used to make sure that common guidelines for a pull request are followed.

General Guidelines

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

SDK Generation Guidelines

  • If an SDK is being regenerated based on a new swagger spec, a link to the pull request containing these swagger spec changes has been included above.
  • The generate.cmd file for the SDK has been updated with the version of AutoRest, as well as the commitid of your swagger spec or link to the swagger spec, used to generate the code.
  • The *.csproj and AssemblyInfo.cs files have been updated with the new version of the SDK.

…are one ResourceData type

Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-net/sessions/8678245a-2488-4c7a-9d5a-be11226713b3

Co-authored-by: haiyuazhang <1924967+haiyuazhang@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix LRO generation crashes with KeyNotFoundException [Mgmt][CodeGen] Fix KeyNotFoundException in LRO generation when multiple resources share one ResourceData type May 20, 2026
Copilot AI requested a review from haiyuazhang May 20, 2026 16:22
@live1206
live1206 marked this pull request as ready for review May 26, 2026 02:17
Copilot AI review requested due to automatic review settings May 26, 2026 02:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a crash in the management-plane generator’s LRO handling when multiple ResourceClientProviders share the same ResourceData type by making OperationSourceDict registration symmetric with the lookup behavior in ResourceOperationMethodProvider.BuildLroHandling.

Changes:

  • Register an additional fallback OperationSourceProvider keyed by the raw data model type when multiple resources share that model type.
  • Update the existing unit test to assert the new fallback operation source entry is generated.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/ManagementOutputLibrary.cs Adds a raw-data-type fallback entry in ProcessLroMethod when multiple resources match the LRO return model type, preventing OperationSourceDict lookup failures.
eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/test/Providers/OperationSourceProviderTests.cs Updates assertions to expect the additional data-type-keyed fallback operation source alongside the per-resource entries.

@live1206
live1206 merged commit d3d9ea0 into main May 28, 2026
33 checks passed
@live1206
live1206 deleted the copilot/fix-lro-generation-keynotfound branch May 28, 2026 06:24
mcgallan pushed a commit to mcgallan/azure-sdk-for-net that referenced this pull request Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants