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 UUID #109

Merged
merged 1 commit into from
Apr 26, 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 @@ -29,6 +29,7 @@ chrono_types = ["utoipa-gen/chrono_types"]
chrono_types_with_format = ["utoipa-gen/chrono_types_with_format"]
decimal = ["utoipa-gen/decimal"]
yaml = ["serde_yaml"]
uuid = ["utoipa-gen/uuid"]

[dependencies]
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -42,6 +43,7 @@ paste = "1"
chrono = { version = "0.4", features = ["serde"] }
rust_decimal = "1"
rocket = "0.5.0-rc.1"
uuid = "1"

[workspace]
members = [
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Utoipa build](https://github.com/juhaku/utoipa/actions/workflows/build.yaml/badge.svg)](https://github.com/juhaku/utoipa/actions/workflows/build.yaml)
[![crates.io](https://img.shields.io/static/v1?label=crates.io&message=0.2.0&color=orange&logo=rust)](https://crates.io/crates/utoipa/0.2.0)
[![docs.rs](https://img.shields.io/static/v1?label=docs.rs&message=utoipa&color=blue&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K)](https://docs.rs/utoipa/0.2.0/utoipa/)
![rustc](https://img.shields.io/static/v1?label=rustc&message=1.60%2B&color=orange&logo=rust)

Want to have your API documented with OpenAPI? But you dont want to see the
trouble with manual yaml or json tweaking? Would like it to be so easy that it would almost
Expand Down Expand Up @@ -64,6 +65,8 @@ and the `ipa` is _api_ reversed. Aaand... `ipa` is also awesome type of beer :be
* **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.2.0/utoipa/derive.Component.html).
* **uuid** Add support for [UUID](https://github.com/uuid-rs/uuid). `Uuid` type will be presented as `String` with
format `uuid` in OpenAPI spec.

## Install

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
//! * **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].
//! * **uuid** Add support for [UUID](https://github.com/uuid-rs/uuid). `Uuid` type will be presented as `String` with
//! format `uuid` in OpenAPI spec.
//!
//! # Install
//!
Expand Down
5 changes: 5 additions & 0 deletions src/openapi/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,11 @@ pub enum ComponentFormat {
DateTime,
/// Hint to UI to obsucre input.
Password,
/// Used with [`String`] values to indicate value is in UUID format.
///
/// **uuid** feature need to be enabled.
#[cfg(feature = "uuid")]
Uuid,
}

#[cfg(test)]
Expand Down
17 changes: 17 additions & 0 deletions tests/component_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,20 @@ fn derive_struct_with_rust_decimal_with_type_override() {
"properties.rating.format" = r#""float""#, "Post rating format"
}
}

#[cfg(feature = "uuid")]
#[test]
fn derive_struct_with_uuid_type() {
use uuid::Uuid;

let post = api_doc! {
struct Post {
id: Uuid,
}
};

assert_value! {post=>
"properties.id.type" = r#""string""#, "Post id type"
"properties.id.format" = r#""uuid""#, "Post id format"
}
}
34 changes: 34 additions & 0 deletions tests/path_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,37 @@ fn derive_path_with_security_requirements() {
"security.[2].jwt_token" = "[]", "jwt_token auth scopes"
}
}

#[cfg(feature = "uuid")]
#[test]
fn derive_path_with_uuid() {
use uuid::Uuid;

#[utoipa::path(
get,
path = "/items/{id}",
responses(
(status = 200, description = "success response")
),
params(
("id" = Uuid, description = "Foo uuid"),
)
)]
#[allow(unused)]
fn get_items(id: Uuid) -> String {
"".to_string()
}
let operation = test_api_fn_doc! {
get_items,
operation: get,
path: "/items/{id}"
};

assert_value! {operation=>
"parameters.[0].schema.type" = r#""string""#, "Parameter id type"
"parameters.[0].schema.format" = r#""uuid""#, "Parameter id format"
"parameters.[0].description" = r#""Foo uuid""#, "Parameter id description"
"parameters.[0].name" = r#""id""#, "Parameter id id"
"parameters.[0].in" = r#""path""#, "Parameter in"
}
}
2 changes: 2 additions & 0 deletions utoipa-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ quote = "1.0"
proc-macro-error = "1.0"
regex = { version = "1.5", optional = true }
lazy_static = { version = "1.4", optional = true }
uuid = { version = "1", optional = true }

[dev-dependencies]
utoipa = { path = ".." }
Expand All @@ -36,3 +37,4 @@ chrono_types_with_format = []
json = []
decimal = []
rocket_extras = ["regex", "lazy_static"]
uuid = ["dep:uuid"]
25 changes: 21 additions & 4 deletions utoipa-gen/src/component_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ where
feature = "chrono_types",
feature = "chrono_types_with_format",
feature = "decimal",
feature = "rocket_extras"
feature = "rocket_extras",
feature = "uuid"
)))]
{
is_primitive(name)
Expand All @@ -27,7 +28,8 @@ where
feature = "chrono_types",
feature = "chrono_types_with_format",
feature = "decimal",
feature = "rocket_extras"
feature = "rocket_extras",
feature = "uuid",
))]
{
let mut primitive = is_primitive(name);
Expand All @@ -47,6 +49,11 @@ where
primitive = matches!(name, "PathBuf");
}

#[cfg(feature = "uuid")]
if !primitive {
primitive = matches!(name, "Uuid");
}

primitive
}
}
Expand Down Expand Up @@ -112,6 +119,8 @@ where
"Decimal" => tokens.extend(quote! { utoipa::openapi::ComponentType::String }),
#[cfg(feature = "rocket_extras")]
"PathBuf" => tokens.extend(quote! { utoipa::openapi::ComponentType::String }),
#[cfg(feature = "uuid")]
"Uuid" => tokens.extend(quote! { utoipa::openapi::ComponentType::String }),
_ => tokens.extend(quote! { utoipa::openapi::ComponentType::Object }),
}
}
Expand All @@ -125,19 +134,25 @@ impl<T: Display> ComponentFormat<T> {
pub fn is_known_format(&self) -> bool {
let name = &*self.0.to_string();

#[cfg(not(feature = "chrono_types_with_format"))]
#[cfg(not(any(feature = "chrono_types_with_format", feature = "uuid")))]
{
is_known_format(name)
}

#[cfg(feature = "chrono_types_with_format")]
#[cfg(any(feature = "chrono_types_with_format", feature = "uuid"))]
{
let mut known_format = is_known_format(name);

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

#[cfg(feature = "uuid")]
if !known_format {
known_format = matches!(name, "Uuid");
}

known_format
}
}
Expand Down Expand Up @@ -165,6 +180,8 @@ impl<T: Display> ToTokens for ComponentFormat<T> {
"DateTime" => tokens.extend(quote! { utoipa::openapi::ComponentFormat::DateTime }),
#[cfg(feature = "chrono_types_with_format")]
"Date" => tokens.extend(quote! { utoipa::openapi::ComponentFormat::Date }),
#[cfg(feature = "uuid")]
"Uuid" => tokens.extend(quote! { utoipa::openapi::ComponentFormat::Uuid }),
_ => (),
}
}
Expand Down