Azure 函数:自动 OpenAPI 规范中的仅限日期参数

问题描述 投票:0回答:1

我正在使用 Microsoft.Azure.WebJobs.Extensions.OpenApi 在 .net 6 中创建一个进程内 Azure 函数,以启用自动 OpenAPI/Swagger 规范。

我需要发送一个日期(没有时间)作为路径的一部分,如下所示:

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

我可以添加这样的参数:

[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)]

但它会导致规范中“fromDate”的类型 string($date-time) 。

是否可以将其设为仅限日期?

c# azure-functions swagger-ui openapi
1个回答
0
投票

我创建了一个简单的.NET 6.0 Azure Function,并在我的环境中重现了您的要求。

我尝试使用您提供的代码(具有

DateTime
类型)

[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)]

根据我的观察,它预计采用 DateTime 格式。

enter image description here

是否可以将其设为仅限日期?

是的,您可以使用 DateOnly 类型代替

DateTime
。我在我的环境中尝试过并且有效:

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

enter image description here enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.