Contents

Error "The input content is invalid because the required properties - 'id; ' - are missing" when using CosmosDB

Contents

I’ve been working on an Azure Functions project in the .NET 5 isolated model, and while I am loving the experience, there are a few small annoyances along the way.

One of those is when using the Microsoft.Azure.Cosmos Nuget package, which is currently at 3.x and relies on Newtonsoft.Json for serialization of CosmosDB documents.

See, from .NET Core 3.0 and above, JSON serialization has been available through the System.Text.Json namespace, and although they are still working on parity of features, since .NET 5 it is at a great place to use instead of Newtonsoft.Json.

When I gave my database entity JSON property names using newer JsonPropertyName attribute…

using System.Text.Json.Serialization;

...

public record ExampleEntity<T>
{
    [JsonPropertyName("id")\]
    public string Id { get; init; }
    
    [JsonPropertyName("body")\]
    public T Body { get; init; }
    
    [JsonPropertyName("partitionKey")\]
    public string PartitionKey { get; init; }
}

I would come across the error The input content is invalid because the required properties - 'id; ' - are missing

The error is misleading as I had an id, and was even making sure that the name was in the right case.

For any projects that are using Microsoft.Azure.Cosmos v3, entities will need to use the JsonProperty attribute instead.

using Newtonsoft.Json;

...

public record ExampleEntity<T>
{
    [JsonProperty("id")\]
    public string Id { get; init; }
    
    [JsonProperty("body")\]
    public T Body { get; init; }
    
    [JsonProperty("partitionKey")\]
    public string PartitionKey { get; init; }
}

This intention for v4 of Microsoft.Azure.Cosmos is to remove any dependency of Newtonsoft.Json, and allow you to use System.Text.Json instead. But for now this is what you need to do.