internal static async Task SetValueInternalAsync()

in src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBItemValueBinder.cs [79:113]


        internal static async Task SetValueInternalAsync(JObject originalItem, T newItem, CosmosDBContext context)
        {
            // We can short-circuit here as strings are immutable.
            if (newItem is string)
            {
                return;
            }

            JObject currentValue = JObject.FromObject(newItem);

            if (HasChanged(originalItem, currentValue))
            {
                // make sure it's not the id that has changed
                if (TryGetId(currentValue, out string currentId) &&
                    !string.IsNullOrEmpty(currentId) &&
                    TryGetId(originalItem, out string originalId) &&
                    !string.IsNullOrEmpty(originalId))
                {
                    // make sure it's not the Id that has changed
                    if (!string.Equals(originalId, currentId, StringComparison.Ordinal))
                    {
                        throw new InvalidOperationException("Cannot update the 'id' property.");
                    }
                }
                else
                {
                    // If the serialzied object does not have a lowercase 'id' property, DocDB will reject it.
                    // We'll just short-circuit here since we validate that the 'id' hasn't changed.
                    throw new InvalidOperationException(string.Format("The document must have an 'id' property."));
                }

                Container container = context.Service.GetContainer(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.ContainerName);
                await container.ReplaceItemAsync<T>(newItem, originalId);
            }
        }