Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non generic root generic references #1095

Merged
merged 1 commit into from
Oct 5, 2024

Conversation

juhaku
Copy link
Owner

@juhaku juhaku commented Oct 5, 2024

Prior to this commit the type having generic reference resulted incorrect schema due to wrong reference names of generic references. This commit will fix this by correctly resolving names for generic schema references. This is done by relying on ToSchema::name implementation instead of directly Ident of the type when composing children name.

Following example now works where the deeply nested generic references will all get correct name resulting correct OpenAPI schema.

 #[derive(ToSchema)]
 struct Foo<T> {
     bar: Bar<T>,
 }

 #[derive(ToSchema)]
 struct Bar<T> {
     value: T,
 }

 #[derive(ToSchema)]
 struct Top {
     foo1: Foo<String>,
     foo2: Foo<i32>,
 }

 #[derive(OpenApi)]
 #[openapi(components(schemas(Top)))]
 struct ApiDoc;

Fixes #1092

Prior to this commit the type having generic reference resulted
incorrect schema due to wrong reference names of generic references.
This commit will fix this by correctly resolving names for generic
schema references. This is done by relying on `ToSchema::name`
implementation instead of directly Ident of the type when composing
children name.

Following example now works where the deeply nested generic references
will all get correct name resulting correct OpenAPI schema.
```rust
 #[derive(ToSchema)]
 struct Foo<T> {
     bar: Bar<T>,
 }

 #[derive(ToSchema)]
 struct Bar<T> {
     value: T,
 }

 #[derive(ToSchema)]
 struct Top {
     foo1: Foo<String>,
     foo2: Foo<i32>,
 }

 #[derive(OpenApi)]
 #[openapi(components(schemas(Top)))]
 struct ApiDoc;
```

Fixes #1092
@juhaku juhaku force-pushed the fix-non-generic-root-generic-references branch from 9d402bf to b42d283 Compare October 5, 2024 14:24
@juhaku juhaku merged commit 7034022 into master Oct 5, 2024
22 checks passed
@juhaku juhaku deleted the fix-non-generic-root-generic-references branch October 5, 2024 14:31
juhaku added a commit that referenced this pull request Oct 6, 2024
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.
@juhaku juhaku mentioned this pull request Oct 6, 2024
juhaku added a commit that referenced this pull request Oct 6, 2024
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.
juhaku added a commit that referenced this pull request Oct 6, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Released
Development

Successfully merging this pull request may close these issues.

[5.0.0-rc.0] Deeply nested generics (>=2 levels) generates incorrect schemas
1 participant