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

Nested generic parameters must be manually inlined #1197

Open
1 task
JMLX42 opened this issue Nov 8, 2024 · 0 comments
Open
1 task

Nested generic parameters must be manually inlined #1197

JMLX42 opened this issue Nov 8, 2024 · 0 comments

Comments

@JMLX42
Copy link
Contributor

JMLX42 commented Nov 8, 2024

Problem

Follow up on #1182 / #1184

The new behavior for generic types is as follow:

  • If it's a native type, force inline = true except if #[schema(inline = false)] is specified (because if it is specified, maybe someone does actually want to make their own u32 schema?).
  • If #[schema(inline = ...)] is specified, use it.
  • Otherwise, assume #[schema(inline = true)] (in order to keep the existing behavior).

But an edge case remains (cf this comment).

Nested generics need to be inlined manually in some cases.

Here is an example:

   #[derive(ToSchema)]
   #[schema(as = path::MyType<T>)]
   struct Type<T> {
       t: T,
   }

   #[derive(ToSchema)]
   struct Person<T: Sized, P> {
       field: T,
       t: P,
   }

In this case:

  1. Person<String, Type<MyStruct> will work as expected.
  2. Person<String, Type<i32> will not work, because it will generate an invalid reference to the the i32 schema ("$ref": "#/components/schemas/i32") instead of inlining the i32 schema.

Workaround

To workaround case 2., #[schema(inline)] must be added manually on the nested generic's field(s):

   #[derive(ToSchema)]
   #[schema(as = path::MyType<T>)]
   struct Type<T> {
       #[schema(inline)]
       t: T,
   }

   #[derive(ToSchema)]
   struct Person<T: Sized, P> {
       field: T,
       #[schema(inline)]
       t: P,
   }

To Do

  • Find an actual fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant