-
Couldn't load subscription status.
- Fork 200
Description
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
-
Documentation followed for Customizing JSON serialization
-
Related issues: Unable to override the JsonSerializer when using ConfigureFunctionsWebApplication
-
Note adding the JsonConverter attribute is not an option as the classes are third party (Kiota generated).
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].