Skip to content
Open
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
54 changes: 44 additions & 10 deletions utoipa-gen/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,14 +1263,30 @@ impl ComponentSchema {
.collect::<Array<_>>();

if index.is_some() {
quote_spanned! {type_path.span()=>
let _ = <#rewritten_path as utoipa::PartialSchema>::schema;

let composed_tokens = quote! {
if let Some(composed) = generics.get_mut(#index) {
composed.clone()
} else {
<#rewritten_path as utoipa::PartialSchema>::schema()
}
};

if default.is_some() || nullable {
quote_spanned! {type_path.span()=>
let _ = <#rewritten_path as utoipa::PartialSchema>::schema;

utoipa::openapi::schema::OneOfBuilder::new()
#nullable_item
.item(
#composed_tokens
)
}
} else {
quote_spanned! {type_path.span()=>
let _ = <#rewritten_path as utoipa::PartialSchema>::schema;

#composed_tokens
}
}
} else {
quote_spanned! {type_path.span()=>
Expand Down Expand Up @@ -1337,14 +1353,32 @@ impl ComponentSchema {
quote! { <#rewritten_path as utoipa::ToSchema>::schemas(schemas) };
let composed_or_ref = |item_tokens: TokenStream| -> TokenStream {
if let Some(index) = &index {
quote_spanned! {type_path.span()=>
{
let _ = <#rewritten_path as utoipa::PartialSchema>::schema;
let composed_tokens = quote! {
if let Some(composed) = generics.get_mut(#index) {
composed.clone()
} else {
#item_tokens.into()
}
};

if default.is_some() || nullable {
quote_spanned! {type_path.span()=>
{
let _ = <#rewritten_path as utoipa::PartialSchema>::schema;

utoipa::openapi::schema::OneOfBuilder::new()
#nullable_item
.item(
#composed_tokens
)
}
}
} else {
quote_spanned! {type_path.span()=>
{
let _ = <#rewritten_path as utoipa::PartialSchema>::schema;

if let Some(composed) = generics.get_mut(#index) {
composed.clone()
} else {
#item_tokens.into()
#composed_tokens
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions utoipa-gen/tests/schema_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,29 @@ fn schema_with_non_generic_root() {
assert_json_snapshot!(api);
}

#[test]
fn schema_option_generic() {
#![allow(unused)]

#[derive(ToSchema)]
struct Top {
bounds: Bounds<i32>,
}

#[derive(ToSchema)]
struct Bounds<T> {
min: Option<T>,
}

#[derive(OpenApi)]
#[openapi(components(schemas(Top)))]
struct ApiDoc;
let mut api = ApiDoc::openapi();
api.info = Info::new("title", "version");

assert_json_snapshot!(api);
}

#[test]
fn derive_generic_schema_enum_variants() {
#![allow(unused)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
source: utoipa-gen/tests/schema_generics.rs
expression: api
---
{
"openapi": "3.1.0",
"info": {
"title": "title",
"version": "version"
},
"paths": {},
"components": {
"schemas": {
"Bounds_i32": {
"type": "object",
"properties": {
"min": {
"oneOf": [
{
"type": "null"
},
{
"type": "integer",
"format": "int32"
}
]
}
}
},
"Top": {
"type": "object",
"required": [
"bounds"
],
"properties": {
"bounds": {
"$ref": "#/components/schemas/Bounds_i32"
}
}
}
}
}
}