Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't Set Value to Null using Patch Operation #3959

Closed
brandonsmith86 opened this issue Jun 29, 2023 · 6 comments
Closed

Can't Set Value to Null using Patch Operation #3959

brandonsmith86 opened this issue Jun 29, 2023 · 6 comments
Assignees

Comments

@brandonsmith86
Copy link

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

Describe the bug
When using PatchOperation.Replace, I am unable to set value to null. I have tried both null and new JProperty(propName, null).Value. The latter was attempted due to recommendation in #2931.

To Reproduce
This is the code used to create PatchOperation: PatchOperation.Replace($"/{cosmosPropName}", propValue ?? createNullValue(cosmosPropName)).

propValue is type object?. createNullValue() creates the JProperty mentioned in the description.

Expected behavior
I would expect the property to be set to null value. Below example assumes description property is being set to null:

{
 "id": "adfda-asdgasd-adsfasdf",
  "partition_key": "aigeqwg-iuiou-qryqer",
  "description": null
}

Actual behavior
Description property is unchanged.

Environment summary
SDK Version: 3.32.0
OS Version: Windows 10

Additional context
Nothing additional to add.

@aavasthy
Copy link
Contributor

@brandonsmith86 I am able to replace the property value with null using the latest SDK version https://www.nuget.org/packages/Microsoft.Azure.Cosmos. You would need to explicitly specify the type argument for 'Replace' method.

Code example:

ItemResponse<Product> response = await container.PatchItemAsync<Product>(
           id: newItem.id,
           partitionKey: new PartitionKey(newItem.category),
           patchOperations: new[] { PatchOperation.Replace<string>($"/{cosmosPropName}", null) });

Please let me know if you run into any issues with this.

@brandonsmith86
Copy link
Author

brandonsmith86 commented Jun 29, 2023

Hi @aavasthy. Thank you for spending the time to reproduce. Do you think my problem is version, or lack of generic type? With my current implementation, I would not be able to supply type as I'm using reflection to get the value, which returns object? type.

Edit: I suppose I can do something like this. I'll check tomorrow if this resolves it (in addition to updating to latest version).

patchOperations.Add(propValue == null // use generic version of PatchOperation.Replace if value is null
  ? PatchOperation.Replace<string?>($"/{cosmosPropName}", null)
  : PatchOperation.Replace($"/{cosmosPropName}", propValue));

@aavasthy
Copy link
Contributor

@brandonsmith86 Please try with the latest version because I don't see SDK Version: 3.32.0 in here https://www.nuget.org/packages/Microsoft.Azure.Cosmos.
I was able to update the property name even with this code

string val = null;
ItemResponse<Product> response = await container.PatchItemAsync<Product>(
    id: newItem.id,
    partitionKey: new PartitionKey(newItem.category),
    patchOperations: new[] { PatchOperation.Replace($"/propertyName", val) });

@brandonsmith86
Copy link
Author

@aavasthy Decided to give it a try today. The code that I posted above did not clear out the property. Very curious...

@brandonsmith86
Copy link
Author

@aavasthy Found an issue in my logic. Updating versions does resolve it. Thank you!

@brandonsmith86
Copy link
Author

Closing this issue since updating fixed my issue.

@brandonsmith86 brandonsmith86 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants