Skip to content

Fix mgmt generator: use longest prefix match for parent resource detection#56180

Merged
ArthurMa1978 merged 1 commit intomainfrom
fix-mgmt-generator-parent-detection
Feb 11, 2026
Merged

Fix mgmt generator: use longest prefix match for parent resource detection#56180
ArthurMa1978 merged 1 commit intomainfrom
fix-mgmt-generator-parent-detection

Conversation

@ArthurMa1978
Copy link
Copy Markdown
Member

Fix a bug in resource-detection.ts where parent-child resource detection used first-match instead of longest-prefix-match when iterating the resource metadata map. This caused resources with 3+ levels of nesting (e.g., BestPractice → BestPracticeVersion → BestPracticeVersionDetail) to potentially be placed on the wrong parent if a shorter prefix (grandparent) was iterated before the correct parent.

The fix replaces the inner loop's 'break' on first match with tracking the longest matching prefix by segment count, ensuring the most specific parent resource is always selected.

Also adds a 3-level nesting test case (BestPracticeVersionDetail) to the multiplepaths.tsp test file to verify the fix.

Contributing to the Azure SDK

Please see our CONTRIBUTING.md if you are not familiar with contributing to this repository or have questions.

For specific information about pull request etiquette and best practices, see this section.

Copilot AI review requested due to automatic review settings February 11, 2026 07:30
@github-actions github-actions bot added CodeGen Issues that relate to code generation Mgmt This issue is related to a management package. labels Feb 11, 2026
@ArthurMa1978 ArthurMa1978 force-pushed the fix-mgmt-generator-parent-detection branch from 12fdb3c to 9aff20c Compare February 11, 2026 07:40
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 fixes legacy ARM resource parent/child detection in the management generator emitter by selecting the most specific parent (longest prefix match) rather than the first prefix encountered, preventing incorrect parenting for 3+ level nested resources. It also expands the multiplepaths.tsp test scenario to include a third nesting level and updates the expected generated output accordingly.

Changes:

  • Update legacy resource detection to choose the longest matching parent path when inferring parentResourceId.
  • Add a 3rd-level nested resource scenario (BestPracticeVersionDetail) to the multiplepaths.tsp TypeSpec test.
  • Refresh generated C# baseline output to include the new nested resource, its collection, REST operations, paging helpers, and ArmClient extensions.

Reviewed changes

Copilot reviewed 2 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
eng/packages/http-client-csharp-mgmt/emitter/src/resource-detection.ts Fix parent detection by tracking the longest matching prefix instead of breaking on first match.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/multiplepaths.tsp Add 3rd-level nested resource operations to validate longest-prefix parent inference.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/BestPracticeVersionResource.cs Add APIs to access BestPracticeVersionDetail collection and single-resource getters.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/BestPracticeVersionDetailCollection.cs New collection type for nested version details under a version resource.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/BestPracticeVersionDetailResource.cs New resource type representing a version detail instance and its operations.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/BestPracticeVersionDetailResource.Serialization.cs Add model serialization glue for the new resource wrapper.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/RestOperations/BestPracticeVersionDetailsRestOperations.cs New REST operations client for version detail endpoints.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/CollectionResults/BestPracticeVersionDetailsGetAllCollectionResultOfT.cs New sync paging implementation for listing version details.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/CollectionResults/BestPracticeVersionDetailsGetAllAsyncCollectionResultOfT.cs New async paging implementation for listing version details.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Extensions/MockableAzureGeneratorMgmtTypeSpecTestsArmClient.cs Add mockable ArmClient helper to construct BestPracticeVersionDetailResource from an id.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Extensions/AzureGeneratorMgmtTypeSpecTestsExtensions.cs Add ArmClient extension method to access BestPracticeVersionDetailResource.
eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Models/AzureGeneratorMgmtTypeSpecTestsContext.cs Register the new resource type for model reader/writer source generation.

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 11, 2026

@ArthurMa1978 I've opened a new pull request, #56181, to work on those changes. Once the pull request is ready, I'll request review from you.

@ArthurMa1978 ArthurMa1978 force-pushed the fix-mgmt-generator-parent-detection branch from 9aff20c to d6f4951 Compare February 11, 2026 08:06
…ction

Fix a bug in resource-detection.ts where parent-child resource detection
used first-match instead of longest-prefix-match when iterating the
resource metadata map. This caused resources with 3+ levels of nesting
(e.g., BestPractice → BestPracticeVersion → BestPracticeVersionDetail)
to potentially be placed on the wrong parent if a shorter prefix
(grandparent) was iterated before the correct parent.

The fix replaces the inner loop's 'break' on first match with tracking
the longest matching prefix by segment count, ensuring the most specific
parent resource is always selected.

Also adds a 3-level nesting test case (BestPracticeVersionDetail) to
the multiplepaths.tsp test file to verify the fix.
@ArthurMa1978 ArthurMa1978 force-pushed the fix-mgmt-generator-parent-detection branch from d6f4951 to d3277b9 Compare February 11, 2026 08:16
@ArthurMa1978 ArthurMa1978 merged commit 7719235 into main Feb 11, 2026
27 checks passed
@ArthurMa1978 ArthurMa1978 deleted the fix-mgmt-generator-parent-detection branch February 11, 2026 08:39
live1206 added a commit that referenced this pull request Feb 11, 2026
…esource-detection

- Add getSegmentCount helper to utils.ts to avoid redundant path splitting
- Replace isPrefix + getSharedSegmentCount combo in resource-detection.ts
  with direct getSharedSegmentCount + getSegmentCount calls
- Update isPrefix itself to use getSegmentCount instead of inline splits
- Remove isPrefix import from resource-detection.ts (no longer needed)

This eliminates redundant string splitting operations where isPrefix
was called followed by getSharedSegmentCount on the same paths.

Addresses feedback from PR #56180 (discussion r2791991621).
live1206 added a commit to azure-sdk/azure-sdk-for-net that referenced this pull request Feb 12, 2026
…urce parent change

The parent detection fix (PR Azure#56180) correctly moved SignalRReplicaSharedPrivateLinkResource
from SignalRResource to SignalRReplicaResource. Add [Obsolete] shim methods to preserve
backward compatibility with the 1.1.4 stable API surface.
live1206 added a commit to azure-sdk/azure-sdk-for-net that referenced this pull request Feb 12, 2026
…urce parent change

The parent detection fix (PR Azure#56180) correctly moved SignalRReplicaSharedPrivateLinkResource
from SignalRResource to SignalRReplicaResource. Add backward-compatible shim methods with
[EditorBrowsable(Never)] to preserve the old API surface with full working implementations.
live1206 added a commit to azure-sdk/azure-sdk-for-net that referenced this pull request Feb 12, 2026
…urce parent change

The parent detection fix (PR Azure#56180) correctly moved SignalRReplicaSharedPrivateLinkResource
from SignalRResource to SignalRReplicaResource. Add backward-compatible shim methods with
[EditorBrowsable(Never)] to preserve the old API surface with full working implementations.
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.

5 participants