{"record":{"id":"921ddd3edbdd07b1","repo":"microsoft/aspire","slug":"roles-i-is-not-a-valid-nameof-azureopenairole-value","errorCode":null,"errorMessage":"'{roles[i]}' is not a valid {nameof(AzureOpenAIRole)} value.","messagePattern":"'(.+?)' is not a valid (.+?) value\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.CognitiveServices/AzureOpenAIExtensions.cs","lineNumber":286,"sourceCode":"        this IResourceBuilder<T> builder,\n        IResourceBuilder<AzureOpenAIResource> target,\n        params AzureOpenAIRole[] roles)\n        where T : IResource\n    {\n        if (roles is null || roles.Length == 0)\n        {\n            return builder.WithRoleAssignments(target, Array.Empty<CognitiveServicesBuiltInRole>());\n        }\n\n        var builtInRoles = new CognitiveServicesBuiltInRole[roles.Length];\n        for (var i = 0; i < roles.Length; i++)\n        {\n            builtInRoles[i] = roles[i] switch\n            {\n                AzureOpenAIRole.CognitiveServicesOpenAIContributor => CognitiveServicesBuiltInRole.CognitiveServicesOpenAIContributor,\n                AzureOpenAIRole.CognitiveServicesOpenAIUser => CognitiveServicesBuiltInRole.CognitiveServicesOpenAIUser,\n                AzureOpenAIRole.CognitiveServicesUser => CognitiveServicesBuiltInRole.CognitiveServicesUser,\n                _ => throw new ArgumentException($\"'{roles[i]}' is not a valid {nameof(AzureOpenAIRole)} value.\", nameof(roles))\n            };\n        }\n\n        return builder.WithRoleAssignments(target, builtInRoles);\n    }\n}\n","sourceCodeStart":268,"sourceCodeEnd":293,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.CognitiveServices/AzureOpenAIExtensions.cs#L268-L293","documentation":"WithRoleAssignments maps each AzureOpenAIRole enum value to the corresponding Azure.Provisioning CognitiveServicesBuiltInRole. If a role value has no mapping in the switch expression, the library throws this ArgumentException because it cannot translate the role into a built-in role assignment. This typically indicates a new AzureOpenAIRole member was added without updating this extension, or an invalid cast produced an out-of-range value.","triggerScenarios":"Calling WithRoleAssignments on an Azure OpenAI resource builder with an AzureOpenAIRole value not handled by the switch (CognitiveServicesOpenAIContributor, CognitiveServicesOpenAIUser, CognitiveServicesUser are the only supported values), e.g. after casting an arbitrary int/enum to AzureOpenAIRole or using a newer enum member with an older extension version.","commonSituations":"Dynamically constructing role lists from config files or strings cast to the enum; upgrading the Azure.Provisioning / Aspire packages where a new AzureOpenAIRole member exists but the extension mapping lags; typos leading to default enum values like 0 not corresponding to a defined member.","solutions":["Use only the supported AzureOpenAIRole members: CognitiveServicesOpenAIContributor, CognitiveServicesOpenAIUser, CognitiveServicesUser","Parse role names with Enum.TryParse<AzureOpenAIRole> and validate each value against the supported set before calling WithRoleAssignments","Update Aspire.Hosting.Azure.CognitiveServices (and Azure.Provisioning) packages so new enum members have mappings"],"exampleFix":"// before\nvar role = (AzureOpenAIRole)intValue; // unmapped value -> ArgumentException\nbuilder.AddOpenAI(\"ai\").WithRoleAssignments(target, [role]);\n// after\nif (Enum.IsDefined(role) && role is AzureOpenAIRole.CognitiveServicesOpenAIContributor\n    or AzureOpenAIRole.CognitiveServicesOpenAIUser or AzureOpenAIRole.CognitiveServicesUser)\n{\n    builder.AddOpenAI(\"ai\").WithRoleAssignments(target, [role]);\n}","handlingStrategy":"validation","validationCode":"AzureOpenAIRole[] supported = [AzureOpenAIRole.CognitiveServicesOpenAIContributor, AzureOpenAIRole.CognitiveServicesOpenAIUser, AzureOpenAIRole.CognitiveServicesUser];\nif (roles.Any(r => !supported.Contains(r))) throw new InvalidOperationException($\"Unsupported AzureOpenAIRole: {roles.First(r => !supported.Contains(r))}\");","typeGuard":"static bool IsValidAzureOpenAIRole(AzureOpenAIRole r) => r is AzureOpenAIRole.CognitiveServicesOpenAIContributor or AzureOpenAIRole.CognitiveServicesOpenAIUser or AzureOpenAIRole.CognitiveServicesUser;","tryCatchPattern":"try { builder.WithRoleAssignments(target, roles); } catch (ArgumentException ex) when (ex.ParamName == \"roles\") { /* log and surface unsupported role */ }","preventionTips":["Never cast raw ints/strings to AzureOpenAIRole without Enum.TryParse/IsDefined","Keep a whitelist of supported roles next to the call site","Pin package versions so enum and extension mappings stay in sync"],"tags":["azure","role-assignments","enum","argument"],"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"}