question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PatchOperation.Set cannot handle null values

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
ghostcommented, Feb 4, 2022

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));

2reactions
yang-ercommented, Jan 16, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cosmos DB "Partial Update"/Patch, cant set new property ...
The problem is, that it throws at the PatchOperation-Add() if I set second parameter to null (with message "Value cannot be null").
Read more >
Get started with Azure Cosmos DB Partial Document Update
Learn how to use the partial document update feature with the .NET, Java, and Node SDKs for Azure Cosmos DB for NoSQL.
Read more >
Leveraging Azure Cosmos DB Partial Document Update ...
Recently Cosmos DB team has announced support for partial documents updates. In this post I'm attemping to leverage this in an ASP.
Read more >
Partial Update (PATCH) With Explicit Null Values | Konrad Drozd
In this article, I will show you, how to perform a partial update with explicit values using JsonNullable and mapstruct.
Read more >
REST: Partial updates with PATCH - Michael Scharhag
In this post we will look at the different options to use HTTP PATCH for partial resource updates.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found