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

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

Open
rhullah opened this issue Nov 27, 2024 · 0 comments

Comments

@rhullah
Copy link

rhullah commented Nov 27, 2024

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.

@rhullah rhullah changed the title Bug report, feature request or other request Cannot set Json Serialization settings for Swagger UI/ OpenAPI schema when using IHostApplicationBuilder Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant