azure-cosmos-dotnet-v3: PatchOperation.Set cannot handle null values

We are continuously addressing and improving the SDK, if possible, make sure the problem persist in the latest SDK version.

Describe the bug PatchOperation.Set throws ArgumentNullException when value is null.

To Reproduce await container.PatchItemAsync<T>(id, PartitionKey.None, new List<PatchOperation> { PatchOperation.Set<string>(“/name”, null) });

Expected behavior Value should be set to null.

Actual behavior

System.ArgumentNullException: Value cannot be null. (Parameter ‘value’) at Microsoft.Azure.Cosmos.PatchOperationCore`1…ctor(PatchOperationType operationType, String path, T value) at Microsoft.Azure.Cosmos.PatchOperation.Set[T](String path, T value)

Environment summary SDK Version: 3.23.0 OS Version (e.g. Windows, Linux, MacOSX) Windows 11

Additional context According to @sajeetharan (https://stackoverflow.com/a/70194090/22092), this seems to be a known issue but I couldn’t find any matching issue in github. image

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

This works for me.

Instead of using null I just create a JProperty with the value null. And use that value.

private JToken NullValue { get { return new JProperty("nullValue", null).Value; } } And use it like this operations.Add(PatchOperation.Set("\customer", value ?? NullValue));

Hi, I have a workaround as below which could resolve your problem:

await container.PatchItemAsync(id, PartitionKey.None, new List { PatchOperation.Set("/name", new MemoryStream(Encoding.UTF8.GetBytes("null"))) });

My thought is that here we shouldn’t add null checks for value.

Yep, thanks, that fixes it

BTW, a much easier way to get a null JToken: JValue.CreateNull()