4

I have a swagger definition which is already finalized so I am not able to alter attributes or propose changes. This swagger file has various date definitions like this:

fromDate:
 description: |
   - en - fromDate.....
 type: string
  format: date

In java clients this is generated as a Date attribute. In C# there is no basic implementation of Date but just DateTime which is taken as the generated datatype. This is the same for all of the 4 ways I tried when generating the client:

Swagger Editor with C# Client generation
Swagger Editor with C# #2 Client generation  (RestSharp 2)
Open API generator with C# Client generation
Open API generator with C# #2 Client generation (RestSharp 2)

I would expect a serialization pattern like yyyy-mm-dd to be created in the c# client to match the date type but this is not the case. Dates are always serialized as full date/time/timezone strings. This is not working for the server application.

The official documentation says

string date As defined by full-date - RFC3339

RFC339 full date says

 full-date = date-fullyear "-" date-month "-" date-mday

As expected my client produces wrong output data (for example 2020-12-31T23:59:60Z) and crashes when it parses incoming data in the api client class (crash occurs before I can handle the input).

When I alter the swagger to have a string type with a pattern as suggested in other posts it works but this is not how it should be, I must use the predefined swagger file.

How can I create the clients or add a special pattern that gets automatically called by the client?

Cheers

Tim
  • 5,435
  • 7
  • 42
  • 62
Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41
  • Is `fromDate` in path/query or in body? – weichch Apr 18 '20 at 05:44
  • What Swagger client generator are you using? NSwag or Swashbuckle? – Bruno Zell Apr 19 '20 at 00:05
  • @BrunoZell I use https://editor.swagger.io and https://mermade.github.io/openapi-gui/, and select C# or C#V2 as a client so it`s the most "pure" implementations – Marc Wittmann Apr 20 '20 at 09:26
  • @weichch in the body. Problem is the parser that deserialises the dates in the body json (serverside after I send a request with a date or clientside when I receive a respponse with a date) – Marc Wittmann Apr 20 '20 at 09:29
  • I thought if you generate code from swagger editor, there's a `SwaggerDateTimeConverter` created for serializing / deserializing date in body. Is that not functioning well? – weichch Apr 22 '20 at 22:45
  • @weichch I don`t see anything like that in the generated code. I try now to use the configuration setting but this will overwrite all existing patterns I suppose. I would have expected to have it automatically in the generated model class OR so I can adjust the pattern with every call – Marc Wittmann Apr 26 '20 at 21:16

0 Answers0