[BUG] NoneType model value examples are not supported
See original GitHub issueDescription
I’m not able to pass a None
/ as an example for a model value created with pydantic.Field
Looks like parser can’t find a difference between example=None
as a NoneType variable and “missing example field”
class SomeResponse(BaseModel):
city: Optional[str] = Field(..., nullable=True, example=None)
Expected openapi.json
"city": {
"title": "City",
"type": "string",
"nullable": true,
"example": null
}
Occurred openapi.json
"city": {
"title": "City",
"type": "string",
"nullable": true
}
Environment
-
OS: macOS
-
FastAPI Version: 0.61.1
-
pydantic Version: 1.6.1
-
Python version: 3.7.9
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:10 (2 by maintainers)
Top Results From Across the Web
How to Fix TypeError in Python: NoneType Object Is Not Iterable
Here's an example of a Python TypeError: NoneType Object Is Not Iterable thrown when trying iterate over a None value:
Read more >python - 'NoneType' object has no attribute 'model' error when ...
python - 'NoneType' object has no attribute 'model' error when retrieving POST request data - Stack Overflow. Stack Overflow for Teams – Start ......
Read more >Jinja Errors - Bloomreach Documentation
Error 1: "'NoneType' object is not iterable" The error occurs when Jinja tries to iterate some empty, not existing or ... For example,...
Read more >TypeError: 'NoneType' object is not subscriptable
NoneType is the type of the None object which represents a lack of value, for example, a function that does not explicitly return...
Read more >Common issues and solutions - mypy 0.991 documentation
This section has examples of cases when you need to update your code to use static typing, and ideas for working around issues...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi all, I ran into the same issue today. It seems like None values of fields in examples in OpenAPI schema generation are being excluded by FastAPI. The API works as expected, it’s only an issue in the OpenAPI schema.
Sample Model
OpenAPI Response
Maybe this is the location in the code?
exclude_none=True
https://github.com/tiangolo/fastapi/blob/d2eb4a71eef08e201082d23517d4ddc99ff258b5/fastapi/openapi/utils.py#L377
Environment
FastAPI Version: 0.62.0
Hi @tiangolo - Could you please take a look at this?