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 rust_decimal type #79

Merged
merged 1 commit into from
Apr 9, 2022
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ actix_extras = ["utoipa-gen/actix_extras"]
json = ["serde_json", "utoipa-gen/json"]
chrono_types = ["utoipa-gen/chrono_types"]
chrono_types_with_format = ["utoipa-gen/chrono_types_with_format"]
decimal = ["utoipa-gen/decimal"]

[dependencies]
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -36,6 +37,7 @@ utoipa-gen = { version = "0.1.2", path = "./utoipa-gen" }
actix-web = { version = "4" }
paste = "1"
chrono = { version = "0.4", features = ["serde"] }
rust_decimal = "1"

[workspace]
members = [
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ and the `ipa` is _api_ reversed. Aaand... `ipa` is also awesome type of beer :be
* **actix_extras** Enhances actix-web intgration with being able to parse some documentation
from actix web macro attributes and types. See the [path attribute macro](https://docs.rs/utoipa/0.1.1/utoipa/attr.path.html) for more details.
* **debug** Add extra traits such as debug traits to openapi definitions and elsewhere.
* **chrono_types** Add support for _**chrono**_ `DateTime`, `Date` and `Duration` types. By default these types
* **chrono_types** Add support for [chrono](https://crates.io/crates/chrono) `DateTime`, `Date` and `Duration` types. By default these types
are parsed to `string` types without additional format. If you want to have formats added to the types
use *chrono_types_with_format* feature. This is useful because OpenAPI 3.1 spec does not have date-time formats.
* **chrono_types_with_format** Add support to _**chrono**_ types described above with additional `format`
* **chrono_types_with_format** Add support to [chrono](https://crates.io/crates/chrono) types described above with additional `format`
information type. `date-time` for `DateTime` and `date` for `Date` according
[RFC3339](https://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14) as `ISO-8601`.
* **decimal** Add support for [rust_decimal](https://crates.io/crates/rust_decimal) `Decimal` type. **By default**
it is interpreted as `String`. If you wish to change the format you need to override the type.
See the `value_type` in [component derive docs](https://docs.rs/utoipa/0.1.1/utoipa/derive.Component.html).

## Install

Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@
//! * **actix_extras** Enhances actix-web intgration with being able to parse some documentation
//! from actix web macro attributes and types. See [`utoipa::path(...)`][path] for more details.
//! * **debug** Add extra traits such as debug traits to openapi definitions and elsewhere.
//! * **chrono_types** Add support for _**chrono**_ `DateTime`, `Date` and `Duration` types. By default these types
//! * **chrono_types** Add support for [chrono](https://crates.io/crates/chrono) `DateTime`, `Date` and `Duration` types. By default these types
//! are parsed to `string` types without
//! additional format. If you want to have formats added to the types use _chrono_with_format_ feature.
//! This is useful because OpenAPI 3.1 spec does not have date-time formats.
//! * **chrono_types_with_format** Add support to _**chrono**_ types described above with additional `format`
//! * **chrono_types_with_format** Add support to [chrono](https://crates.io/crates/chrono) types described above with additional `format`
//! information type. `date-time` for `DateTime` and `date` for `Date` according
//! [RFC3339](https://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14) as `ISO-8601`.
//! * **decimal** Add support for [rust_decimal](https://crates.io/crates/rust_decimal) `Decimal` type. **By default**
//! it is interpreted as `String`. If you wish to change the format you need to override the type.
//! See the `value_type` in [component derive docs][component_derive].
//!
//! # Install
//!
Expand Down Expand Up @@ -183,6 +186,7 @@
//! [path]: attr.path.html
//!
//! [security]: openapi/security/index.html
//! [component_derive]: derive.Component.html

pub mod openapi;

Expand Down
41 changes: 41 additions & 0 deletions tests/component_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,3 +736,44 @@ fn derive_unnamed_struct_component_type_override_with_format() {
"format" = r#""byte""#, "Value format"
}
}

#[cfg(feature = "decimal")]
#[test]
fn derive_struct_with_rust_decimal() {
use rust_decimal::Decimal;

let post = api_doc! {
struct Post {
id: i32,
rating: Decimal,
}
};

assert_value! {post=>
"properties.id.type" = r#""integer""#, "Post id type"
"properties.id.format" = r#""int32""#, "Post id format"
"properties.rating.type" = r#""string""#, "Post rating type"
"properties.rating.format" = r#"null"#, "Post rating format"
}
}

#[cfg(feature = "decimal")]
#[test]
fn derive_struct_with_rust_decimal_with_type_override() {
use rust_decimal::Decimal;

let post = api_doc! {
struct Post {
id: i32,
#[component(value_type = f64)]
rating: Decimal,
}
};

assert_value! {post=>
"properties.id.type" = r#""integer""#, "Post id type"
"properties.id.format" = r#""int32""#, "Post id format"
"properties.rating.type" = r#""number""#, "Post rating type"
"properties.rating.format" = r#""float""#, "Post rating format"
}
}
1 change: 1 addition & 0 deletions utoipa-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ actix_extras = ["regex", "lazy_static"]
chrono_types = []
chrono_types_with_format = []
json = []
decimal = []
31 changes: 22 additions & 9 deletions utoipa-gen/src/component_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ where
primitive = is_primitive_chrono(name);
}

#[cfg(feature = "decimal")]
if !primitive {
primitive = is_primitive_rust_decimal(name);
}

primitive
}
}
Expand Down Expand Up @@ -58,6 +63,12 @@ fn is_primitive_chrono(name: &str) -> bool {
matches!(name, "DateTime" | "Date" | "Duration")
}

#[inline]
#[cfg(feature = "chrono_types")]
fn is_primitive_rust_decimal(name: &str) -> bool {
matches!(name, "Decimal")
}

impl<'a, T> ToTokens for ComponentType<'a, T>
where
T: Display,
Expand All @@ -69,15 +80,17 @@ where
"String" | "str" | "char" => {
tokens.extend(quote! {utoipa::openapi::ComponentType::String})
}
"bool" => tokens.extend(quote! {utoipa::openapi::ComponentType::Boolean}),
"bool" => tokens.extend(quote! { utoipa::openapi::ComponentType::Boolean }),
"i8" | "i16" | "i32" | "i64" | "i128" | "isize" | "u8" | "u16" | "u32" | "u64"
| "u128" | "usize" => tokens.extend(quote! {utoipa::openapi::ComponentType::Integer}),
"f32" | "f64" => tokens.extend(quote! {utoipa::openapi::ComponentType::Number}),
| "u128" | "usize" => tokens.extend(quote! { utoipa::openapi::ComponentType::Integer }),
"f32" | "f64" => tokens.extend(quote! { utoipa::openapi::ComponentType::Number }),
#[cfg(any(feature = "chrono_types", feature = "chrono_types_with_format"))]
"DateTime" | "Date" | "Duration" => {
tokens.extend(quote! { utoipa::openapi::ComponentType::String })
}
_ => tokens.extend(quote! {utoipa::openapi::ComponentType::Object}),
#[cfg(feature = "decimal")]
"Decimal" => tokens.extend(quote! { utoipa::openapi::ComponentType::String }),
_ => tokens.extend(quote! { utoipa::openapi::ComponentType::Object }),
}
}
}
Expand Down Expand Up @@ -118,14 +131,14 @@ impl<T: Display> ToTokens for ComponentFormat<T> {

match name {
"i8" | "i16" | "i32" | "u8" | "u16" | "u32" => {
tokens.extend(quote! {utoipa::openapi::ComponentFormat::Int32})
tokens.extend(quote! { utoipa::openapi::ComponentFormat::Int32 })
}
"i64" | "u64" => tokens.extend(quote! {utoipa::openapi::ComponentFormat::Int64}),
"f32" | "f64" => tokens.extend(quote! {utoipa::openapi::ComponentFormat::Float}),
"i64" | "u64" => tokens.extend(quote! { utoipa::openapi::ComponentFormat::Int64 }),
"f32" | "f64" => tokens.extend(quote! { utoipa::openapi::ComponentFormat::Float }),
#[cfg(feature = "chrono_types_with_format")]
"DateTime" => tokens.extend(quote! { utoipa::openapi::ComponentFormat::DateTime}),
"DateTime" => tokens.extend(quote! { utoipa::openapi::ComponentFormat::DateTime }),
#[cfg(feature = "chrono_types_with_format")]
"Date" => tokens.extend(quote! { utoipa::openapi::ComponentFormat::Date}),
"Date" => tokens.extend(quote! { utoipa::openapi::ComponentFormat::Date }),
_ => (),
}
}
Expand Down
Loading