Skip to content

Conversation

YuriiMotov
Copy link
Contributor

@YuriiMotov YuriiMotov commented May 8, 2025

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):

#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 YuriiMotov force-pushed the fix-support-form_feed-for-model-docstring branch from ce0ad4e to 3c4ca86 Compare May 8, 2025 21:33
@YuriiMotov YuriiMotov added the bug Something isn't working label May 8, 2025
Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you! 🙌

@tiangolo tiangolo merged commit 535d5b3 into fastapi:master Jun 17, 2025
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants