4

Below is the code that I have:

List<PatchOperation> patchOperations = new List<PatchOperation>();
            patchOperations.Add(PatchOperation.Replace("/endpointId", 100));
string id = "id1";
PartitionKey partitionKey = new PartitionKey("partitionkey1");

await _container.PatchItemAsync<Watermark>(id,
    partitionKey,
    patchOperations);

I am expecting to get endpointId property to be replaced with 100.

However, I am faced with Message: {"Errors":["One of the specified inputs is invalid"]}.

May I check which part am I missing or do I have to wait for patch private preview feature to be enabled for my cosmos db?

chiaDev
  • 389
  • 3
  • 17
  • The partition key is supposed to have a String value. – Sajeetharan Sep 06 '21 at 07:16
  • My partition key is a string type. The PatchItemAsync signature is PatchItemAsync(string id, PartitionKey partitionKey, IReadOnlyList patchOperations, PatchItemRequestOptions requestOptions = null, CancellationToken cancellationToken = default). Hence I create the new partition key and passed into PatchItemAsync – chiaDev Sep 06 '21 at 08:06
  • you don't have access to patch preview? – Sajeetharan Sep 06 '21 at 08:26
  • I just applied for it, still pending for response from the team. But is this the reason why I am facing the issue? – chiaDev Sep 06 '21 at 08:29

5 Answers5

6

For anyone else landing here looking for the reason why Cosmos might respond with One of the specified inputs is invalid for other request types, you may need to rename your Id property to id lower-cased or add an attribute:

[JsonProperty("id")]
public string Id { get; set; }
Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
1

id attribite in cosmos db is case sensitive. Try to replace your id getter setter with "id" this case.

Anushree Garg
  • 162
  • 1
  • 4
1

Another reason for this error message could be if you forget the forward slash before your property name:

await container.PatchItemAsync<T>(item.Id, new PartitionKey(item.PartitionKey), new[]
{
    PatchOperation.Add("isDeleted", true),
    PatchOperation.Add("ttl", DefaultTtl),
});

The correct code would be:

await container.PatchItemAsync<T>(item.Id, new PartitionKey(item.PartitionKey), new[]
{
    PatchOperation.Add("/isDeleted", true),
    PatchOperation.Add("/ttl", DefaultTtl),
});
Oliver
  • 43,366
  • 8
  • 94
  • 151
0

Even though error says "One of the specified inputs is invalid" you should see a status code 400 which indicates Bad Request. So Private Preview feature needs to be enabled for your account.

You can also enable Patch with your emulator by adding EnablePreview when you start the emulator,

.\CosmosDB.Emulator.exe /EnablePreview

In order to get it enabled you can register using this form and it should be done in 15-20 minutes. Let me know if that does not work

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • I have submitted the form yesterday. Do u know how long it will take for the feature to be enabled? – chiaDev Sep 07 '21 at 02:07
  • Thats great! Yes, I have followed the instruction. My sub id is b57bffe2-3e1b-49fe-82b1-1aba60464293 looking forward to test out the patch feature! – chiaDev Sep 07 '21 at 05:31
0

One option is to set the CosmosSerializationOptions to CamelCase when the client is built. This forces all the properties to be written in CamelCase. Then in your JsonSerializer, set the options to be CaseInsensitive;

Build Cosmos Client:

builder.Services.AddSingleton(s =>
        {
            string connectionString = Configuration["CosmosDbConnection"];
            if (string.IsNullOrWhiteSpace(connectionString))
                throw new InvalidOperationException("The Cosmos Database connection was not found in appsettings.json");

            CosmosSerializationOptions serializerOptions = new()
            {
                PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase,
            };
            return new CosmosClientBuilder(connectionString)
            .WithSerializerOptions(serializerOptions)
            .Build();
        });

Set JsonSerializerOptions

private static readonly JsonSerializerOptions _serializerOptions = new()
{
    PropertyNameCaseInsensitive = true,
    MaxDepth = 64,
};

return JsonSerializer.Serialize<T>(data, _serializerOptions);
ggrewe1959
  • 87
  • 1
  • 3
  • 12