Skip to content
Open
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
3 changes: 3 additions & 0 deletions utoipa-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ paste = "1"
rocket = { version = "0.5", features = ["json"] }
smallvec = { version = "1.10", features = ["serde"] }
rust_decimal = { version = "1", default-features = false }
bigdecimal = { version = "0.4", default-features = false }
chrono = { version = "0.4", features = ["serde"] }
time = { version = "0.3", features = ["serde-human-readable"] }
jiff = { version = "0.2", features = ["serde"] }
Expand All @@ -63,6 +64,8 @@ chrono = []
yaml = []
decimal = []
decimal_float = []
bigdecimal = []
bigdecimal_float = []
rocket_extras = ["regex", "syn/extra-traits"]
non_strict_integers = []
uuid = ["dep:uuid"]
Expand Down
3 changes: 3 additions & 0 deletions utoipa-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#[cfg(all(feature = "decimal", feature = "decimal_float"))]
compile_error!("`decimal` and `decimal_float` are mutually exclusive feature flags");

#[cfg(all(feature = "bigdecimal", feature = "bigdecimal_float"))]
compile_error!("`bigdecimal` and `bigdecimal_float` are mutually exclusive feature flags");

#[cfg(all(
feature = "actix_extras",
feature = "axum_extras",
Expand Down
28 changes: 28 additions & 0 deletions utoipa-gen/src/schema_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ impl SchemaType<'_> {
feature = "chrono",
feature = "decimal",
feature = "decimal_float",
feature = "bigdecimal",
feature = "bigdecimal_float",
feature = "rocket_extras",
feature = "uuid",
feature = "ulid",
Expand All @@ -88,6 +90,8 @@ impl SchemaType<'_> {
feature = "chrono",
feature = "decimal",
feature = "decimal_float",
feature = "bigdecimal",
feature = "bigdecimal_float",
feature = "rocket_extras",
feature = "uuid",
feature = "ulid",
Expand Down Expand Up @@ -117,6 +121,11 @@ impl SchemaType<'_> {
primitive = matches!(name, "Uuid");
}

#[cfg(any(feature = "bigdecimal", feature = "bigdecimal_float"))]
if !primitive {
primitive = matches!(name, "BigDecimal");
}

#[cfg(feature = "ulid")]
if !primitive {
primitive = matches!(name, "Ulid");
Expand Down Expand Up @@ -284,6 +293,12 @@ impl ToTokensDiagnostics for SchemaType<'_> {
#[cfg(feature = "decimal_float")]
"Decimal" => schema_type_tokens(tokens, SchemaTypeInner::Number, self.nullable),

#[cfg(feature = "bigdecimal")]
"BigDecimal" => schema_type_tokens(tokens, SchemaTypeInner::String, self.nullable),

#[cfg(feature = "bigdecimal_float")]
"BigDecimal" => schema_type_tokens(tokens, SchemaTypeInner::Number, self.nullable),

#[cfg(feature = "rocket_extras")]
"PathBuf" => schema_type_tokens(tokens, SchemaTypeInner::String, self.nullable),

Expand Down Expand Up @@ -412,6 +427,9 @@ impl KnownFormat {
#[cfg(feature = "decimal_float")]
"Decimal" => Self::Double,

#[cfg(feature = "bigdecimal_float")]
"BigDecimal" => Self::Double,

#[cfg(feature = "uuid")]
"Uuid" => Self::Uuid,

Expand Down Expand Up @@ -724,6 +742,16 @@ impl PrimitiveType {
syn::parse_quote!(f64)
}

#[cfg(feature = "bigdecimal")]
"BigDecimal" => {
syn::parse_quote!(String)
}

#[cfg(feature = "bigdecimal_float")]
"BigDecimal" => {
syn::parse_quote!(f64)
}

#[cfg(feature = "rocket_extras")]
"PathBuf" => {
syn::parse_quote!(String)
Expand Down
82 changes: 82 additions & 0 deletions utoipa-gen/tests/schema_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,88 @@ fn derive_struct_override_type_with_a_reference() {
assert_json_snapshot!(value);
}

#[cfg(feature = "bigdecimal")]
#[test]
fn derive_struct_with_bigdecimal() {
use bigdecimal::BigDecimal;

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

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 = "bigdecimal")]
#[test]
fn derive_struct_with_bigdecimal_with_type_override() {
use bigdecimal::BigDecimal;

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

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#""double""#, "Post rating format"
}
}

#[cfg(feature = "bigdecimal_float")]
#[test]
fn derive_struct_with_rust_decimal_float() {
use bigdecimal::BigDecimal;

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

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#""double""#, "Post rating format"
}
}

#[cfg(feature = "bigdecimal_float")]
#[test]
fn derive_struct_with_bigdecimal_float_with_type_override() {
use bigdecimal::BigDecimal;

let post = api_doc! {
struct Post {
id: i32,
#[schema(value_type = String)]
rating: BigDecimal,
}
};

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() {
Expand Down
2 changes: 2 additions & 0 deletions utoipa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ axum_extras = ["utoipa-gen?/axum_extras"]
chrono = ["utoipa-gen?/chrono"]
decimal = ["utoipa-gen?/decimal"]
decimal_float = ["utoipa-gen?/decimal_float"]
bigdecimal = ["utoipa-gen?/bigdecimal"]
bigdecimal_float = ["utoipa-gen?/bigdecimal_float"]
non_strict_integers = ["utoipa-gen?/non_strict_integers"]
yaml = ["serde_norway", "utoipa-gen?/yaml"]
uuid = ["utoipa-gen?/uuid"]
Expand Down