azure-functions-dotnet-worker: CosmosDBOutput Unsupported PartitionKey value component '[]'

In a .NET 8 Isolated function (migrating from .NET 6 In-Process), I’m trying to use CosmosDBOutput to write multiple documents without knowing their types but I’m getting the following error:

System.Private.CoreLib: Exception while executing function: Functions.Function1. Microsoft.Azure.Cosmos.Client: Unsupported PartitionKey value component ‘[]’. Numeric, string, bool, null, Undefined are the only supported types.

Here is a repo:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Newtonsoft.Json;

namespace TestCosmos
{
    public class Function1
    {
        [Function("Function1")]
        [CosmosDBOutput(databaseName: "Test", containerName: "Test", CreateIfNotExists = true, PartitionKey = "/email", Connection = "cosmosConnectionString")]
        public Object[] Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
        {
            var input = @"[
                        {
                            ""email"": ""hello@toto.com"",
                            ""event"": ""processed""
                        },
                        {
                            ""email"": ""hello@toto.com"",
                            ""event"": ""deferred"",
                            ""category"": ""cat facts""
                        }]";

            dynamic data = JsonConvert.DeserializeObject(input);

            List<Object> outputList = new List<Object>();

            foreach(var item in data)
            {
                item.id = System.Guid.NewGuid().ToString();

                outputList.Add(item);
            }

            return outputList.ToArray();
        }
    }
}

Am I doing anything wrong?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (12 by maintainers)

Most upvoted comments

@kshyju > that makes sense. No reason to include in the Cosmos binding page indeed. Thanks a lot for your time.

If you use option 2 you may not be able to share POCOs between the function app and any projects that use the actual CosmosClient as CosmosClient seems to have a dependency on Newtonsoft for serialization.

You can if you set your CosmosClient with a custom System.Text.Json serializer. We are shipping support soon (https://github.com/Azure/azure-cosmos-dotnet-v3/pull/4332) that should make it easier.

@ealsur Could you help with this?