Repository: Azure/azure-sdk-for-net
File: eng/packages/http-client-csharp-mgmt/emitter/src/resource-detection.ts (TypeScript)

Code before:
    if (parentResourceModelId) {
      metadata.parentResourceModelId = parentResourceModelId;
    }
  }

  // For multiple-path resources (same model at different paths), detect parent-child relationships through path matching
  // This is needed when both parent and child use the same model (e.g., legacy-operations pattern)
  // This is also specific to legacy resource detection
  for (const [metadataKey, metadata] of resourcePathToMetadataMap) {
    if (!metadata.parentResourceId && metadata.resourceIdPattern) {
      // Check if this resource's path is a child of another resource's path
      for (const [otherKey, otherMetadata] of resourcePathToMetadataMap) {
        if (otherKey !== metadataKey && otherMetadata.resourceIdPattern) {
          const thisPath = metadata.resourceIdPattern;
          const potentialParentPath = otherMetadata.resourceIdPattern;

          // The child path should start with the parent path followed by a "/"
          if (
            isPrefix(potentialParentPath, thisPath) &&
            !isPrefix(thisPath, potentialParentPath)
          ) {
            metadata.parentResourceId = potentialParentPath;
            // Note: we don't set parentResourceModelId here since they share the same model
            break;
          }
        }
      }
    }
  }

  // Update the model's resourceScope based on resource scope decorator if it exists or based on the Read method's scope.
  // This is specific to legacy resource detection
  for (const [metadataKey, metadata] of resourcePathToMetadataMap) {
    const modelId = metadataKey.split("|")[0];
    const model = resourceModelMap.get(modelId);
    if (model) {
      metadata.resourceScope = getResourceScope(model, metadata.methods);
    }
  }

  // Create parent lookup context for legacy resource detection
  // In this case, parent information comes from decorators and path matching (already populated above)
  const parentLookup: ParentResourceLookupContext = {
    getParentResource: (
      resource: ArmResourceSchema
    ): ArmResourceSchema | undefined => {
      const parentModelId = resource.metadata.parentResourceModelId;
      if (!parentModelId) return undefined;

      // Find parent resource with matching model ID and a valid resourceIdPattern
      for (const r of resources) {

Review comment (suggestion):
isPrefix is doing the slit, and getSharedSegmentCount is also doing the split. So, we are still duplicating the split operations. And I feel like isPrefix can be replace by getSharedSegmentCount with checking the shared segment count. And that can be done in a separate PR.

Code after:
    if (parentResourceModelId) {
      metadata.parentResourceModelId = parentResourceModelId;
    }
  }

  // For multiple-path resources (same model at different paths), detect parent-child relationships through path matching
  // This is needed when both parent and child use the same model (e.g., legacy-operations pattern)
  // This is also specific to legacy resource detection
  for (const [metadataKey, metadata] of resourcePathToMetadataMap) {
    if (!metadata.parentResourceId && metadata.resourceIdPattern) {
      // Find the longest matching parent path (most specific parent)
      let longestParentPath: string | undefined;
      let longestParentSegmentCount = 0;
      // Check if this resource's path is a child of another resource's path
      for (const [otherKey, otherMetadata] of resourcePathToMetadataMap) {
        if (otherKey !== metadataKey && otherMetadata.resourceIdPattern) {
          const thisPath = metadata.resourceIdPattern;
          const potentialParentPath = otherMetadata.resourceIdPattern;

          // The child path should start with the parent path followed by a "/"
          if (
            isPrefix(potentialParentPath, thisPath) &&
            !isPrefix(thisPath, potentialParentPath)
          ) {
            // Use getSharedSegmentCount to get the segment count without redundant splitting
            const segmentCount = getSharedSegmentCount(
              potentialParentPath,
              thisPath
            );
            if (segmentCount > longestParentSegmentCount) {
              longestParentSegmentCount = segmentCount;
              longestParentPath = potentialParentPath;
            }
          }
        }
      }
      if (longestParentPath) {
        metadata.parentResourceId = longestParentPath;
        // Note: we don't set parentResourceModelId here since they share the same model
      }
    }
  }

  // Update the model's resourceScope based on resource scope decorator if it exists or based on the Read method's scope.
  // This is specific to legacy resource detection
  for (const [metadataKey, metadata] of resourcePathToMetadataMap) {
    const modelId = metadataKey.split("|")[0];
    const model = resourceModelMap.get(modelId);
    if (model) {
      metadata.resourceScope = getResourceScope(model, metadata.methods);
    }