Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
427 stars 182 forks source link

Strings containing a DateTime formatted value are parsed and converted to server's time zone even when Newtonsoft JSON serializer is used and date parsing is disabled when using a Cosmos DB trigger #2442

Open mickvikt opened 5 months ago

mickvikt commented 5 months ago

Description

Context of our problem:

The problem:

Strings containing dates are converted to server's time zone.

Steps to reproduce

  1. Create a new Cosmos DB triggered Azure function, accepting an IReadOnlyList<JObject>:
public class MyCosmosDbTrigger
{
    private readonly ILogger _logger;

    public MyCosmosDbTrigger(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<MyCosmosDbTrigger>();
    }

    [Function("MyCosmosDbTrigger")]
    public void Run([CosmosDBTrigger(
            databaseName: "MyDatabase",
            containerName: "MyContainer",
            Connection = "CosmosDbConnectionString",
            LeaseContainerName = "leases",
            CreateLeaseContainerIfNotExists = true)] 
        IReadOnlyList<JObject> input,
        FunctionContext context)
    {
        foreach (var document in input)
        {
            this._logger.LogInformation(document.ToString());
        }
    }
}
  1. Create database MyDatabase and a new container MyContainer in your Cosmos DB emulator
  2. Set CosmosDbConnectionString in local.settings.json to your Cosmos DB emulator connection string
  3. Add nuget package Microsoft.Azure.Core.NewtonsoftJson
  4. Configure functions worker defaults in Program.cs to use Newtonsoft JSON as a serializer and set DateParseHandling to DateParseHandling.None
var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults(builder =>
    {
        builder.Services.Configure<WorkerOptions>(options =>
        {
            var settings = NewtonsoftJsonObjectSerializer.CreateJsonSerializerSettings();
            settings.DateParseHandling = DateParseHandling.None;
            options.Serializer = new NewtonsoftJsonObjectSerializer(settings);
        });
    })
    .Build();

host.Run();
  1. Run the function
  2. Create a new document containing data provided below in container MyContainer of the MyDatabase database of your Cosmos DB emulator to trigger function execution:
{
    "id": "1",
    "dateAsString": "2024-04-29T11:35:00+08:00"
}
  1. Observe in the log output that date is converted to server's time zone. Sample log output:
[2024-05-06T13:06:45.943Z] Executing 'Functions.MyCosmosDbTrigger' (Reason='New changes on container MyContainer at 2024-05-06T13:06:45.9175858Z', Id=c36d2261-fd79-43be-9128-81940a4e2598)
[2024-05-06T13:06:46.041Z] {
[2024-05-06T13:06:46.043Z]   "id": "1",
[2024-05-06T13:06:46.043Z]   "dateAsString": "2024-04-29T06:35:00+03:00",
[2024-05-06T13:06:46.044Z]   "_rid": "NNRYAJ5akvwDAAAAAAAAAA==",
[2024-05-06T13:06:46.045Z]   "_self": "dbs/NNRYAA==/colls/NNRYAJ5akvw=/docs/NNRYAJ5akvwDAAAAAAAAAA==/",
[2024-05-06T13:06:46.045Z]   "_etag": "\"00000000-0000-0000-9fb6-3d892aca01da\"",
[2024-05-06T13:06:46.046Z]   "_attachments": "attachments/",
[2024-05-06T13:06:46.047Z]   "_ts": 1715000802,
[2024-05-06T13:06:46.047Z]   "_lsn": 98
[2024-05-06T13:06:46.048Z] }
[2024-05-06T13:06:46.060Z] Executed 'Functions.MyCosmosDbTrigger' (Succeeded, Id=c36d2261-fd79-43be-9128-81940a4e2598, Duration=134ms)
SilvijaC commented 5 months ago

I have the same issue

mickvikt commented 4 months ago

Is there any developments on this problem? This is the only reason keeping us from migrating to isolated model!