Configuring OpenAPI Client Generator used by Visual Studio to fix DateTime/Offset issue
Asked Answered
V

2

8

I've got an ASP.NET Core 5 Web API that uses Swagger to generate an OpenAPI3 json file. Using Visual Studo 2019 (16.8.2) on my Blazor WASM client, I've added a Service Reference to that json file.

All of this is working fine, except for some reason the POCO's generated by Visual Studio (? NSWag maybe based on the references) are all using DateTimeOffset.

Now I think I need to set a setting useDateTimeOffset based on this, but no clue where or how to do so.

EDIT: It looks like DateTimeOffset is the recommend data type for general purpose dates. But I still want to know how to change the options!

In case, it's a problem with the OpenAPI3 json, targetDate should be date only, and dateRaised should be a date and time:

            "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
        },
Verily answered 25/11, 2020 at 17:13 Comment(0)
G
3

You can configure nswag to use DateTime instead of DateTimeOffset by adding /DateTimeType:"System.DateTime" to the additional code generation options.

If editing via Connected Service in Visual Studio (right-click your project > Add > Connected Service), it should look like the following screenshot. Be sure to "refresh" from the overflow menu to regenerate the code. Any DateTimeOffset should now be replaced with DateTime.

enter image description here

DateTimeOffset is recommended, but you may have to hit a web api that is expecting a DateTime, which was my situation. Be sure to send a UTC date or you may get an error stating your date isn't a valid ISO 8601 datetime.

Guttapercha answered 14/11, 2023 at 0:0 Comment(1)
This saved me today, thanksGalle
V
0

In the end I worked around this using the partial classes to add an additional property:

    [JsonIgnore]
    public DateTime ClosedOnDT
    {
        get => ClosedOn.HasValue ? ClosedOn.Value.LocalDateTime : default;
        set => ClosedOn = new DateTimeOffset(value, TimeZoneInfo.Local.GetUtcOffset(value));
    }
Verily answered 26/7, 2021 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.