Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Support Python internal description on Pydantic model's docstring #3032

Merged
merged 3 commits into from Sep 2, 2022

Conversation

Kludex
Copy link
Sponsor Collaborator

@Kludex Kludex commented Apr 1, 2021

Problem

Currently, on FastAPI endpoint functions we can document for OpenAPI description/Swagger and for internal Python purposes:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

@app.get("/")
def useful():
    """My description.
    \f
    Internal documentation.
    """
    ...

On Swagger we'll have:
image

When documenting models, it doesn't have the same behavior:

from enum import Enum
from fastapi import FastAPI

class Banana(str, Enum):
    """My description.
    \f
    Internal documentation.
    """
    HAHA = "haha"
    HOHO = "hoho"

app = FastAPI()

@app.get("/")
def useful(banana: Banana):
    ...

On Swagger, we'll have:
image

Solution

The proposed two lines of code on this PR, makes it possible to have the same behavior as the endpoint description:
image

It follows the same idea as:

self.description = self.description.split("\f")[0]

Related issues

Questions & Alternatives

@Kludex
Copy link
Sponsor Collaborator Author

Kludex commented Apr 1, 2021

Do you guys have an opinion on this? @PrettyWood @samuelcolvin

@github-actions
Copy link
Contributor

github-actions bot commented Apr 1, 2021

📝 Docs preview for commit 00da79c at: https://606608e5d9c9b620c64fe21f--fastapi.netlify.app

@codecov
Copy link

codecov bot commented Apr 1, 2021

Codecov Report

Merging #3032 (00becde) into master (b1d0f1e) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master     #3032   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          538       538           
  Lines        13870     13872    +2     
=========================================
+ Hits         13870     13872    +2     
Impacted Files Coverage Δ
fastapi/utils.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 1, 2021

📝 Docs preview for commit 1c93179 at: https://60660aa9f6c5301f7f6c8b97--fastapi.netlify.app

@Kludex Kludex changed the title ✨ Support Python internal description on Pydantic model's docstrings ✨ Support Python internal description on Pydantic model's docstring Apr 1, 2021
@killswitch-GUI
Copy link

Just bumping.

@killswitch-GUI
Copy link

@Kludex Think this one will make it in?

@Kludex
Copy link
Sponsor Collaborator Author

Kludex commented Oct 14, 2021

I don't know. 😗

@github-actions
Copy link
Contributor

github-actions bot commented Sep 2, 2022

📝 Docs preview for commit 00becde at: https://6311f9a78e0d811162b5915a--fastapi.netlify.app

@tiangolo tiangolo changed the title ✨ Support Python internal description on Pydantic model's docstring ✨ Support Python internal description on Pydantic model's docstring Sep 2, 2022
@tiangolo
Copy link
Owner

tiangolo commented Sep 2, 2022

Awesome, thanks @Kludex! 🍰

@tiangolo tiangolo merged commit 30b3905 into tiangolo:master Sep 2, 2022
@killswitch-GUI
Copy link

@tiangolo and @Kludex thanks for helping fix this! The team will be thankful!

@Kludex Kludex deleted the feat/comment-on-pydantic-models branch September 2, 2022 12:45
@Kludex
Copy link
Sponsor Collaborator Author

Kludex commented Sep 2, 2022

@tiangolo Even if coverage didn't drop, Kludex forgot to add a test case for this, jfyk.

@tiangolo
Copy link
Owner

tiangolo commented Sep 2, 2022

Thanks! We can probably add a test in another PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants