Home / Function/ _should_embed_body_fields() — fastapi Function Reference

_should_embed_body_fields() — fastapi Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  18580490_8a5f_0766_c8e1_329e703c7527["_should_embed_body_fields()"]
  9e602cbf_3139_86ae_5666_97b8806942de["utils.py"]
  18580490_8a5f_0766_c8e1_329e703c7527 -->|defined in| 9e602cbf_3139_86ae_5666_97b8806942de
  0df3979a_d2ac_6dd9_1bb1_6629b89a941b["__init__()"]
  0df3979a_d2ac_6dd9_1bb1_6629b89a941b -->|calls| 18580490_8a5f_0766_c8e1_329e703c7527
  d8b4fb83_3521_0b7e_fcfa_0c7f161ca116["__init__()"]
  d8b4fb83_3521_0b7e_fcfa_0c7f161ca116 -->|calls| 18580490_8a5f_0766_c8e1_329e703c7527
  0d837460_f27d_53af_8d59_7190a2a974f5["is_union_of_base_models()"]
  18580490_8a5f_0766_c8e1_329e703c7527 -->|calls| 0d837460_f27d_53af_8d59_7190a2a974f5
  style 18580490_8a5f_0766_c8e1_329e703c7527 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fastapi/dependencies/utils.py lines 858–879

def _should_embed_body_fields(fields: list[ModelField]) -> bool:
    if not fields:
        return False
    # More than one dependency could have the same field, it would show up as multiple
    # fields but it's the same one, so count them by name
    body_param_names_set = {field.name for field in fields}
    # A top level field has to be a single field, not multiple
    if len(body_param_names_set) > 1:
        return True
    first_field = fields[0]
    # If it explicitly specifies it is embedded, it has to be embedded
    if getattr(first_field.field_info, "embed", None):
        return True
    # If it's a Form (or File) field, it has to be a BaseModel (or a union of BaseModels) to be top level
    # otherwise it has to be embedded, so that the key value pair can be extracted
    if (
        isinstance(first_field.field_info, params.Form)
        and not lenient_issubclass(first_field.field_info.annotation, BaseModel)
        and not is_union_of_base_models(first_field.field_info.annotation)
    ):
        return True
    return False

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free