Skip to content

Commit

Permalink
Fix generic aliases (#1083)
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 authored Oct 3, 2024
1 parent 64caf06 commit d5e722a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
4 changes: 4 additions & 0 deletions utoipa-config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* Add global config for `utiopa` (https://github.com/juhaku/utoipa/pull/1048)

### Fixed

* Fix generic aliases (https://github.com/juhaku/utoipa/pull/1083)

### Changed

* Fixed broken tests at `utoipa-config`
Expand Down
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
1 change: 1 addition & 0 deletions utoipa-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

### Fixed

* Fix generic aliases (https://github.com/juhaku/utoipa/pull/1083)
* Fix nest path config struct name (https://github.com/juhaku/utoipa/pull/1081)
* Fix `as` attribute path format (https://github.com/juhaku/utoipa/pull/1080)
* Fix allow response `content_type` without schema (https://github.com/juhaku/utoipa/pull/1073)
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 d5e722a

Please sign in to comment.