Home / Function/ get_body_field() — fastapi Function Reference

get_body_field() — fastapi Function Reference

Architecture documentation for the get_body_field() function in utils.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  442274ca_f275_350e_f39a_7f88cd50be4a["get_body_field()"]
  9e602cbf_3139_86ae_5666_97b8806942de["utils.py"]
  442274ca_f275_350e_f39a_7f88cd50be4a -->|defined in| 9e602cbf_3139_86ae_5666_97b8806942de
  d8b4fb83_3521_0b7e_fcfa_0c7f161ca116["__init__()"]
  d8b4fb83_3521_0b7e_fcfa_0c7f161ca116 -->|calls| 442274ca_f275_350e_f39a_7f88cd50be4a
  8e7d845c_a4e7_ee84_5295_5f3b995b05b4["create_model_field()"]
  442274ca_f275_350e_f39a_7f88cd50be4a -->|calls| 8e7d845c_a4e7_ee84_5295_5f3b995b05b4
  style 442274ca_f275_350e_f39a_7f88cd50be4a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fastapi/dependencies/utils.py lines 971–1022

def get_body_field(
    *, flat_dependant: Dependant, name: str, embed_body_fields: bool
) -> Optional[ModelField]:
    """
    Get a ModelField representing the request body for a path operation, combining
    all body parameters into a single field if necessary.

    Used to check if it's form data (with `isinstance(body_field, params.Form)`)
    or JSON and to generate the JSON Schema for a request body.

    This is **not** used to validate/parse the request body, that's done with each
    individual body parameter.
    """
    if not flat_dependant.body_params:
        return None
    first_param = flat_dependant.body_params[0]
    if not embed_body_fields:
        return first_param
    model_name = "Body_" + name
    BodyModel = create_body_model(
        fields=flat_dependant.body_params, model_name=model_name
    )
    required = any(
        True for f in flat_dependant.body_params if f.field_info.is_required()
    )
    BodyFieldInfo_kwargs: dict[str, Any] = {
        "annotation": BodyModel,
        "alias": "body",
    }
    if not required:
        BodyFieldInfo_kwargs["default"] = None
    if any(isinstance(f.field_info, params.File) for f in flat_dependant.body_params):
        BodyFieldInfo: type[params.Body] = params.File
    elif any(isinstance(f.field_info, params.Form) for f in flat_dependant.body_params):
        BodyFieldInfo = params.Form
    else:
        BodyFieldInfo = params.Body

        body_param_media_types = [
            f.field_info.media_type
            for f in flat_dependant.body_params
            if isinstance(f.field_info, params.Body)
        ]
        if len(set(body_param_media_types)) == 1:
            BodyFieldInfo_kwargs["media_type"] = body_param_media_types[0]
    final_field = create_model_field(
        name="body",
        type_=BodyModel,
        alias="body",
        field_info=BodyFieldInfo(**BodyFieldInfo_kwargs),
    )
    return final_field

Subdomains

Called By

Frequently Asked Questions

What does get_body_field() do?
get_body_field() is a function in the fastapi codebase, defined in fastapi/dependencies/utils.py.
Where is get_body_field() defined?
get_body_field() is defined in fastapi/dependencies/utils.py at line 971.
What does get_body_field() call?
get_body_field() calls 1 function(s): create_model_field.
What calls get_body_field()?
get_body_field() is called by 1 function(s): __init__.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free