Skip to content

Commit

Permalink
oapi: Add HashSet and BTreeSet (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn authored Nov 27, 2023
1 parent b12a72e commit 96cc7ba
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
19 changes: 19 additions & 0 deletions crates/oapi-macros/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ impl<'c> ComponentSchema {
deprecated_stream,
type_definition,
),
Some(GenericType::Set) => ComponentSchema::vec_to_tokens(
&mut tokens,
features,
type_tree,
object_name,
description_stream,
deprecated_stream,
type_definition,
),
#[cfg(feature = "smallvec")]
Some(GenericType::SmallVec) => ComponentSchema::vec_to_tokens(
&mut tokens,
Expand Down Expand Up @@ -212,6 +221,8 @@ impl<'c> ComponentSchema {
.next()
.expect("CompnentSchema Vec should have 1 child");

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

// is octet-stream
let schema = if child
.path
Expand All @@ -234,8 +245,16 @@ impl<'c> ComponentSchema {
type_definition,
});

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

quote! {
#oapi::oapi::schema::Array::new(#component_schema)
#unique
}
};

Expand Down
2 changes: 1 addition & 1 deletion crates/oapi-macros/src/operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,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().unwrap().iter().any(|child| child.is_array()),
None => false,
}
Expand Down
2 changes: 2 additions & 0 deletions crates/oapi-macros/src/type_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,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 @@ -273,6 +274,7 @@ pub(crate) enum ValueType {
pub(crate) enum GenericType {
Vec,
LinkedList,
Set,
#[cfg(feature = "smallvec")]
SmallVec,
Map,
Expand Down
5 changes: 2 additions & 3 deletions crates/oapi/src/openapi/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,9 @@ mod tests {
discriminator.mapping = [("int".to_string(), "#/components/schemas/MyInt".to_string())]
.into_iter()
.collect::<BTreeMap<_, _>>();
let one_of = OneOfBuilder::new()
let one_of = OneOf::new()
.item(Ref::from_schema_name("MyInt"))
.discriminator(Some(discriminator))
.build();
.discriminator(discriminator);
let json_value = serde_json::to_value(one_of).unwrap();

assert_json_eq!(
Expand Down

0 comments on commit 96cc7ba

Please sign in to comment.