Skip to content

Cannot set Json Serialization settings for Swagger UI/ OpenAPI schema when using IHostApplicationBuilder #677

@rhullah

Description

@rhullah

Describe the issue
When using IHostApplicationBuilder, I cannot figure out how to adjust the Json Serialization setting to match what the function returns.

To Reproduce
Steps to reproduce the behavior:

  1. Create a default HttpTrigger function app, which should use FunctionsApplication.CreateBuilder(args)
  2. Change the Json Options to use a different naming policy
  3. Update the HttpTrigger to return an object and add an appropriate [OpenApiResponseWithBody] attribute.
  4. View the Swagger UI page, the respons object will not have appropriate formatting of names, while when you run the HtttpTrigger will return proper casing.

Program.cs

var builder = FunctionsApplication.CreateBuilder(args);

builder.ConfigureFunctionsWebApplication();

builder.Services.AddMvc().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
});

builder.Build().Run();

HttpTrigger1.cs

namespace Company.Function
{
    public class HttpTrigger1
    {
        private readonly ILogger<HttpTrigger1> _logger;

        public HttpTrigger1(ILogger<HttpTrigger1> logger)
        {
            _logger = logger;
        }

        [Function("HttpTrigger1")]
        [OpenApiOperation(operationId: "Run")]
        [OpenApiResponseWithBody(HttpStatusCode.OK, "application/json", typeof(ResponseData), Description = "Some Data")]
        public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");
            return new OkObjectResult(new ResponseData { AgeInYears = 18, FullName = "Fred" });
        }
    }

    public class ResponseData
    {
        public int AgeInYears { get; set; }
        public string FullName { get; set; } = string.Empty;
    }
}

Current behavior:
Image

You can see in the screen shot that the execution response used the proper casing (SnakeCaseLower), but the Example Value Response did not.

Expected behavior
It would be ideal for the same serializer to be used in both the response objects and the Swagger UI/OpenAPI schema.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions