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

Generic type is lost when using inline #729

Closed
laggron42 opened this issue Aug 10, 2023 · 2 comments · Fixed by #1034
Closed

Generic type is lost when using inline #729

laggron42 opened this issue Aug 10, 2023 · 2 comments · Fixed by #1034
Labels
bug Something isn't working Generics - Hard Stuff concerning generics implementation

Comments

@laggron42
Copy link

laggron42 commented Aug 10, 2023

I have a paginator defined like this

#[derive(Debug, Serialize, ToSchema)]
#[aliases(PaginatedCat = PaginatedResponse<Cat>)]
pub struct PaginatedResponse<T> {
    pub count: i64,
    pub previous: Option<i64>,
    pub next: Option<i64>,
    pub results: Vec<T>,
}

Which is then used in the following scenario

#[utoipa::path(
    context_path = "/cats/",
    params(PaginationQueryParam),
    responses(
        (status = 200, description = "Return a list of cats", body = PaginatedCat),
    ),
)]

This works as expected, the references chain themselves like so

components:
  schemas:
    PaginatedCat:
      description: A paginated response
      properties:
        [...]
        results:
          items:
            $ref: '#/components/schemas/Cat'
          type: array
      type: object
paths:
  /cats/:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PaginatedCat'

However, if I try to turn this into an inline response, it is trying to refer T instead of Cat

paths:
  /cats/:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                description: A paginated response
                properties:
                  [...]
                  results:
                    items:
                      $ref: '#/components/schemas/T'
                    type: array
                type: object
-         (status = 200, description = "Return a list of cats", body = PaginatedCat),
+         (status = 200, description = "Return a list of cats", body = inline(PaginatedCat)),
@juhaku
Copy link
Owner

juhaku commented Nov 12, 2023

Oh... This is kind of unfortunate. As when inline is being used it actually just calls ToSchema::schema() behind the scenes. That is, it does not know about whether the type itself has generics or aliases defined.

This is bit hard to solve at first thought. There is most likely a need for some significant effort in order to actually fix this issue as it is actually spread allover where the inline is being allowed to use.

@juhaku juhaku added the bug Something isn't working label Nov 12, 2023
@juhaku juhaku added the Generics - Hard Stuff concerning generics implementation label Sep 4, 2024
@juhaku
Copy link
Owner

juhaku commented Sep 11, 2024

This suppose to be fixed in PR #1034.

@juhaku juhaku closed this as completed Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Generics - Hard Stuff concerning generics implementation
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants