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

oapi: Add HashSet and BTreeSet #515

Merged
merged 1 commit into from
Nov 27, 2023
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
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
Loading