RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.38k stars 532 forks source link

CSharpGeneratorSettings DateType and TimeType should use DateOnly and TimeOnly #1613

Closed collinstevens closed 1 week ago

collinstevens commented 1 year ago

CSharpGeneratorSettings is defaulting to use System.DateTimeOffset for date formats and System.TimeSpan for time formats instead of the new System.DateOnly and System.TimeOnly types.

https://github.com/RicoSuter/NJsonSchema/blob/ed250a8b9c4f86a4da40cf0061cf0172cdea0449/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs#L22

https://github.com/RicoSuter/NJsonSchema/blob/ed250a8b9c4f86a4da40cf0061cf0172cdea0449/src/NJsonSchema.CodeGeneration.CSharp/CSharpGeneratorSettings.cs#L24

The ability to override the types used for these formats exists with the following flags, but there exists another issue which prevents this from working.

'/dateType:System.DateOnly',
'/timeOnly:System.TimeOnly',

The DateFormatConverter assumes the time is a System.DateTime and therefore will throw an exception upon deserialization.

https://github.com/RicoSuter/NJsonSchema/blob/ed250a8b9c4f86a4da40cf0061cf0172cdea0449/src/NJsonSchema.CodeGeneration.CSharp/Templates/DateFormatConverter.liquid#L2-L29

There is also no ability to disable this from being generated because it only checks if the type starts with System.Date unfortunately...

https://github.com/RicoSuter/NJsonSchema/blob/ed250a8b9c4f86a4da40cf0061cf0172cdea0449/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs#L148

I don't know what the solution to this should be; obviously changing this default would be a breaking change and it can't be done without also exposing or modifying other functionality around this formatter.

For now, we are generating the clients using those flags, but removing the DateFormatConverter and associated attributes in our generation script.