Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to override the JsonSerializer when using ConfigureFunctionsWebApplication for CosmoDb Input bindings #2911

Open
JayChase opened this issue Jan 3, 2025 · 2 comments
Labels
bug Something isn't working extensions: cosmos-db

Comments

@JayChase
Copy link

JayChase commented Jan 3, 2025

Description

Azure Function App issue: setting JsonSerializerOptions not working with Cosmos input bindings

Issue

Custom JSON Serialization options are ignored when deserializing CosmosDB input bindings. The main issue for my project is this then leads to Enums in third party classes (Ms Graph SDK) causing an exception.

This repo recreates the issue.

This issue us related to the FromBody issue here.

Refs

Steps to reproduce

Create a dotnet isolated Function App with a fucntion that has CosmosDb input binding...

Program.cs (the config)

var host = new HostBuilder()
       .ConfigureFunctionsWebApplication((IFunctionsWorkerApplicationBuilder builder) =>
    {
        builder.Services.Configure<JsonSerializerOptions>(jsonSerializerOptions =>
        {
            jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
            jsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
            jsonSerializerOptions.WriteIndented = true;
            jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
        });
    })
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .Build();

Message.cs (a class with an enum property)

public enum Sentiment
{
    Positive,
    Neutral,
    Negative
}

public class Message
{

    public string Id { get; set; } = string.Empty;

    //[JsonConverter(typeof(JsonStringEnumConverter))]
    public Sentiment? Sentiment { get; set; }
}

MessageFromBinding.cs (the http triggered function with the CosmosDb input binding)

[Function("MessageFromBinding")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "messageFromBinding/{messageId}")] HttpRequest req,
    [CosmosDBInput(
databaseName: "%COSMOS_DB_NAME%",
containerName: "%COSMOS_DB_CONTAINER_NAME%",
Connection  = "COSMOS_DB_CONNECTION",
Id = "{messageId}",
PartitionKey = "{messageId}")] Message cosmosDbMessage)
{
    _logger.LogInformation("cosmosDbMessage received: {@cosmosDbMessage}", cosmosDbMessage);
    _logger.LogInformation("C# HTTP trigger function processed a request.");
    return new OkObjectResult("Welcome to Azure Functions!");
}

The error

Error:System.Text.Json.JsonException: The JSON value could not be converted to System.Nullable`1[FuncJsonIssue.Models.Sentiment].
@JayChase JayChase added the potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug label Jan 3, 2025
@liliankasem
Copy link
Member

It looks like we don't use the worker serializer in the cosmos extension

return (await JsonSerializer.DeserializeAsync(item.Content, targetType, _serializerOptions))!;

But we do in blobs, for example:

return _workerOptions?.Value?.Serializer?.Deserialize(content, targetType, CancellationToken.None);

I'm not sure if Fabio's guidance applies here or not, but if it doesn't help, I think this will just have to be a fix made to the cosmos extension.

@JayChase
Copy link
Author

Hi @liliankasem. Thank you for looking at this. I have tried the workaround and it does not help.

@liliankasem liliankasem added bug Something isn't working and removed potential-bug Items opened using the bug report template, not yet triaged and confirmed as a bug labels Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working extensions: cosmos-db
Projects
None yet
Development

No branches or pull requests

2 participants