fastapi: Not rendering multi-select in API doc while using Pydantic model

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import typing
from fastapi import FastAPI, Query, Depends
from pydantic import BaseModel
from enum import Enum

app = FastAPI()


class Status(str, Enum):
    SUCCESS = "SUCCESS"
    REFUND = "REFUND"
    FAIL = "FAIL"
    CANCEL = "CANCEL"


@app.get("/working-example/")
async def root_with_normal_query_params(status_in: typing.List[Status] = Query(...)):
    return {"status_inputs": status_in}


class StatusModel(BaseModel):
    status_in: typing.List[Status]


@app.get("/not-working-example/")
async def root_with_pydantic(status_inputs: StatusModel = Depends()):
    return {"status_inputs": status_inputs}

Description

The API docs are not generating the multi-select option while using the Pydantic model for the query/request parser.

without using Pydantic model

image

with using Pydantic model

image

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.70.1

Python Version

Python 3.9.11

Additional Context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

You can use dataclass from Pydantic.

import typing
from enum import Enum

from fastapi import FastAPI, Query, Depends
from pydantic.dataclasses import dataclass

app = FastAPI()


class Status(str, Enum):
    SUCCESS = "SUCCESS"
    REFUND = "REFUND"
    FAIL = "FAIL"
    CANCEL = "CANCEL"


@app.get("/working-example/")
async def root_with_normal_query_params(status_in: typing.List[Status] = Query(...)):
    return {"status_inputs": status_in}


@dataclass
class StatusModel:
    status_in: list[Status] = Query(...)


@app.get("/not-working-example/")  # it now works
async def root_with_pydantic(status_inputs: StatusModel = Depends()):
    return {"status_inputs": status_inputs}