{"record":{"id":"cb0d51dd4e972ac5","repo":"microsoft/aspire","slug":"partition-key-paths-cannot-contain-null-or-empty-strings","errorCode":null,"errorMessage":"Partition key paths cannot contain null or empty strings.","messagePattern":"Partition key paths cannot contain null or empty strings\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBContainerResource.cs","lineNumber":39,"sourceCode":"    /// <summary>\n    /// Initializes a new instance of the <see cref=\"AzureCosmosDBContainerResource\"/> class.\n    /// </summary>\n    /// <param name=\"name\">The resource name.</param>\n    /// <param name=\"containerName\">The container name.</param>\n    /// <param name=\"partitionKeyPaths\">The hierarchical partition key paths.</param>\n    /// <param name=\"parent\">The parent Azure Cosmos DB database resource.</param>\n    public AzureCosmosDBContainerResource(string name, string containerName, IEnumerable<string> partitionKeyPaths, AzureCosmosDBDatabaseResource parent) : base(name)\n    {\n        ArgumentException.ThrowIfNullOrEmpty(containerName);\n        ArgumentNullException.ThrowIfNull(partitionKeyPaths);\n        var partitionKeyPathsArray = partitionKeyPaths.ToArray();\n        if (partitionKeyPathsArray.Length == 0)\n        {\n            throw new ArgumentException(\"At least one partition key path should be provided.\", nameof(partitionKeyPaths));\n        }\n        if (partitionKeyPaths.Any(string.IsNullOrEmpty))\n        {\n            throw new ArgumentException(\"Partition key paths cannot contain null or empty strings.\", nameof(partitionKeyPaths));\n        }\n        ContainerProperties = new ContainerProperties(containerName, partitionKeyPathsArray);\n\n        Parent = parent ?? throw new ArgumentNullException(nameof(parent));\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"AzureCosmosDBContainerResource\"/> class.\n    /// </summary>\n    /// <param name=\"name\">The resource name.</param>\n    /// <param name=\"containerName\">The container name.</param>\n    /// <param name=\"partitionKeyPath\">The partition key path.</param>\n    /// <param name=\"parent\">The parent Azure Cosmos DB database resource.</param>\n    public AzureCosmosDBContainerResource(string name, string containerName, string partitionKeyPath, AzureCosmosDBDatabaseResource parent) : base(name)\n    {\n        ArgumentException.ThrowIfNullOrEmpty(containerName);\n        ArgumentException.ThrowIfNullOrEmpty(partitionKeyPath);\n        ContainerProperties = new ContainerProperties(containerName, partitionKeyPath);","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBContainerResource.cs#L21-L57","documentation":"Individual entries in the partitionKeyPaths collection are validated because Cosmos DB partition key paths must be non-empty strings (typically '/somePath'). Passing a collection containing null or an empty string produces an ArgumentException naming the partitionKeyPaths parameter.","triggerScenarios":"Calling the constructor with a list like new[] { \"/id\", \"\" } or containing null — e.g. from string.Split on an empty segment, or user-supplied path strings not trimmed/validated before building the list.","commonSituations":"Parsing a comma-separated partition key string like \"/a,,/b\" and splitting without filtering empty entries; config values with trailing commas; deserializing JSON paths where one entry is null.","solutions":["Remove or fix null/empty entries before constructing: paths.Where(p => !string.IsNullOrEmpty(p)).ToArray().","Ensure each path starts with '/' and is a real property path, e.g. '/address/zipCode'.","Split configured strings with RemoveEmptyEntries: value.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).","Validate input at the configuration boundary and throw your own descriptive error before reaching the resource constructor."],"exampleFix":"// before\nvar paths = \"/customerId,,\".Split(','); // contains empty string\nAddContainer(db, \"orders\", paths); // throws\n// after\nvar paths = \"/customerId\".Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);\nAddContainer(db, \"orders\", paths);","handlingStrategy":"validation","validationCode":"if (paths.Any(string.IsNullOrEmpty))\n{\n    throw new ArgumentException(\"All partition key paths must be non-empty strings starting with '/'.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    AddContainer(db, \"orders\", paths);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"partitionKeyPaths\")\n{\n    logger.LogError(ex, \"Invalid partition key path entry for container {Container}.\", containerName);\n    throw;\n}","preventionTips":["Split configured path strings with RemoveEmptyEntries and TrimEntries.","Sanitize each path to start with '/'.","Validate user/config-supplied paths at the boundary before constructing resources."],"tags":["argument-validation","cosmosdb","azure","null-or-empty"],"backgroundTag":"invalid-argument-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"}