{"record":{"id":"9467e186a3ea919b","repo":"OrchardCMS/OrchardCore","slug":"the-key-cannot-be-null-or-empty","errorCode":null,"errorMessage":"The 'key' cannot be null or empty","messagePattern":"The 'key' cannot be null or empty","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore.Modules/OrchardCore.Roles/ViewModels/PermissionGroupKey.cs","lineNumber":15,"sourceCode":"namespace OrchardCore.Roles.ViewModels;\n\npublic class PermissionGroupKey\n{\n    public readonly string Key;\n\n    public readonly string Title;\n\n    public string Source { get; set; }\n\n    public PermissionGroupKey(string key)\n    {\n        if (string.IsNullOrWhiteSpace(key))\n        {\n            throw new ArgumentException($\"The '{nameof(key)}' cannot be null or empty\", nameof(key));\n        }\n\n        Key = key;\n    }\n\n    public PermissionGroupKey(string key, string title) : this(key)\n    {\n        Title = title;\n    }\n\n    public override int GetHashCode() => Key.GetHashCode();\n\n    public override bool Equals(object obj)\n    {\n        var other = obj as PermissionGroupKey;\n\n        return other != null && Key.Equals(other.Key, StringComparison.Ordinal);\n    }","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore.Modules/OrchardCore.Roles/ViewModels/PermissionGroupKey.cs#L1-L33","documentation":"PermissionGroupKey's constructor validates that the group key is not null/empty/whitespace and throws ArgumentException with message \"The 'key' cannot be null or empty\". It is a fail-fast guard ensuring role permission groups always have an identifier.","triggerScenarios":"Calling new PermissionGroupKey(null), PermissionGroupKey(\"\"), or PermissionGroupKey(\" \"), including via the (key, title) overload which chains to this constructor; e.g. building permission groups from data where the key field is missing.","commonSituations":"Custom role permission providers returning groups with missing keys, deserializing permission group definitions from JSON/config with absent key values.","solutions":["Ensure the string passed as key is non-null and non-whitespace before constructing","Validate source data (recipe/JSON/provider output) for the key field","Default the key to a stable identifier when the source omits it"],"exampleFix":"// before\nvar group = new PermissionGroupKey(source.Key);\n// after\nvar group = string.IsNullOrWhiteSpace(source.Key) ? null : new PermissionGroupKey(source.Key);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException(\"key required\", nameof(key)); var g = new PermissionGroupKey(key);","typeGuard":"bool IsValidKey(string k) => !string.IsNullOrWhiteSpace(k);","tryCatchPattern":"try { group = new PermissionGroupKey(key); } catch (ArgumentException) { log.LogWarning(\"Skipped permission group with empty key\"); }","preventionTips":["Validate keys at deserialization boundaries","Never build groups from untrusted/optional data without checks","Add unit tests for empty-key inputs"],"tags":["csharp","argument-validation","orchardcore-roles"],"backgroundTag":"empty-required-field","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}