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

Viewed 913

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
        },
1 Answers

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));
    }
Related