{"record":{"id":"76e3b55be929423a","repo":"microsoft/aspire","slug":"invalid-azure-ai-search-role-roles-i","errorCode":null,"errorMessage":"Invalid Azure AI Search role: {roles[i]}.","messagePattern":"Invalid Azure AI Search role: (.+?)\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Search/AzureSearchExtensions.cs","lineNumber":173,"sourceCode":"        this IResourceBuilder<T> builder,\n        IResourceBuilder<AzureSearchResource> target,\n        params AzureSearchRole[] roles)\n        where T : IResource\n    {\n        if (roles is null || roles.Length == 0)\n        {\n            return builder.WithRoleAssignments(target, Array.Empty<SearchBuiltInRole>());\n        }\n\n        var builtInRoles = new SearchBuiltInRole[roles.Length];\n        for (var i = 0; i < roles.Length; i++)\n        {\n            builtInRoles[i] = roles[i] switch\n            {\n                AzureSearchRole.SearchIndexDataContributor => SearchBuiltInRole.SearchIndexDataContributor,\n                AzureSearchRole.SearchIndexDataReader => SearchBuiltInRole.SearchIndexDataReader,\n                AzureSearchRole.SearchServiceContributor => SearchBuiltInRole.SearchServiceContributor,\n                _ => throw new ArgumentException($\"Invalid Azure AI Search role: {roles[i]}.\", nameof(roles))\n            };\n        }\n\n        return builder.WithRoleAssignments(target, builtInRoles);\n    }\n}\n","sourceCodeStart":155,"sourceCodeEnd":180,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Search/AzureSearchExtensions.cs#L155-L180","documentation":"WithRoleAssignments for Azure AI Search maps each public AzureSearchRole enum value to the corresponding provisioning SearchBuiltInRole; any value outside SearchIndexDataContributor, SearchIndexDataReader, or SearchServiceContributor falls through the switch and throws ArgumentException. This guards against future or misused enum members that have no mapping to an Azure built-in role.","triggerScenarios":"Calling builder.WithRoleAssignments(...) on an Azure AI Search resource with an AzureSearchRole value that is not one of the three supported members (or default(0) if such a value exists).","commonSituations":"Passing a cast or default-initialized AzureSearchRole; upgrading packages where new enum members exist without a mapping; copying code from other Azure resources (e.g. Service Bus roles) into the Search API.","solutions":["Use only AzureSearchRole.SearchIndexDataContributor, SearchIndexDataReader, or SearchServiceContributor when calling WithRoleAssignments.","Update to the latest Aspire.Hosting.Azure.Search package, where newly added roles may be mapped.","Check each element of the roles array — the message names roles[i], so find the offending index and correct it.","If you need an unmapped Azure role, use the underlying provisioning API directly instead of the extension."],"exampleFix":"// before\nbuilder.WithRoleAssignments(search, (AzureSearchRole)42);\n// after\nbuilder.WithRoleAssignments(search, AzureSearchRole.SearchIndexDataContributor, AzureSearchRole.SearchIndexDataReader);","handlingStrategy":"validation","validationCode":"static readonly AzureSearchRole[] AllowedSearchRoles =\n[\n    AzureSearchRole.SearchIndexDataContributor,\n    AzureSearchRole.SearchIndexDataReader,\n    AzureSearchRole.SearchServiceContributor\n];\n\nif (roles.Any(r => !AllowedSearchRoles.Contains(r)))\n{\n    throw new ArgumentException(\"Unsupported AzureSearchRole value.\");\n}\nbuilder.WithRoleAssignments(search, roles);","typeGuard":"static bool IsValidSearchRole(AzureSearchRole r) =>\n    r is AzureSearchRole.SearchIndexDataContributor\n      or AzureSearchRole.SearchIndexDataReader\n      or AzureSearchRole.SearchServiceContributor;","tryCatchPattern":null,"preventionTips":["Only pass the three supported AzureSearchRole members; never cast arbitrary integers into the enum.","Avoid default(AzureSearchRole) when building role arrays.","Update the package when new role members are released so mappings exist.","Keep role-assignment calls centralized so a bad role is caught in one place."],"tags":["azure","search","role-assignments","enum"],"backgroundTag":"invalid-enum-value","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}