question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] NoneType model value examples are not supported

See original GitHub issue

Description

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:open
  • Created 3 years ago
  • Reactions:6
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
sommergiscommented, Jan 8, 2021

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

class APIErrorMessage(BaseModel):
    """ Model for basic API response for an unsuccessful request. """
    content: Any = None
    error: str = ""
    detail: Optional[str] = None

    class Config:
        """ Real world example for OpenAPI documentation """
        schema_extra = {
            "example": {
                "content": None,
                "error": "Authentication Error",
                "detail": "key validity expired"
            }
        }

OpenAPI Response


"example": {
    "error": "Authentication Error",
    "detail": "key validity expired"
}

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

8reactions
abuendiacommented, Sep 22, 2021

Hi @tiangolo - Could you please take a look at this?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found