Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Question about "Required, can be None" parameter #12419

@Kludex

Description

@Kludex

Discussed in #12418

Originally posted by bowenerchen October 10, 2024

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question 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

from typing import Annotated

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Annotated[str | None, Query(min_length=3)] = ...):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

Description

https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#required-can-be-none
QQ_1728534861112
how to pass a None value to the required query parameter q?

Operating System

macOS

Operating System Details

Sonoma 14.7 M2 Max

FastAPI Version

0.115.0

Pydantic Version

2.5.3

Python Version

Python 3.12.4

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation about how to use FastAPI

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions