Getting "One of the specified inputs is invalid" in Azure CosmosDb PatchItemAsync
Asked Answered
T

5

5

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?

Tendency answered 6/9, 2021 at 7:8 Comment(4)
The partition key is supposed to have a String value.Desmid
My partition key is a string type. The PatchItemAsync signature is PatchItemAsync<T>(string id, PartitionKey partitionKey, IReadOnlyList<PatchOperation> patchOperations, PatchItemRequestOptions requestOptions = null, CancellationToken cancellationToken = default). Hence I create the new partition key and passed into PatchItemAsyncTendency
you don't have access to patch preview?Desmid
I just applied for it, still pending for response from the team. But is this the reason why I am facing the issue?Tendency
D
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

Desmid answered 6/9, 2021 at 17:27 Comment(2)
I have submitted the form yesterday. Do u know how long it will take for the feature to be enabled?Tendency
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!Tendency
T
9

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; }
Tangled answered 13/9, 2021 at 1:35 Comment(0)
A
3

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),
});
Aerophobia answered 26/8, 2022 at 12:36 Comment(0)
B
3

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);
Barrington answered 8/3, 2023 at 13:36 Comment(1)
This solution solved my issue with inserting data into Azure Cosmos DB NoSQL.Malleolus
B
1

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

Blandishment answered 19/1, 2022 at 17:11 Comment(1)
Also, make sure that id is a top-level prop (*facepalm).Africa
D
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

Desmid answered 6/9, 2021 at 17:27 Comment(2)
I have submitted the form yesterday. Do u know how long it will take for the feature to be enabled?Tendency
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!Tendency

© 2022 - 2024 — McMap. All rights reserved.