Skip to content

Commit ac71b23

Browse files
Turbo87juhaku
andauthored
Add support for jiff v0.2 (#1332)
This commit adds support for v0.2 of the `jiff` date-time crate, similar to how it is already implemented for `chrono` and `time`. --------- Co-authored-by: Juha Kukkonen <juha7kukkonen@gmail.com>
1 parent 2e19e9a commit ac71b23

File tree

10 files changed

+79
-4
lines changed

10 files changed

+79
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ and the `ipa` is _api_ reversed. Aaand... `ipa` is also an awesome type of beer
8484
By default these types are parsed as `string`. `OffsetDateTime` and `PrimitiveDateTime` will use `date-time` format. `Date` will use
8585
`date` format and `Duration` will not have any format. To override default `string` representation users have to use `value_type` attribute
8686
to override the type. See [docs](https://docs.rs/utoipa/latest/utoipa/derive.ToSchema.html) for more details.
87+
- **`jiff_0_2`** Add support for [jiff 0.2](https://crates.io/crates/jiff) `Zoned`, and `civil::Date` types.
88+
By default these types are parsed as `string`. `Zoned` will use `date-time` format. `civil::Date` will use
89+
`date` format. To override default `string` representation users have to use `value_type` attribute
90+
to override the type. See [docs](https://docs.rs/utoipa/latest/utoipa/derive.ToSchema.html) for more details.
8791
- **`decimal`**: Add support for [rust_decimal](https://crates.io/crates/rust_decimal) `Decimal` type. **By default**
8892
it is interpreted as `String`. If you wish to change the format you need to override the type.
8993
See the `value_type` in [component derive docs](https://docs.rs/utoipa/latest/utoipa/derive.ToSchema.html).

scripts/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ for crate in $crates; do
1919
if [[ "$crate" == "utoipa" ]]; then
2020
$CARGO ${CARGO_COMMAND} -p utoipa --features openapi_extensions,preserve_order,preserve_path_order,debug,macros
2121
elif [[ "$crate" == "utoipa-gen" ]]; then
22-
$CARGO ${CARGO_COMMAND} -p utoipa-gen --features utoipa/actix_extras,chrono,decimal,utoipa/uuid,uuid,utoipa/ulid,ulid,utoipa/url,url,utoipa/time,time,utoipa/repr,utoipa/smallvec,smallvec,rc_schema,utoipa/rc_schema,utoipa/macros
22+
$CARGO ${CARGO_COMMAND} -p utoipa-gen --features utoipa/actix_extras,chrono,decimal,utoipa/uuid,uuid,utoipa/ulid,ulid,utoipa/url,url,utoipa/time,time,jiff_0_2,utoipa/repr,utoipa/smallvec,smallvec,rc_schema,utoipa/rc_schema,utoipa/macros
2323
$CARGO ${CARGO_COMMAND} -p utoipa-gen --test schema_derive_test --features decimal_float,utoipa/macros
2424

2525
$CARGO ${CARGO_COMMAND} -p utoipa-gen --test path_derive_auto_into_responses --features auto_into_responses,utoipa/uuid,uuid,utoipa/macros

utoipa-gen/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog - utoipa-gen
22

3+
## Unreleased
4+
5+
### Added
6+
7+
* Add support for jiff v0.2 (https://github.com/juahku/utoipa/pull/1332)
8+
39
## 5.3.1 - Jan 6 2025
410

511
### Fixed

utoipa-gen/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ smallvec = { version = "1.10", features = ["serde"] }
5151
rust_decimal = { version = "1", default-features = false }
5252
chrono = { version = "0.4", features = ["serde"] }
5353
time = { version = "0.3", features = ["serde-human-readable"] }
54+
jiff = { version = "0.2", features = ["serde"] }
5455
serde_with = "3.0"
5556
insta = { version = "1.41", features = ["json"] }
5657

@@ -69,6 +70,7 @@ ulid = ["dep:ulid"]
6970
url = ["dep:url"]
7071
axum_extras = ["regex", "syn/extra-traits"]
7172
time = []
73+
jiff_0_2 = []
7274
smallvec = []
7375
repr = []
7476
indexmap = []

utoipa-gen/src/schema_type.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ impl SchemaType<'_> {
135135
);
136136
}
137137

138+
#[cfg(feature = "jiff_0_2")]
139+
if !primitive {
140+
primitive = matches!(name, "Zoned" | "Date");
141+
}
142+
138143
primitive
139144
}
140145
}
@@ -268,7 +273,7 @@ impl ToTokensDiagnostics for SchemaType<'_> {
268273
schema_type_tokens(tokens, SchemaTypeInner::String, self.nullable)
269274
}
270275

271-
#[cfg(any(feature = "chrono", feature = "time"))]
276+
#[cfg(any(feature = "chrono", feature = "time", feature = "jiff_0_2"))]
272277
"Date" | "Duration" => {
273278
schema_type_tokens(tokens, SchemaTypeInner::String, self.nullable)
274279
}
@@ -295,6 +300,8 @@ impl ToTokensDiagnostics for SchemaType<'_> {
295300
"PrimitiveDateTime" | "OffsetDateTime" => {
296301
schema_type_tokens(tokens, SchemaTypeInner::String, self.nullable)
297302
}
303+
#[cfg(feature = "jiff_0_2")]
304+
"Zoned" => schema_type_tokens(tokens, SchemaTypeInner::String, self.nullable),
298305
_ => schema_type_tokens(tokens, SchemaTypeInner::Object, self.nullable),
299306
};
300307

@@ -399,7 +406,7 @@ impl KnownFormat {
399406
#[cfg(feature = "chrono")]
400407
"DateTime" | "NaiveDateTime" => Self::DateTime,
401408

402-
#[cfg(any(feature = "chrono", feature = "time"))]
409+
#[cfg(any(feature = "chrono", feature = "time", feature = "jiff_0_2"))]
403410
"Date" => Self::Date,
404411

405412
#[cfg(feature = "decimal_float")]
@@ -416,6 +423,9 @@ impl KnownFormat {
416423

417424
#[cfg(feature = "time")]
418425
"PrimitiveDateTime" | "OffsetDateTime" => Self::DateTime,
426+
427+
#[cfg(feature = "jiff_0_2")]
428+
"Zoned" => Self::DateTime,
419429
_ => Self::Unknown,
420430
};
421431

@@ -694,8 +704,13 @@ impl PrimitiveType {
694704
syn::parse_quote!(String)
695705
}
696706

707+
#[cfg(any(feature = "chrono", feature = "time", feature = "jiff_0_2"))]
708+
"Date" => {
709+
syn::parse_quote!(String)
710+
}
711+
697712
#[cfg(any(feature = "chrono", feature = "time"))]
698-
"Date" | "Duration" => {
713+
"Duration" => {
699714
syn::parse_quote!(String)
700715
}
701716

@@ -733,6 +748,11 @@ impl PrimitiveType {
733748
"PrimitiveDateTime" | "OffsetDateTime" => {
734749
syn::parse_quote!(String)
735750
}
751+
752+
#[cfg(feature = "jiff_0_2")]
753+
"Zoned" => {
754+
syn::parse_quote!(String)
755+
}
736756
_ => {
737757
// not a primitive type
738758
return None;

utoipa-gen/tests/schema_derive_test.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,19 @@ fn derive_component_with_time_feature() {
16651665
assert_json_snapshot!(&times);
16661666
}
16671667

1668+
#[cfg(feature = "jiff_0_2")]
1669+
#[test]
1670+
fn derive_component_with_jiff_0_2_feature() {
1671+
let doc = api_doc! {
1672+
struct Timetest {
1673+
civil_date: jiff::civil::Date,
1674+
zoned: jiff::Zoned,
1675+
}
1676+
};
1677+
1678+
assert_json_snapshot!(&doc);
1679+
}
1680+
16681681
#[test]
16691682
fn derive_struct_component_field_type_override() {
16701683
let post = api_doc! {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: utoipa-gen/tests/schema_derive_test.rs
3+
expression: "&doc"
4+
---
5+
{
6+
"properties": {
7+
"civil_date": {
8+
"format": "date",
9+
"type": "string"
10+
},
11+
"zoned": {
12+
"format": "date-time",
13+
"type": "string"
14+
}
15+
},
16+
"required": [
17+
"civil_date",
18+
"zoned"
19+
],
20+
"type": "object"
21+
}

utoipa/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ to look into changes introduced to **`utoipa-gen`**.
55

66
## Unreleased
77

8+
### Added
9+
10+
* Add support for jiff v0.2 (https://github.com/juahku/utoipa/pull/1332)
11+
812
### Changed
913

1014
* Replaced `serde_yaml` with `serde_norway` (https://github.com/juhaku/utoipa/pull/1311)

utoipa/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ uuid = ["utoipa-gen?/uuid"]
4343
ulid = ["utoipa-gen?/ulid"]
4444
url = ["utoipa-gen?/url"]
4545
time = ["utoipa-gen?/time"]
46+
jiff_0_2 = ["utoipa-gen?/jiff_0_2"]
4647
smallvec = ["utoipa-gen?/smallvec"]
4748
indexmap = ["utoipa-gen?/indexmap"]
4849
openapi_extensions = []

utoipa/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
//! By default these types are parsed as `string`. `OffsetDateTime` and `PrimitiveDateTime` will use `date-time` format. `Date` will use
8282
//! `date` format and `Duration` will not have any format. To override default `string` representation users have to use `value_type` attribute
8383
//! to override the type. See [docs](https://docs.rs/utoipa/latest/utoipa/derive.ToSchema.html) for more details.
84+
//! * **`jiff_0_2`** Add support for [jiff 0.2](https://crates.io/crates/jiff) `Zoned`, and `civil::Date` types.
85+
//! By default these types are parsed as `string`. `Zoned` will use `date-time` format. `civil::Date` will use
86+
//! `date` format. To override default `string` representation users have to use `value_type` attribute
87+
//! to override the type. See [docs](https://docs.rs/utoipa/latest/utoipa/derive.ToSchema.html) for more details.
8488
//! * **`decimal`** Add support for [rust_decimal](https://crates.io/crates/rust_decimal) `Decimal` type. **By default**
8589
//! it is interpreted as `String`. If you wish to change the format you need to override the type.
8690
//! See the `value_type` in [`ToSchema` derive docs][to_schema_derive].

0 commit comments

Comments
 (0)