Search code examples
visual-studioasp.net-coreswaggeropenapi-generatorasp.net-core-5.0

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


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

Solution

  • 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.