我的API使用DateTime?
公开一些endpoint,而使用DateTimeOffset?
公开其他endpoint。
我想使用OpenApi-Generator生成一个API客户端,它将为每个endpoint创建客户端代码,考虑类型之间的差异。
OpenApiGenerator提供了useDateTimeOffset=true
的选项,它将在任何地方使用DateTimeOffset
生成客户端,而不管API是否公开了DateTime
或DateTimeOffset
。因此,我不想使用它。
因此,为了解决这个问题,我尝试创建一个Filter,在DateTimeOffset的情况下,我将在其中指定它
生成器应使用类型DateTimeOffset
并使其可为空
。
这是过滤器:
public class FixDateTimeOffsetTypeSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema model, SchemaFilterContext context)
{
if (context.Type == typeof(DateTimeOffset?))
{
model.Format = "date-time-offset-nullable";
model.Type = "DateTimeOffset";
model.Nullable = true;
}
}
}
这是其输出的示例:
"/Api/MyApiEndpoint": {
"get": {
"tags": [
"General"
],
"operationId": "General_MyApiEndpoint",
"parameters": [
{
"name": "startDatetime",
"in": "query",
"schema": {
"type": "DateTimeOffset",
"format": "date-time-offset-nullable",
"nullable": true
}
},
当我运行这个并从那个JSON生成客户机时,< code>DateTimeOffset对象永远不会< code >为null 。它似乎不尊重那个指令。
我需要做什么才能使其正常工作,以便当我指定DateTimeOffset
应为可空
时,它在代码中显示为可空
?
看起来这是一个已知的问题:https://github.com/OpenAPITools/openapi-generator/pull/3530
下面是解决这个问题的请求:https://github . com/open API tools/open API-generator/pull/3530/commits/89 FD 5d 70 e 9 f 15 a5 be 9 FFE 26 C2 beddc 07770043 c 0
也检查这个链接:https://jane.readthedocs.io/en/latest/tips/nullable.html