swagger 配置Visual Studio使用的OpenAPI客户端生成器以修复DateTime/Offset问题

rpppsulh  于 7个月前  发布在  其他
关注(0)|答案(2)|浏览(93)

我有一个ASP.NET Core 5 Web API,它使用Swagger生成OpenAPI 3 JSON文件。在我的Blazor WASM客户端上使用Visual Studo 2019(16.8.2),我已经向该JSON文件添加了一个服务引用。
所有这些都工作得很好,除了由于某种原因,Visual Studio生成的POCO(NSWag可能基于引用)都使用DateTimeOffset
现在我想我需要设置一个设置useDateTimeOffsetbased on this,但不知道在哪里或如何这样做。

**编辑:**看起来DateTimeOffset是通用日期的推荐数据类型。但我仍然想知道如何更改选项!

如果是OpenAPI 3 json的问题,targetDate应该是日期,而dateRaised应该是日期和时间:

"InitialRequestDTO": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer",
                    "format": "int32"
                },
                "requesterName": {
                    "type": "string",
                    "nullable": true
                },
                "sponsorName": {
                    "type": "string",
                    "nullable": true
                },
                "requestTitle": {
                    "type": "string",
                    "nullable": true
                },
                "ownReference": {
                    "type": "string",
                    "nullable": true
                },
                "dateRaised": {
                    "type": "string",
                    "format": "date-time"
                },
                "details": {
                    "type": "string",
                    "nullable": true
                },
                "existingApplicationId": {
                    "type": "integer",
                    "format": "int32",
                    "nullable": true
                },
                "targetDate": {
                    "type": "string",
                    "format": "date",
                    "nullable": true
                },
                "benefits": {
                    "type": "string",
                    "nullable": true
                },
                "externalDependencies": {
                    "type": "string",
                    "nullable": true
                },
                "nameOfLargerChange": {
                    "type": "string",
                    "nullable": true
                }
            },
            "additionalProperties": false
        },

字符串

fbcarpbf

fbcarpbf1#

通过将/DateTimeType:"System.DateTime"添加到其他代码生成选项中,可以将nswag配置为使用DateTime而不是DateTimeOffset。
如果在Visual Studio中通过Connected Service进行编辑(右键单击您的项目>添加> Connected Service),它应该看起来像下面的屏幕截图。请确保从溢出菜单中“刷新”以重新生成代码。任何DateTimeOffset现在都应该替换为DateTime


的数据
建议使用DateTimeOffset,但您可能需要访问一个需要DateTime的Web API,这就是我的情况。请务必发送UTC日期,否则您可能会收到一个错误,说明您的日期不是有效的ISO 8601日期时间。

5us2dqdw

5us2dqdw2#

最后,我使用partial类来解决这个问题,添加了一个额外的属性:

[JsonIgnore]
    public DateTime ClosedOnDT
    {
        get => ClosedOn.HasValue ? ClosedOn.Value.LocalDateTime : default;
        set => ClosedOn = new DateTimeOffset(value, TimeZoneInfo.Local.GetUtcOffset(value));
    }

字符串

相关问题