Skip to content

Commit

Permalink
Fix generic aliases
Browse files Browse the repository at this point in the history
Fix aliases support for generic container types like `Option<T>` and
`Vec<T>`. Previously if alias was used within container type it was not
recognized and the `TypeTree` was resolved incorrectly.

Fixes #1057
  • Loading branch information
juhaku committed Oct 3, 2024
1 parent 64caf06 commit af56f45
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
2 changes: 2 additions & 0 deletions utoipa-config/config-test-crate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct AliasValues {
my_value: bool,

date: MyDateTime,

optional_date: Option<MyDateTime>,
}

#[allow(unused)]
Expand Down
77 changes: 50 additions & 27 deletions utoipa-gen/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,15 +696,20 @@ impl ComponentSchema {
{
features.push(Nullable::new().into());
}
let child = type_tree
.children
.as_ref()
.expect("ComponentSchema generic container type should have children")
.iter()
.next()
.expect("ComponentSchema generic container type should have 1 child");
let alias = child.get_alias_type()?;
let alias = alias.as_ref().map_try(TypeTree::from_type)?;
let child = alias.as_ref().unwrap_or(child);

let schema = ComponentSchema::new(ComponentSchemaProps {
container,
type_tree: type_tree
.children
.as_ref()
.expect("ComponentSchema generic container type should have children")
.iter()
.next()
.expect("ComponentSchema generic container type should have 1 child"),
type_tree: child,
features,
description,
})?;
Expand All @@ -713,15 +718,20 @@ impl ComponentSchema {
schema_references.extend(schema.schema_references);
}
Some(GenericType::Cow | GenericType::Box | GenericType::RefCell) => {
let child = type_tree
.children
.as_ref()
.expect("ComponentSchema generic container type should have children")
.iter()
.next()
.expect("ComponentSchema generic container type should have 1 child");
let alias = child.get_alias_type()?;
let alias = alias.as_ref().map_try(TypeTree::from_type)?;
let child = alias.as_ref().unwrap_or(child);

let schema = ComponentSchema::new(ComponentSchemaProps {
container,
type_tree: type_tree
.children
.as_ref()
.expect("ComponentSchema generic container type should have children")
.iter()
.next()
.expect("ComponentSchema generic container type should have 1 child"),
type_tree: child,
features,
description,
})?;
Expand All @@ -731,15 +741,20 @@ impl ComponentSchema {
}
#[cfg(feature = "rc_schema")]
Some(GenericType::Arc) | Some(GenericType::Rc) => {
let child = type_tree
.children
.as_ref()
.expect("ComponentSchema rc generic container type should have children")
.iter()
.next()
.expect("ComponentSchema rc generic container type should have 1 child");
let alias = child.get_alias_type()?;
let alias = alias.as_ref().map_try(TypeTree::from_type)?;
let child = alias.as_ref().unwrap_or(child);

let schema = ComponentSchema::new(ComponentSchemaProps {
container,
type_tree: type_tree
.children
.as_ref()
.expect("ComponentSchema rc generic container type should have children")
.iter()
.next()
.expect("ComponentSchema rc generic container type should have 1 child"),
type_tree: child,
features,
description,
})?;
Expand Down Expand Up @@ -810,14 +825,19 @@ impl ComponentSchema {
// additionalProperties denoting the type
// maps have 2 child schemas and we are interested the second one of them
// which is used to determine the additional properties
let child = type_tree
.children
.as_ref()
.expect("ComponentSchema Map type should have children")
.get(1)
.expect("ComponentSchema Map type should have 2 child");
let alias = child.get_alias_type()?;
let alias = alias.as_ref().map_try(TypeTree::from_type)?;
let child = alias.as_ref().unwrap_or(child);

let schema_property = ComponentSchema::new(ComponentSchemaProps {
container,
type_tree: type_tree
.children
.as_ref()
.expect("ComponentSchema Map type should have children")
.get(1)
.expect("ComponentSchema Map type should have 2 child"),
type_tree: child,
features,
description: None,
})?;
Expand Down Expand Up @@ -882,6 +902,9 @@ impl ComponentSchema {
} else {
child
};
let alias = child.get_alias_type()?;
let alias = alias.as_ref().map_try(TypeTree::from_type)?;
let child = alias.as_ref().unwrap_or(child);

let component_schema = ComponentSchema::new(ComponentSchemaProps {
container,
Expand Down

0 comments on commit af56f45

Please sign in to comment.