Fastapi: [QUESTION] How is the request body processed with pydantic?

Created on 12 Oct 2019  路  3Comments  路  Source: tiangolo/fastapi

I'm trying to understand how FastAPI combines Python type declarations with Pydantic to do data validation (see https://fastapi.tiangolo.com/tutorial/body/#results). Can someone please point me to the section of the codebase that is responsible for this magic?

question

Most helpful comment

You'll want to look in:

  • fastapi.dependencies.utils.get_dependant: this is where the endpoint call is converted into a "dependant" -- basically a descriptor of the things that need to be read from the request (and its body). This is where the arguments in the endpoint function signature are converted into pydantic "fields" stored on the "dependant".
  • fastapi.dependencies.utils.solve_dependencies: this is where the request (and request body) are converted into the inputs to the endpoint function, and the endpoint function is called (the endpoint function is part of the dependant).

    In particular, you'll want to look at the calls to request_params_to_args and request_body_to_args: these are where the pydantic validation is actually happening.

  • fastapi.routing.get_request_handler: the app function defined inside this function is where the body is read from the request, the dependencies are "solved", and the output of the endpoint function is obtained and converted to a response.

All 3 comments

You'll want to look in:

  • fastapi.dependencies.utils.get_dependant: this is where the endpoint call is converted into a "dependant" -- basically a descriptor of the things that need to be read from the request (and its body). This is where the arguments in the endpoint function signature are converted into pydantic "fields" stored on the "dependant".
  • fastapi.dependencies.utils.solve_dependencies: this is where the request (and request body) are converted into the inputs to the endpoint function, and the endpoint function is called (the endpoint function is part of the dependant).

    In particular, you'll want to look at the calls to request_params_to_args and request_body_to_args: these are where the pydantic validation is actually happening.

  • fastapi.routing.get_request_handler: the app function defined inside this function is where the body is read from the request, the dependencies are "solved", and the output of the endpoint function is obtained and converted to a response.

Thanks @dmontagu! :rocket:

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings