How to use the fastapi.params.Body function in fastapi

To help you get started, we’ve selected a few fastapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tiangolo / fastapi / fastapi / openapi / utils.py View on Github external
def get_openapi_operation_request_body(
    *, body_field: Optional[ModelField], model_name_map: Dict[Type[BaseModel], str]
) -> Optional[Dict]:
    if not body_field:
        return None
    assert isinstance(body_field, ModelField)
    body_schema, _, _ = field_schema(
        body_field, model_name_map=model_name_map, ref_prefix=REF_PREFIX
    )
    field_info = cast(Body, get_field_info(body_field))
    request_media_type = field_info.media_type
    required = body_field.required
    request_body_oai: Dict[str, Any] = {}
    if required:
        request_body_oai["required"] = required
    request_body_oai["content"] = {request_media_type: {"schema": body_schema}}
    return request_body_oai
github tiangolo / fastapi / fastapi / dependencies / utils.py View on Github external
field = ModelField(  # type: ignore
            name=param.name,
            type_=annotation,
            default=None if required else default_value,
            alias=alias,
            required=required,
            model_config=BaseConfig,
            class_validators={},
            schema=field_info,
        )
        field.required = required
    if not had_schema and not is_scalar_field(field=field):
        if PYDANTIC_1:
            field.field_info = params.Body(field_info.default)
        else:
            field.schema = params.Body(field_info.default)  # type: ignore  # pragma: nocover

    return field
github tiangolo / fastapi / fastapi / dependencies / utils.py View on Github external
BodyModel = create_model(model_name)
    for f in flat_dependant.body_params:
        BodyModel.__fields__[f.name] = get_schema_compatible_field(field=f)
    required = any(True for f in flat_dependant.body_params if f.required)

    BodyFieldInfo_kwargs: Dict[str, Any] = dict(default=None)
    if any(
        isinstance(get_field_info(f), params.File) for f in flat_dependant.body_params
    ):
        BodyFieldInfo: Type[params.Body] = params.File
    elif any(
        isinstance(get_field_info(f), params.Form) for f in flat_dependant.body_params
    ):
        BodyFieldInfo = params.Form
    else:
        BodyFieldInfo = params.Body

        body_param_media_types = [
            getattr(get_field_info(f), "media_type")
            for f in flat_dependant.body_params
            if isinstance(get_field_info(f), params.Body)
        ]
        if len(set(body_param_media_types)) == 1:
            BodyFieldInfo_kwargs["media_type"] = body_param_media_types[0]
    if PYDANTIC_1:
        field = ModelField(
            name="body",
            type_=BodyModel,
            default=None,
            required=required,
            model_config=BaseConfig,
            class_validators={},
github tiangolo / fastapi / fastapi / param_functions.py View on Github external
*,
    embed: bool = False,
    media_type: str = "application/json",
    alias: str = None,
    title: str = None,
    description: str = None,
    gt: float = None,
    ge: float = None,
    lt: float = None,
    le: float = None,
    min_length: int = None,
    max_length: int = None,
    regex: str = None,
    **extra: Any,
) -> Any:
    return params.Body(
        default,
        embed=embed,
        media_type=media_type,
        alias=alias,
        title=title,
        description=description,
        gt=gt,
        ge=ge,
        lt=lt,
        le=le,
        min_length=min_length,
        max_length=max_length,
        regex=regex,
        **extra,
    )
github tiangolo / fastapi / fastapi / dependencies / utils.py View on Github external
BodyFieldInfo_kwargs: Dict[str, Any] = dict(default=None)
    if any(
        isinstance(get_field_info(f), params.File) for f in flat_dependant.body_params
    ):
        BodyFieldInfo: Type[params.Body] = params.File
    elif any(
        isinstance(get_field_info(f), params.Form) for f in flat_dependant.body_params
    ):
        BodyFieldInfo = params.Form
    else:
        BodyFieldInfo = params.Body

        body_param_media_types = [
            getattr(get_field_info(f), "media_type")
            for f in flat_dependant.body_params
            if isinstance(get_field_info(f), params.Body)
        ]
        if len(set(body_param_media_types)) == 1:
            BodyFieldInfo_kwargs["media_type"] = body_param_media_types[0]
    if PYDANTIC_1:
        field = ModelField(
            name="body",
            type_=BodyModel,
            default=None,
            required=required,
            model_config=BaseConfig,
            class_validators={},
            alias="body",
            field_info=BodyFieldInfo(**BodyFieldInfo_kwargs),
        )
    else:  # pragma: nocover
        field = ModelField(  # type: ignore