📝 Clarify required but nullable request body fields#14751
📝 Clarify required but nullable request body fields#14751azamzar wants to merge 12 commits intofastapi:masterfrom
Conversation
📝 Docs previewLast commit 2682f7d at: https://e9874f20.fastapitiangolo.pages.dev Modified Pages |
8a19a81 to
fa6af8b
Compare
|
This PR complements #14653 by providing an alternative explanation and example for required but nullable request body fields.
This is intended to enhance the explanation already started in #14653 and provide additional clarity for readers. Happy to collaborate and merge the best parts with #14653 if the maintainers prefer. |
| ```python | ||
| from typing import Optional | ||
| from fastapi import FastAPI | ||
| from pydantic import BaseModel | ||
|
|
||
| app = FastAPI() | ||
|
|
||
| class Item(BaseModel): | ||
| description: Optional[str] | ||
|
|
||
| @app.post("/items/") | ||
| async def create_item(item: Item): | ||
| return item | ||
| ``` |
There was a problem hiding this comment.
All code examples should be placed in separate files and covered by tests.
Please, look how it's done for other code examples in docs
There was a problem hiding this comment.
Thanks for the feedback!
I’ve moved the example to separate numbered tutorial files under docs_src/body (for Python 3.9 and 3.10+), added tests following the existing pattern, and updated the documentation to reference them.
There was a problem hiding this comment.
Hi @YuriiMotov ! Just a gentle ping in case this PR got buried.
All checks are green now — happy to adjust anything if needed. Thanks!
f12a2bb to
a6196a3
Compare
91469e0 to
eec2fe9
Compare
There was a problem hiding this comment.
@azamzar, thanks!
In addition to code review comments, the test file should be named test_tutorial005.py and placed in tests/test_tutorial/test_body
…files, add test_tutorial005
Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
|
Hi @YuriiMotov, the requested changes have been implemented and all tests are passing. Please take another look. Thanks! |
|
Please, review carefully before asking others for review |
|
@YuriiMotov The requested changes have been applied: the description field is now required as intended. |
|
Thanks for the interest, but this is already explained in a different point in the tutorial, I don't think it's necessary to explain it here too. I'll pass on this for now, thanks! ☕ |
Thanks for the clarification! That makes sense. |
This PR clarifies how request body fields can be required while still allowing
null(None) values when usingOptional[T]without a default.Changes included:
None" under the Request Body tutorial.This is a documentation-only change; no runtime or API behavior is affected.