Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Optional form field not working with test client #12245

Copy link
Copy link
@Kludex

Description

@Kludex
Issue body actions

Discussed in #12227

Originally posted by MartinAchtnerAA September 19, 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, Literal, Optional

from fastapi import FastAPI, Form
from fastapi.testclient import TestClient

app = FastAPI()

@app.post("/")
async def read_main(
    attribute : Annotated[Optional[Literal["abc", "def"]], Form()]
):
    print(attribute)


client = TestClient(app)


data = {}
data["attribute"] = None

response = client.post("/", data=data)
print(response.content)
assert response.status_code == 200

Description

In FastAPI 0.113 .0 the above code passes.
Since version 0.114.0 the response is a 422 status code with error message '{"detail":[{"type":"literal_error","loc":["body","attribute"],"msg":"Input should be 'abc' or 'def'","input":"","ctx":{"expected":"'abc' or 'def'"}}]}'

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.114.0

Pydantic Version

2.9.1

Python Version

3.12.3

Additional Context

No response

spladug, binbjz and agbares

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion or problemQuestion or problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.