Azure / azure-functions-dotnet-worker

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

DateTimeConverter parses a string containing a timestamp with a timezone even if the TargetType is string #2731

Open viktorasmickunas opened 3 days ago

viktorasmickunas commented 3 days ago

Description

Context: In a Cosmos DB triggered function, Azure function receives an IReadOnlyList<Poco>, Poco is defined as:

public record Poco
{
    public string Id { get; set; }

    public string DateAsString { get; set; }
}

Although property DateAsString is of type string and, as I understand, should not be converted by DateTimeConverter since the target property is not DateTime or DateTimeOffset, it still gets parsed and consequently its timezone info is converted to the local timezone.

Steps to reproduce

  1. Create a new Cosmos DB triggered Azure function, e.g.:
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace AzureFunctionTest;

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<Poco> input,
        FunctionContext context)
    {
        foreach (var document in input)
        {
            this._logger.LogInformation(document.ToString());
        }
    }
}
  1. Define Poco as:
public record Poco
{
    public string Id { get; set; }

    public string DateAsString { get; set; }
}
  1. Create a document in container MyDatabase.MyContainer:
    {
    "id": "1",
    "dateAsString": "2024-04-29T11:46:02+08:00",
    "_rid": "NNRYAJ5akvwDAAAAAAAAAA==",
    "_self": "dbs/NNRYAA==/colls/NNRYAJ5akvw=/docs/NNRYAJ5akvwDAAAAAAAAAA==/",
    "_etag": "\"00000000-0000-0000-0e7c-920b07b101db\"",
    "_attachments": "attachments/",
    "_ts": 1727180612
    }
  2. Observe the output of the function: Poco { Id = 1, DateAsString = 2024-04-29T06:46:00+03:00 } and notice that the timezone has been converted.