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 smallvec crate #229

Merged
merged 1 commit into from
Jul 24, 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 @@ -32,6 +32,7 @@ decimal = ["utoipa-gen/decimal"]
yaml = ["serde_yaml"]
uuid = ["utoipa-gen/uuid"]
time = ["utoipa-gen/time"]
smallvec = ["utoipa-gen/smallvec"]

[dependencies]
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -49,6 +50,7 @@ rust_decimal = "1"
rocket = "0.5.0-rc.1"
uuid = "1"
time = { version = "0.3.11", features = [ "serde-human-readable" ] }
smallvec = { version = "1.9.0", features = [ "serde" ] }

[workspace]
members = [
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ and the `ipa` is _api_ reversed. Aaand... `ipa` is also awesome type of beer :be
See the `value_type` in [component derive docs](https://docs.rs/utoipa/1.1.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.
* **smallvec** Add support for [smallvec](https://crates.io/crates/smallvec). `SmallVec` will be treated as `Vec`.

Utoipa implicitly has partial support for `serde` attributes. See [docs](https://docs.rs/utoipa/1.1.0/utoipa/derive.Component.html#partial-serde-attributes-support) for more details.

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
//! 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.
//! * **smallvec** Add support for [smallvec](https://crates.io/crates/smallvec). `SmallVec` will be treated as `Vec`.
//!
//! Utoipa implicitly has partial support for `serde` attributes. See [component derive][serde] for more details.
//!
Expand Down
30 changes: 29 additions & 1 deletion tests/component_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ fn derive_component_with_chrono_with_chrono_feature() {

#[cfg(feature = "time")]
#[test]
fn derive_component_with_chrono_with_chrono_feature() {
fn derive_component_with_time_feature() {
use time::{Date, Duration, OffsetDateTime, PrimitiveDateTime};

let times = api_doc! {
Expand Down Expand Up @@ -1774,3 +1774,31 @@ fn derive_component_with_raw_identifier() {
})
)
}

#[cfg(feature = "smallvec")]
#[test]
fn derive_component_with_smallvec_feature() {
use smallvec::SmallVec;

let bar = api_doc! {
struct Bar<'b> {
links: SmallVec<[&'b str; 2]>
}
};

assert_json_eq!(
bar,
json!({
"properties": {
"links": {
"items": {
"type": "string"
},
"type": "array",
}
},
"required": ["links"],
"type": "object"
})
)
}
1 change: 1 addition & 0 deletions utoipa-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ rocket_extras = ["regex", "lazy_static"]
uuid = ["dep:uuid"]
axum_extras = []
time = []
smallvec = []
3 changes: 2 additions & 1 deletion utoipa-gen/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl<'t> TypeTree<'t> {
Type::Reference(reference) => Self::get_type_paths(reference.elem.as_ref()),
Type::Tuple(tuple) => tuple.elems.iter().flat_map(Self::get_type_paths).collect(),
Type::Group(group) => Self::get_type_paths(group.elem.as_ref()),
Type::Array(array) => Self::get_type_paths(&array.elem),
_ => abort_call_site!(
"unexpected type in component part get type path, expected one of: Path, Reference, Group"
),
Expand Down Expand Up @@ -155,7 +156,7 @@ impl<'t> TypeTree<'t> {
fn get_generic_type(segment: &PathSegment) -> Option<GenericType> {
match &*segment.ident.to_string() {
"HashMap" | "Map" | "BTreeMap" => Some(GenericType::Map),
"Vec" => Some(GenericType::Vec),
"Vec" | "SmallVec" => Some(GenericType::Vec),
"Option" => Some(GenericType::Option),
"Cow" => Some(GenericType::Cow),
"Box" => Some(GenericType::Box),
Expand Down