Skip to content

Commit

Permalink
feat: add HashSet and BTreeSet (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron authored Oct 31, 2023
1 parent f43efc7 commit 2f89c69
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
20 changes: 20 additions & 0 deletions utoipa-gen/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ impl<'t> TypeTree<'t> {
#[cfg(feature = "indexmap")]
"IndexMap" => Some(GenericType::Map),
"Vec" => Some(GenericType::Vec),
"BTreeSet" | "HashSet" => Some(GenericType::Set),
"LinkedList" => Some(GenericType::LinkedList),
#[cfg(feature = "smallvec")]
"SmallVec" => Some(GenericType::SmallVec),
Expand Down Expand Up @@ -393,6 +394,7 @@ pub enum ValueType {
pub enum GenericType {
Vec,
LinkedList,
Set,
#[cfg(feature = "smallvec")]
SmallVec,
Map,
Expand Down Expand Up @@ -503,6 +505,14 @@ impl<'c> ComponentSchema {
description_stream,
deprecated_stream,
),
Some(GenericType::Set) => ComponentSchema::vec_to_tokens(
&mut tokens,
features,
type_tree,
object_name,
description_stream,
deprecated_stream,
),
#[cfg(feature = "smallvec")]
Some(GenericType::SmallVec) => ComponentSchema::vec_to_tokens(
&mut tokens,
Expand Down Expand Up @@ -668,6 +678,8 @@ impl<'c> ComponentSchema {
child
};

let unique = matches!(type_tree.generic_type, Some(GenericType::Set));

// is octet-stream
let schema = if child
.path
Expand All @@ -689,9 +701,17 @@ impl<'c> ComponentSchema {
object_name,
});

let unique = match unique {
true => quote! {
.unique_items(true)
},
false => quote! {},
};

quote! {
utoipa::openapi::schema::ArrayBuilder::new()
.items(#component_schema)
#unique
}
};

Expand Down
2 changes: 1 addition & 1 deletion utoipa-gen/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ impl PathTypeTree for TypeTree<'_> {
/// Check whether [`TypeTree`] is a Vec, slice, array or other supported array type
fn is_array(&self) -> bool {
match self.generic_type {
Some(GenericType::Vec) => true,
Some(GenericType::Vec | GenericType::Set) => true,
Some(_) => self
.children
.as_ref()
Expand Down
60 changes: 60 additions & 0 deletions utoipa-gen/tests/schema_derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4918,6 +4918,66 @@ fn derive_struct_with_rc() {
)
}

#[test]
fn derive_btreeset() {
use std::collections::BTreeSet;

let greeting = api_doc! {
struct Greeting {
values: BTreeSet<String>,
}
};

assert_json_eq!(
greeting,
json!({
"properties": {
"values": {
"type": "array",
"uniqueItems": true,
"items": {
"type": "string"
}
},
},
"required": [
"values"
],
"type": "object"
})
)
}

#[test]
fn derive_hashset() {
use std::collections::HashSet;

let greeting = api_doc! {
struct Greeting {
values: HashSet<String>,
}
};

assert_json_eq!(
greeting,
json!({
"properties": {
"values": {
"type": "array",
"uniqueItems": true,
"items": {
"type": "string"
}
},
},
"required": [
"values"
],
"type": "object"
})
)
}

#[test]
fn derive_doc_hidden() {
let map = api_doc! {
Expand Down

0 comments on commit 2f89c69

Please sign in to comment.