RicoSuter / NSwag

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

Support DateFormatConverter using other formats #1888

Open jnovick opened 5 years ago

jnovick commented 5 years ago

DateFormatConverter always generates with

    public DateFormatConverter()
    {
        DateTimeFormat = "yyyy-MM-dd";
    }

The yyyy-MM-dd should be customizable for interacting with APIs that use other formats.

RicoSuter commented 5 years ago

You can override the template: https://github.com/RSuter/NSwag/wiki/Templates

Or we add a setting for that

RicoSuter commented 5 years ago

The template is probably in NJsonSchema

jnovick commented 5 years ago

I found it: https://github.com/RSuter/NJsonSchema/blob/a7c1beadf18958eb45c13e45852e136b09344630/src/NJsonSchema.CodeGeneration.CSharp/Templates/DateFormatConverter.liquid#L6

I do think this should be a more easily customizable field though.

jnovick commented 5 years ago

I was able to resolve our issue by including a changed DateFormatConverter.liquid file and setting templateDirectory to point at it. Thank you for your help. Having this be configurable would probably benefit others so I don't think this issue should be closed, but I no longer require this change so no need to rush to help me.

rbev commented 5 years ago

I've just run into the issue of a platform providing null dates as "0000-00-00" which will not deserialise cleanly with this converter.

I see two fairly clean solutions that would solve this one

  1. provide configuration to disable the generation of this class & it's attributes entirely (allowing use of SwaggerToCSharpClientGeneratorSettings.CSharpGeneratorSettings.JsonConverters to be used in it's place)
  2. provide configuration for providing a custom type name to use for this converter type in object attributes (and disable generation of the class)
RicoSuter commented 5 years ago

You can override/implement your own converter by override the DateFormatConverter.liquid template

rbev commented 5 years ago

Yeah I realise that, and i've done that for now although it feels a bit messy since I need some global type converters anyway and it would have been nice to include them all the same way.

RicoSuter commented 5 years ago

You could leave the template file empty and define a converter somewhere else with the name and add the using with AdditionalNamespaceUsages

RicoSuter commented 5 years ago

But yes, changing the tpl feels messy but so does adding a new setting for every scenario... each setting adds new complexity for everyone and increases maintenance cost. Also the generated clients should be self contained, ie should just work correctly for the spec and should not require any external changes/implementations.

Maybe we should just change the date converter to convert 0000-00-00 to null if the target type is DateTime? ? Dont see why everyone would want the actual year 0 in a nullable datetime?