question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
iudeencommented, Jul 20, 2022

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}
0reactions
jerinpetergeorgecommented, Jul 20, 2022

@jerinpetergeorge if you are happy with the solution, you can close this and open a ISSUE/BUG to discuss on why the “normal” approach is not working, and probably finding a better solution!

I’m kind of okay with the solution since we’re still not sure “why” FastAPI doesn’t support it - is that on purpose? Or a bug?

Also, I hope this current GH issue considers a bug - or I wrote like that in the first place, which makes me think that it is better not to close this GH issue at the moment.

I would be super happy if someone could change the label of this issue from https://github.com/tiangolo/fastapi/labels/question to https://github.com/tiangolo/fastapi/labels/bug

Read more comments on GitHub >

github_iconTop Results From Across the Web

Body - Multiple Parameters - FastAPI
Now that we have seen how to use Path and Query , let's see more advanced uses of ... parameters in the function...
Read more >
tiangolo/fastapi - Gitter
Add docs for correctly using FastAPI with Peewee ORM. Including how to overwrite parts of ... Has anyone achieved this sort of API...
Read more >
Cool Things You Can Do With Pydantic - Medium
Pydantic models. Pydantic is a useful library for data parsing and validation. It coerces input types to the declared type (using type ...
Read more >
Generate Pydantic Models from JSON in the browser : r/Python
Usually when I'm using some third party API that may or may not have documentation, I write a Pydantic model to represent the...
Read more >
Ssaw.assignments module - API
I'm working on an application to perform assignments. The code below creates the assignment but ... from ssaw.models import Assignment
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found