NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

Nswag CLI generates incorrect DateTime type for C# clients

Open kamilslusarczyk opened this issue 4 years ago • 8 comments

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

kamilslusarczyk avatar Jul 17 '20 09:07 kamilslusarczyk

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

RicoSuter avatar Sep 29 '20 11:09 RicoSuter

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.

p-ob avatar Dec 21 '20 15:12 p-ob

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?

christianh25 avatar May 03 '21 17:05 christianh25

You can configure this:

image

RicoSuter avatar May 07 '21 13:05 RicoSuter

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

justin-murtagh avatar Jul 21 '22 08:07 justin-murtagh

Same issue here !

baramuse avatar Jan 17 '23 14:01 baramuse

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

/DateType:DateTime /DateTimeType:DateTime

mlaily avatar Dec 12 '23 17:12 mlaily

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

AliAfsharii avatar Apr 07 '24 22:04 AliAfsharii