Open ssteiner opened 1 month ago
@philipthomas-MSFT can you please take a look?
Seems like it only happens on PatchOperation.Replace
. Can repro, so will dig more into the root cause next.
FYR: https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update#supported-operations
Yes, I was going to research that. Thanks. It seems like it makes sense why it fails on Replace so not sure this is something we need to fix. @kirankumarkolli
Path API's by-definition taking string as path contracts. => Application needs to provide the right propertyNames based on serialization options/customizations. It's by-design.
In future when Typed APIs are added then SDK will automatically do the required conversion.
If you replace the patch operations in my sample with just pure Adds, it still fails. This will still fail:
List<PatchOperation> patchOperations =
[
PatchOperation.Add($"/{nameof(PhoneBookCategory.Name)}", "updated name"),
PatchOperation.Add($"/{nameof(PhoneBookCategory.SubProp)}/{nameof(TestObject.IntProp)}", 5)
];
Sorry for providing the initial sample with a Replace
, it is indeed not the right syntax to use for this case.
PatchOperation.Add($"/{nameof(PhoneBookCategory.SubProp)}/{nameof(TestObject.IntProp)}", 5)
Maps as "/SubProp/IntProp" and path doesn't (case-sensitive) exist in the document.
It's not PatchOperations (i.e. Add/Replace/etc..) the cause but the JSON paths which are case-sensitive.
API contract is string
=> Application needs to provide a valid JSON path (mapping to the expected serialization)
Okay, I get it.
May I propose that the error message be made more useful? Something along the lines of
"property 'Name' does not exist in document 'xyz
That would be a major improvement (and could also be used elsewhere.. e.g. try commenting out cosmosOptions.SerializerOptions.PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase;` and run the sample again.. this time it'll bomb out on the insert with an HTTP 400 without explanation. The explanation in that case should be 'your document has no 'id' property')
@philipthomas-MSFT how about update Path API code document in remarks section about the possibilities?
And also can you please validate if the shortlink in error/exception has related context to troubleshoot?
My documents all have the Id property named
Id
(upper case) as is the convention in C#. To get things to work with CosmosDb, I'm using theCosmosClientOptions.SerializerOptions.PropertyNamingPolicy
which I'm setting toCosmosPropertyNamingPolicy.CamelCase
to have the CosmosDb SDK write myId
asid
:This has worked fine for me so far - inserting, upserting, deleting, searching all work just fine. It's on Patch where I ran into an issue. Here's a sample to illustrate:
This will cause the HTTP 400 error on this line
var patchRes = await container.PatchItemAsync<PhoneBookCategory>(testItem.Id, new PartitionKey(testItem.Id), patchOperations);
The exception message being:
Now uncomment the line
//[JsonProperty(PropertyName = "id")]
and comment out the line
cosmosOptions.SerializerOptions.PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase;
And it'll work. So it seems we have an issue with Patch and
CosmosPropertyNamingPolicy.CamelCase
. I'd expect the original sample to work and patch the document (it works on all other operations in this sample after all - the insert, the upsert, even the delete). You can even leave the property naming override ([JsonProperty(PropertyName = "id")]
) active, as soon as thePropertyNamingPolicy
is set, you get the issue.Environment: Microsoft.Azure.Cosmos 3.42.0 .NET 8.0.7 VS2022 17.10.5