fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

🐛 Fix truncating the model's description with form feed (`\f`) character for Pydantic V2

Open YuriiMotov opened this issue 8 months ago • 0 comments

Problem description

app = FastAPI()

class MyModel(BaseModel):
    """
    A model with a form feed character in the title.
    \f
    Text after form feed character.
    """

@app.get("/foo")
def foo(v: MyModel):
    pass

The code above will produce different description for MyModel in schema depending on whether Pydantic V1 or Pydantic V2 is used.

With Pydantic V1 the description will be truncated starting from \f character (only first line will be in schema). With Pydantic V2 it will not be truncated (all 3 lines will be in schema).

Feature was introduced in #3032 . But when the support of Pydantic V2 was added to FastAPI this feature wasn't added to the code related to Pydantic V2 (I think it was just oversight).

Related discussion(s):

https://github.com/fastapi/fastapi/discussions/9038

Remark about cast

I added commit that improves typing of new part of code by using cast. Not sure it's needed. If not, it can be easily reverted.

YuriiMotov avatar May 08 '25 21:05 YuriiMotov