Repository: Azure/azure-sdk-for-go (Go)

Issue:
arm.ParseResourceType incorrectly parses resource type for nested CognitiveServices deletedAccounts
### Problem
The `arm.ParseResourceType` function is incorrectly parsing the resource type for CognitiveServices deletedAccounts resources.

### Expected Behavior
For the resource ID:
```
/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.CognitiveServices/locations/eastus/resourceGroups/avm-res-cognitiveservices-account-rg-p26m/deletedAccounts/OpenAI-cog-p26m
```

The function should return:
```
Microsoft.CognitiveServices/locations/resourceGroups/deletedAccounts
```

### Actual Behavior
The function currently returns:
```
Microsoft.Resources/resourceGroups/deletedAccounts
```

### Analysis
The issue appears to be that the parser is incorrectly identifying `resourceGroups` as the primary resource provider namespace instead of recognizing it as part of the nested resource hierarchy under `Microsoft.CognitiveServices/locations`.

### Environment
- Package: `github.com/Azure/azure-sdk-for-go/sdk/azcore`
- Function: `arm.ParseResourceType`

### Steps to Reproduce
```go
resourceID := "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.CognitiveServices/locations/eastus/resourceGroups/avm-res-cognitiveservices-account-rg-p26m/deletedAccounts/OpenAI-cog-p26m"
resourceType := arm.ParseResourceType(resourceID)
fmt.Println(resourceType) // Currently prints: Microsoft.Resources/resourceGroups/deletedAccounts
```


Discussion:

Patch:
diff --git a/sdk/azcore/CHANGELOG.md b/sdk/azcore/CHANGELOG.md
index 01d6fec4754f..44cdbebda47e 100644
--- a/sdk/azcore/CHANGELOG.md
+++ b/sdk/azcore/CHANGELOG.md
@@ -10,6 +10,8 @@
 
 ### Bugs Fixed
 
+* Fixed resource identifier parsing for provider-specific resource hierarchies containing "resourceGroups" segments.
+
 ### Other Changes
 
 ## 1.18.2 (2025-07-31)
diff --git a/sdk/azcore/arm/internal/resource/resource_identifier.go b/sdk/azcore/arm/internal/resource/resource_identifier.go
index a08d3d0ffa68..b8348b7d82e8 100644
--- a/sdk/azcore/arm/internal/resource/resource_identifier.go
+++ b/sdk/azcore/arm/internal/resource/resource_identifier.go
@@ -123,9 +123,9 @@ func newResourceIDWithProvider(parent *ResourceID, providerNamespace, resourceTy
 }
 
 func chooseResourceType(resourceTypeName string, parent *ResourceID) ResourceType {
-	if strings.EqualFold(resourceTypeName, resourceGroupsLowerKey) {
+	if strings.EqualFold(resourceTypeName, resourceGroupsLowerKey) && isSubscriptionResource(parent) {
 		return ResourceGroupResourceType
-	} else if strings.EqualFold(resourceTypeName, subscriptionsKey) && parent != nil && parent.ResourceType.String() == TenantResourceType.String() {
+	} else if strings.EqualFold(resourceTypeName, subscriptionsKey) && isTenantResource(parent) {
 		return SubscriptionResourceType
 	}
 
@@ -182,12 +182,12 @@ func appendNext(parent *ResourceID, parts []string, id string) (*ResourceID, err
 
 	if len(parts) == 1 {
 		// subscriptions and resourceGroups are not valid ids without their names
-		if strings.EqualFold(parts[0], subscriptionsKey) || strings.EqualFold(parts[0], resourceGroupsLowerKey) {
+		if strings.EqualFold(parts[0], subscriptionsKey) && isTenantResource(parent) || strings.EqualFold(parts[0], resourceGroupsLowerKey) && isSubscriptionResource(parent) {
 			return nil, fmt.Errorf("invalid resource ID: %s", id)
 		}
 
 		// resourceGroup must contain either child or provider resource type
-		if parent.ResourceType.String() == ResourceGroupResourceType.String() {
+		if isResourceGroupResource(parent) {
 			return nil, fmt.Errorf("invalid resource ID: %s", id)
 		}
 
@@ -196,7 +196,7 @@ func appendNext(parent *ResourceID, parts []string, id string) (*ResourceID, err
 
 	if strings.EqualFold(parts[0], providersKey) && (len(parts) == 2 || strings.EqualFold(parts[2], providersKey)) {
 		// provider resource can only be on a tenant or a subscription parent
-		if parent.ResourceType.String() != SubscriptionResourceType.String() && parent.ResourceType.String() != TenantResourceType.String() {
+		if !isSubscriptionResource(parent) && !isTenantResource(parent) {
 			return nil, fmt.Errorf("invalid resource ID: %s", id)
 		}
 
@@ -225,3 +225,18 @@ func splitStringAndOmitEmpty(v, sep string) []string {
 
 	return r
 }
+
+// isTenantResource returns true if the resourceID represents a tenant resource. The condition is resource ID matched with TenantResourceType and has no parent.
+func isTenantResource(resourceID *ResourceID) bool {
+	return resourceID != nil && strings.EqualFold(resourceID.ResourceType.String(), TenantResourceType.String()) && resourceID.Parent == nil
+}
+
+// isSubscriptionResource returns true if the resourceID represents a subscription resource. The condition is resource ID matched with SubscriptionResourceType and its parent is a tenant resource.
+func isSubscriptionResource(resourceID *ResourceID) bool {
+	return resourceID != nil && strings.EqualFold(resourceID.ResourceType.String(), SubscriptionResourceType.String()) && isTenantResource(resourceID.Parent)
+}
+
+// isResourceGroupResource returns true if the resourceID represents a resource group resource. The condition is resource ID matched with ResourceGroupResourceType and its parent is a subscription resource.
+func isResourceGroupResource(resourceID *ResourceID) bool {
+	return resourceID != nil && strings.EqualFold(resourceID.ResourceType.String(), ResourceGroupResourceType.String()) && isSubscriptionResource(resourceID.Parent)
+}


Test patch:
diff --git a/sdk/azcore/arm/internal/resource/resource_identifier_test.go b/sdk/azcore/arm/internal/resource/resource_identifier_test.go
index 5c1303cb367e..f5f5b9473827 100644
--- a/sdk/azcore/arm/internal/resource/resource_identifier_test.go
+++ b/sdk/azcore/arm/internal/resource/resource_identifier_test.go
@@ -236,6 +236,53 @@ func TestParseResourceIdentifier(t *testing.T) {
 			isChild:           true,
 			stringValue:       "/subscriptions/0c2f6471-1bf0-4dda-aec3-cb9272f09575/resourceGroups/myRg/providers/Microsoft.Network/virtualNetworks/myVnet/subnets",
 		},
+		"/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d/providers/Microsoft.CognitiveServices/locations/eastus/resourceGroups/avm-res-cognitiveservices-account-rg-p26m/deletedAccounts/OpenAI-cog-p26m": {
+			Parent: &ResourceID{
+				Parent: &ResourceID{
+					Parent: &ResourceID{
+						Parent:         RootResourceID,
+						SubscriptionID: "17fecd63-33d8-4e43-ac6f-0aafa111b38d",
+						ResourceType:   SubscriptionResourceType,
+						Name:           "17fecd63-33d8-4e43-ac6f-0aafa111b38d",
+						isChild:        true,
+						stringValue:    "/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d",
+					},
+					SubscriptionID: "17fecd63-33d8-4e43-ac6f-0aafa111b38d",
+					ResourceType:   NewResourceType("Microsoft.CognitiveServices", "locations"),
+					Name:           "eastus",
+					Location:       "eastus",
+					isChild:        true,
+					stringValue:    "/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d/providers/Microsoft.CognitiveServices/locations/eastus",
+				},
+				SubscriptionID: "17fecd63-33d8-4e43-ac6f-0aafa111b38d",
+				ResourceType:   NewResourceType("Microsoft.CognitiveServices", "locations/resourceGroups"),
+				Name:           "avm-res-cognitiveservices-account-rg-p26m",
+				isChild:        true,
+				stringValue:    "/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d/providers/Microsoft.CognitiveServices/locations/eastus/resourceGroups/avm-res-cognitiveservices-account-rg-p26m",
+			},
+			SubscriptionID: "17fecd63-33d8-4e43-ac6f-0aafa111b38d",
+			ResourceType:   NewResourceType("Microsoft.CognitiveServices", "locations/resourceGroups/deletedAccounts"),
+			Name:           "OpenAI-cog-p26m",
+			isChild:        true,
+			stringValue:    "/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d/providers/Microsoft.CognitiveServices/locations/eastus/resourceGroups/avm-res-cognitiveservices-account-rg-p26m/deletedAccounts/OpenAI-cog-p26m",
+		},
+		"/providers/Microsoft.Billing/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d": {
+			Parent:       RootResourceID,
+			ResourceType: NewResourceType("Microsoft.Billing", "subscriptions"),
+			Name:         "17fecd63-33d8-4e43-ac6f-0aafa111b38d",
+			stringValue:  "/providers/Microsoft.Billing/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d",
+		},
+		"/providers/Microsoft.Billing/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d/resourceGroups/test": {
+			Parent: &ResourceID{
+				Parent:       RootResourceID,
+				ResourceType: NewResourceType("Microsoft.Billing", "subscriptions"),
+				Name:         "17fecd63-33d8-4e43-ac6f-0aafa111b38d",
+				stringValue:  "/providers/Microsoft.Billing/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d",
+			},
+			ResourceType: NewResourceType("Microsoft.Billing/subscriptions", "resourceGroups"),
+			Name:         "test",
+			stringValue:  "/providers/Microsoft.Billing/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d/resourceGroups/test",
+		},
 		// invalid resource identifiers
 		"/providers/MicrosoftSomething/billingAccounts/":             nil,
 		"/MicrosoftSomething/billingAccounts/":                       nil,