Skip to content

Commit

Permalink
Fix generic references
Browse files Browse the repository at this point in the history
This PR is follow up for #1095 which fixed the generic references name
resolving. This commit will fix the generic references inner type
resolving. Prior this commit the generic reference always set everything
as `$ref` even in case of primitive type. Nonetheless now the correct
type will be used instead.
  • Loading branch information
juhaku committed Oct 6, 2024
1 parent 7034022 commit 880d84f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions utoipa-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

### Fixed

* Fix generic references (https://github.com/juhaku/utoipa/pull/1097)
* Fix non generic root generic references (https://github.com/juhaku/utoipa/pull/1095)
* Fix option wrapped tailing query parameters (https://github.com/juhaku/utoipa/pull/1089)
* Fix doc comment trimming to keep relative indentation. (https://github.com/juhaku/utoipa/pull/1082)
Expand Down
10 changes: 8 additions & 2 deletions utoipa-gen/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,8 +1131,14 @@ impl ComponentSchema {
let index = container.generics.get_generic_type_param_index(type_tree);
// only set schema references for concrete non generic types
if index.is_none() {
object_schema_reference.tokens =
quote! {<#type_path as utoipa::PartialSchema>::schema() };
let reference_tokens = if let Some(children) = &type_tree.children {
let composed_generics =
Self::compose_generics(children).collect::<Array<_>>();
quote! { <#type_path as utoipa::__dev::ComposeSchema>::compose(#composed_generics.to_vec()) }
} else {
quote! { <#type_path as utoipa::PartialSchema>::schema() }
};
object_schema_reference.tokens = reference_tokens;
object_schema_reference.references = quote! { <#type_path as utoipa::__dev::SchemaReferences>::schemas(schemas) };
}
let composed_or_ref = |item_tokens: TokenStream| -> TokenStream {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,33 @@
{
"properties": {
"One": {
"$ref": "#/components/schemas/Yeah"
"properties": {
"accounts": {
"items": {
"allOf": [
{
"type": "null"
},
{
"$ref": "#/components/schemas/Account"
}
]
},
"type": "array"
},
"foo_bar": {
"$ref": "#/components/schemas/Foobar"
},
"name": {
"type": "string"
}
},
"required": [
"name",
"foo_bar",
"accounts"
],
"type": "object"
}
},
"required": [
Expand All @@ -69,7 +95,7 @@
"properties": {
"Many": {
"items": {
"$ref": "#/components/schemas/Yeah"
"type": "object"
},
"type": "array"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"properties": {
"value": {
"$ref": "#/components/schemas/String"
"type": "string"
}
}
},
Expand All @@ -25,7 +25,8 @@
],
"properties": {
"value": {
"$ref": "#/components/schemas/i32"
"type": "integer",
"format": "int32"
}
}
},
Expand Down

0 comments on commit 880d84f

Please sign in to comment.