Skip to content

Allow None to be used as a valid json value. #1661

@bharel

Description

@bharel

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • [] After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Description

ATM, if you receive a post with the following json: {data: null}

And you have the following definition:

@app.post("/")
def post_data(data: Optional[str] = Body(...)):
  pass

FastAPI will say data was not passed. That's because it internally uses None as a placeholder for non-existing fields, instead of a sentinel value.

I wish to explicitly require null as one of the possible values, and not put a default value of None.

I've attempted to open a PR, but it failed. It looks like the issue is with dependencies.utils.request_body_to_args using None as a default value for value.

Environment

  • OS: [e.g. Linux / Windows / macOS]: macOS
  • FastAPI Version [e.g. 0.3.0]: 0.58.1
  • Python version: 3.8.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions