OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.8k stars 6.58k forks source link

[REQ](rust, client) Use a proper Date and DateTime type for deserializing dates #19319

Open SorenHolstHansen opened 3 months ago

SorenHolstHansen commented 3 months ago

Is your feature request related to a problem? Please describe.

It is cumbersome to have to parse dates into datetime types yourself when it can be done by serde automatically.

Describe the solution you'd like

Since there are some intricacies to it, it would be best if it could be enabled with a cli flag to specify what date-time format (or, just, date format) to expect. Probably something like

openapi-generator ... --datetime-format=Iso8601

If the flag is left out, the dates will be strings, otherwise it will deserialize the string to a date using the given format.

I would be fine with just enabling the formats specified here, but if more obscure formats are needed, we could do something like the time-crates format descriptions

Describe alternatives you've considered

Alternative would be to leave as is.

trombonehero commented 2 months ago

The rust-server generator uses chrono::NaiveDate and chrono::NaiveDateTime (see #9769); it would be nice to use those types in generated models, too, e.g.,:

use chrono::NaiveDateTime;
// ...
pub struct Post {
    // ...
    #[serde(rename = "posted")]
    pub posted: NaiveDateTime,
}

I do this manually right now, using Git to discard the NaiveDateTime->String conversion every time I regenerate from the API spec.

I looked at doing this via templates for the first part (use), but the data type itself comes from {{{dataType}}} in the middle of the template, which seems better addressed through the generator tool itself.