Search code examples
c#azure-functionsswagger-uiopenapi

Azure function: Date only parameter in automatic OpenAPI spec


I'm creating an in-process Azure function in .net 6 using Microsoft.Azure.WebJobs.Extensions.OpenApi to enable automatic OpenAPI/Swagger specification.

I need to send in a date (without time) as a part of the path, like this:

[HttpTrigger(AuthorizationLevel.Function, "get", Route = "history/{itemId}/{channel}/{fromDate}/{toDate}")] HttpRequest req,

I can add a paramter like this:

[OpenApiParameter(name: "fromDate", In = ParameterLocation.Query, Required = true, Type = typeof(DateTime), Summary = "From date", Description = "The date to fetch prices from. Only the date part is used (time is ignored).", Visibility = OpenApiVisibilityType.Important)]

but it will result in the type string($date-time) for "fromDate" in the spec.

Is it possible to make it a date-only?


Solution

  • I implemented an IDocumentFilter to alter the format of the DateTime parameter to date which gave me the correct type in swagger.json. However I could not try it out in the Swagger UI. The parameter could not be parsed. I believe it has to do with lacking support json deserialization of DateOnly in .net 6.

    Until someone proves it is possible, I will mark this as answered (and not possible to do).