Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for chrono NaiveDateTime #514

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ and the `ipa` is _api_ reversed. Aaand... `ipa` is also awesome type of beer :be
defining the `parameter_in` attribute. See [docs](https://docs.rs/utoipa/latest/utoipa/attr.path.html#axum_extras-feature-support-for-axum)
or [examples](./examples) for more details.
- **debug** Add extra traits such as debug traits to openapi definitions and elsewhere.
- **chrono** Add support for [chrono](https://crates.io/crates/chrono) `DateTime`, `Date`, `NaiveDate` and `Duration`
- **chrono** Add support for [chrono](https://crates.io/crates/chrono) `DateTime`, `Date`, `NaiveDate`, `NaiveDateTime` and `Duration`
types. By default these types are parsed to `string` types with additional `format` information.
`format: date-time` for `DateTime` and `format: date` for `Date` and `NaiveDate` according
`format: date-time` for `DateTime` and `NaiveDateTime` and `format: date` for `Date` and `NaiveDate` according
[RFC3339](https://www.rfc-editor.org/rfc/rfc3339#section-5.6) as `ISO-8601`. To
override default `string` representation users have to use `value_type` attribute to override the type.
See [docs](https://docs.rs/utoipa/latest/utoipa/derive.ToSchema.html) for more details.
Expand Down
8 changes: 6 additions & 2 deletions utoipa-gen/src/schema_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn is_primitive(name: &str) -> bool {
#[inline]
#[cfg(feature = "chrono")]
fn is_primitive_chrono(name: &str) -> bool {
matches!(name, "DateTime" | "Date" | "NaiveDate" | "Duration")
matches!(name, "DateTime" | "Date" | "NaiveDate" | "Duration" | "NaiveDateTime")
}

#[inline]
Expand All @@ -167,6 +167,8 @@ impl ToTokens for SchemaType<'_> {
#[cfg(feature = "chrono")]
"DateTime" => tokens.extend(quote! { utoipa::openapi::SchemaType::String }),
#[cfg(feature = "chrono")]
"NaiveDateTime" => tokens.extend(quote! { utoipa::openapi::SchemaType::String }),
#[cfg(feature = "chrono")]
"NaiveDate" => tokens.extend(quote!(utoipa::openapi::SchemaType::String)),
#[cfg(any(feature = "chrono", feature = "time"))]
"Date" | "Duration" => tokens.extend(quote! { utoipa::openapi::SchemaType::String }),
Expand Down Expand Up @@ -250,7 +252,7 @@ impl Type<'_> {

#[cfg(feature = "chrono")]
if !known_format {
known_format = matches!(name, "DateTime" | "Date" | "NaiveDate");
known_format = matches!(name, "DateTime" | "Date" | "NaiveDate" | "NaiveDateTime");
}

#[cfg(feature = "uuid")]
Expand Down Expand Up @@ -293,6 +295,8 @@ impl ToTokens for Type<'_> {
"NaiveDate" => tokens.extend(quote! { utoipa::openapi::SchemaFormat::KnownFormat(utoipa::openapi::KnownFormat::Date) }),
#[cfg(feature = "chrono")]
"DateTime" => tokens.extend(quote! { utoipa::openapi::SchemaFormat::KnownFormat(utoipa::openapi::KnownFormat::DateTime) }),
#[cfg(feature = "chrono")]
"NaiveDateTime" => tokens.extend(quote! { utoipa::openapi::SchemaFormat::KnownFormat(utoipa::openapi::KnownFormat::DateTime) }),
#[cfg(any(feature = "chrono", feature = "time"))]
"Date" => tokens.extend(quote! { utoipa::openapi::SchemaFormat::KnownFormat(utoipa::openapi::KnownFormat::Date) }),
#[cfg(feature = "uuid")]
Expand Down
5 changes: 4 additions & 1 deletion utoipa-gen/tests/schema_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2526,13 +2526,14 @@ fn derive_struct_xml_with_optional_vec() {
#[test]
fn derive_component_with_chrono_feature() {
#![allow(deprecated)] // allow deprecated Date in tests as long as it is available from chrono
use chrono::{Date, DateTime, Duration, NaiveDate, Utc};
use chrono::{Date, DateTime, NaiveDateTime, Duration, NaiveDate, Utc};

let post = api_doc! {
struct Post {
id: i32,
value: String,
datetime: DateTime<Utc>,
naive_datetime: NaiveDateTime,
date: Date<Utc>,
naive_date: NaiveDate,
duration: Duration,
Expand All @@ -2542,6 +2543,8 @@ fn derive_component_with_chrono_feature() {
assert_value! {post=>
"properties.datetime.type" = r#""string""#, "Post datetime type"
"properties.datetime.format" = r#""date-time""#, "Post datetime format"
"properties.naive_datetime.type" = r#""string""#, "Post datetime type"
"properties.naive_datetime.format" = r#""date-time""#, "Post datetime format"
"properties.date.type" = r#""string""#, "Post date type"
"properties.date.format" = r#""date""#, "Post date format"
"properties.naive_date.type" = r#""string""#, "Post date type"
Expand Down