Skip to content

Commit b49d59f

Browse files
authored
change request path ancestor relationship detection logic. (#52669)
* change request path ancestor relationship dectection logic. * refine. * refine.
1 parent f09e243 commit b49d59f

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Models/RequestPathPattern.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,23 @@ private static IReadOnlyList<RequestPathSegment> ParseSegments(string path)
8181
/// <returns></returns>
8282
public bool IsAncestorOf(RequestPathPattern other)
8383
{
84+
// Ancestor detection: compare only constant segments, skip variable segments.
8485
// To be the parent of other, you must at least be shorter than other.
8586
if (other.Count <= Count)
8687
return false;
8788
for (int i = 0; i < Count; i++)
8889
{
89-
// we need the segment to be identical when strict is true (which is the default value)
90-
// when strict is false, we also need the segment to be identical if it is constant.
91-
// but if it is a reference, we only require they have the same type, do not require they have the same variable name.
92-
// This case happens a lot during the management group parent detection - different RP calls this different things
93-
if (!this[i].Equals(other[i]))
90+
if (this[i].IsConstant)
91+
{
92+
if (!this[i].Equals(other[i]))
93+
return false;
94+
}
95+
else // variable segment
96+
{
97+
if (!other[i].IsConstant)
98+
continue;
9499
return false;
100+
}
95101
}
96102
return true;
97103
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Azure.Generator.Management.Models;
5+
using NUnit.Framework;
6+
7+
namespace Azure.Generator.Management.Tests
8+
{
9+
public class RequestPathPatternTests
10+
{
11+
[TestCase("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}", true)]
12+
[TestCase("/subscriptions/{subscriptionId}", "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}", true)]
13+
[TestCase("/subscriptions/{subscriptionId}", "/providers/Microsoft.Management/managementGroups/{managementGroupId}", false)]
14+
public void IsAncestorOf_BasicCases(string ancestor, string descendant, bool expected)
15+
{
16+
var ancestorPattern = new RequestPathPattern(ancestor);
17+
var descendantPattern = new RequestPathPattern(descendant);
18+
Assert.AreEqual(expected, ancestorPattern.IsAncestorOf(descendantPattern));
19+
}
20+
21+
[Test]
22+
public void IsAncestorOf_AncestorMustBeShorterThanDescendant()
23+
{
24+
var ancestorPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
25+
var childPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
26+
// Ancestor and child are the same length, should return false
27+
Assert.IsFalse(ancestorPattern.IsAncestorOf(childPattern));
28+
29+
var longerAncestor = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage");
30+
var shorterChild = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
31+
// Ancestor is longer than child, should return false
32+
Assert.IsFalse(longerAncestor.IsAncestorOf(shorterChild));
33+
}
34+
35+
[Test]
36+
public void IsAncestorOf_VariableSegmentCheck()
37+
{
38+
var ancestorPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
39+
var descendantPattern = new RequestPathPattern("/subscriptions/{otherSub}/resourceGroups/{otherGroup}/providers/Microsoft.Storage/storageAccounts/{accountName}");
40+
Assert.IsTrue(ancestorPattern.IsAncestorOf(descendantPattern));
41+
}
42+
43+
[Test]
44+
public void IsAncestorOf_ConstantSegmentMismatch()
45+
{
46+
var ancestorPattern = new RequestPathPattern("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}");
47+
var descendantPattern = new RequestPathPattern("/tenants/{tenantId}/resourceGroups/{resourceGroupName}");
48+
Assert.IsFalse(ancestorPattern.IsAncestorOf(descendantPattern));
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)