So after an hour of frustration, I finally managed to figure out why I wasn’t able to do a simple Insert in a Cosmos collection using the latest v3 dotnet sdk
For the first time in a while I was playing with the raw SDK(shameless plug - I usually use the wrapper I wrote that abstracts away a lot of the internals of dealing with the documents and SDK itself) and I was getting this weird exeption while trying to perform the most basic Insert/Upsert operation:
1 | await _container.UpsertItemAsync(movie, new PartitionKey(movie.Title)) |
__container’s PartitionKey definition is /Title_
1 | Microsoft.Azure.Cosmos.CosmosException: 'Response status code does not indicate success: 400 Substatus: 0 Reason: (Message: {"Errors":["One of the specified inputs is invalid"]} |
Here’s the full definition of the object I’m trying to insert:
1 | public class Movie |
Can you spot the issue?
As it turns out it’s the id
property, or more exactly the lack of an id
property. A valid Cosmos document must always define an id
(yes, case sensitive) property.
The fix is relatively easy, either rename the property to id
(not very nice) or use an attribute to tell the serializer to change it:
1 | [ ] |
Hopefully this article will show up when you desperately google “CosmosDB [One of the specified inputs is invalid]” as I have :)