Skip to content

Commit

Permalink
feat: parse schema with lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
Calum4 committed Sep 8, 2024
1 parent eb04cba commit 898dacd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions utoipauto-core/src/discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ fn parse_module_items(
.map(|item| DiscoverType::Fn(build_path(module_path, &item)))
.collect(),
syn::Item::Struct(s) => {
let is_generic = !s.generics.params.is_empty();
let is_generic = !s.generics.params.iter().all(|p| matches!(p, syn::GenericParam::Lifetime(_)));

parse_from_attr(
&s.attrs,
&build_path(module_path, &s.ident.to_string()),
Expand All @@ -96,7 +97,8 @@ fn parse_module_items(
)
}
syn::Item::Enum(e) => {
let is_generic = !e.generics.params.is_empty();
let is_generic = !e.generics.params.iter().all(|p| matches!(p, syn::GenericParam::Lifetime(_)));

parse_from_attr(
&e.attrs,
&build_path(module_path, &e.ident.to_string()),
Expand Down
11 changes: 11 additions & 0 deletions utoipauto/tests/default_features/lifetimes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use utoipa::ToSchema;

#[derive(ToSchema)]
pub struct LifetimeStructSchema<'a> {
foo: &'a str,
}

#[derive(ToSchema)]
pub enum LifetimeEnumSchema<'a> {
Foo(&'a str),
}
1 change: 1 addition & 0 deletions utoipauto/tests/default_features/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod controllers;
pub mod generics;
pub mod lifetimes;
pub mod models;
pub mod test;
13 changes: 13 additions & 0 deletions utoipauto/tests/default_features/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,16 @@ pub struct CustomHandlerApiDocs {}
fn test_custom_handler() {
assert_eq!(CustomHandlerApiDocs::openapi().paths.paths.len(), 1)
}

/// Discover schema with lifetimes
#[utoipauto(
paths = "./utoipauto/tests/default_features/lifetimes.rs",
)]
#[derive(OpenApi)]
#[openapi(info(title = "Lifetimes API", version = "1.0.0"))]
pub struct LifetimesApiDocs {}

#[test]
fn test_lifetimes() {
assert_eq!(LifetimesApiDocs::openapi().components.expect("no components").schemas.len(), 2)
}

0 comments on commit 898dacd

Please sign in to comment.