utoipa icon indicating copy to clipboard operation
utoipa copied to clipboard

Generic type is lost when using inline

Open laggron42 opened this issue 1 year ago • 1 comments

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)),

laggron42 avatar Aug 10 '23 15:08 laggron42

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 avatar Nov 12 '23 10:11 juhaku