Azure / Azure/azure-functions-openapi-extension
Cannot set Json Serialization settings for Swagger UI/ OpenAPI schema when using IHostApplicationBuilder
- Dominant language
- C#
- Stars
- 388
- Forks
- 202
- PR merge metrics
- No merged PRs in 30d
Description
**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
```csharp
var builder = FunctionsApplication.CreateBuilder(args);
builder.ConfigureFunctionsWebApplication();
builder.Services.AddMvc().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
});
builder.Build().Run();
```
HttpTrigger1.cs
```csharp
namespace Company.Function
{
public class HttpTrigger1
{
private readonly ILogger _logger;
public HttpTrigger1(ILogger 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:

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.
Contributor guide
Research direction
Reproduce the mismatch using Program.cs and HttpTrigger1.cs with FunctionsApplication.CreateBuilder and SnakeCaseLower serialization. Compare the executed HttpTrigger response with the OpenAPI response example in Swagger UI; done means both use the same property naming, with an appropriate test or documented verification.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp, openapi
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100