RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

Nswag CLI generates incorrect DateTime type for C# clients #2960

Open kamilslusarczyk opened 4 years ago

kamilslusarczyk commented 4 years ago

Hi,

I have below class definition:

public class SomeClass
{
    public DateTime SomeDateTime { get; set; }
}

After generating C# client with below command:

nswag openapi2csclient /runtime:NetCore31 /dateTimeType: 'System.DateTime'

(Other configuration options like URL address, output file name etc. have been ommitted for brevity).

I get an auto-generated client with method definitions containing DateTimeOffset class:

[Newtonsoft.Json.JsonProperty("someDateTime", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public System.DateTimeOffset SomeDateTime { get; set; }
RicoSuter commented 4 years ago

What is the "format" and "type" in the spec?

p-ob commented 3 years ago

Bold: Can ignore for now; it appears upgrading and clearing the autogenerated client code resolved ths issue. Leaving my comment, just in case.

@RicoSuter We're also experiencing this issue; a sample of a field with this issue in the open api JSON schema:

"contactConsentExpirationDateTime": {
    "type": "string",
    "format": "date-time",
    "nullable": true
 },

Similarly, tried specifying the date time type explicitly in the generator settings args, still comes back as System.DateTimeOffset.

On 13.7.4.

christianh25 commented 3 years ago

I'm also having this issue. Why are "date-time" converted to DateTimeOffset? Suggestion: "date-time" -> C# DateTime (as expected) "date-time-offset" -> C# DateTimeOffset ??

Is there anyway we can force to always use C# DateTime for "date-time" type?

RicoSuter commented 3 years ago

You can configure this:

image

justin-murtagh commented 2 years ago

@RicoSuter how do you do that via the command line (as this bug still exists and is technically two bugs)?

baramuse commented 1 year ago

Same issue here !

mlaily commented 10 months ago

how do you do that via the command line (as this bug still exists and is technically two bugs)?

/DateType:DateTime /DateTimeType:DateTime

AliAfsharii commented 6 months ago

just to make @mlaily comment more accurate in case it doesn't work for anyone like me /DateType:"System.DateTime" /DateTimeType:"System.DateTime"