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
(
*, flat_dependant: Dependant, name: str, embed_body_fields: bool
)
| 1003 | |
| 1004 | |
| 1005 | def get_body_field( |
| 1006 | *, flat_dependant: Dependant, name: str, embed_body_fields: bool |
| 1007 | ) -> ModelField | None: |
| 1008 | """ |
| 1009 | Get a ModelField representing the request body for a path operation, combining |
| 1010 | all body parameters into a single field if necessary. |
| 1011 | |
| 1012 | Used to check if it's form data (with `isinstance(body_field, params.Form)`) |
| 1013 | or JSON and to generate the JSON Schema for a request body. |
| 1014 | |
| 1015 | This is **not** used to validate/parse the request body, that's done with each |
| 1016 | individual body parameter. |
| 1017 | """ |
| 1018 | if not flat_dependant.body_params: |
| 1019 | return None |
| 1020 | first_param = flat_dependant.body_params[0] |
| 1021 | if not embed_body_fields: |
| 1022 | return first_param |
| 1023 | model_name = "Body_" + name |
| 1024 | BodyModel = create_body_model( |
| 1025 | fields=flat_dependant.body_params, model_name=model_name |
| 1026 | ) |
| 1027 | required = any( |
| 1028 | True for f in flat_dependant.body_params if f.field_info.is_required() |
| 1029 | ) |
| 1030 | BodyFieldInfo_kwargs: dict[str, Any] = { |
| 1031 | "annotation": BodyModel, |
| 1032 | "alias": "body", |
| 1033 | } |
| 1034 | if not required: |
| 1035 | BodyFieldInfo_kwargs["default"] = None |
| 1036 | if any(isinstance(f.field_info, params.File) for f in flat_dependant.body_params): |
| 1037 | BodyFieldInfo: type[params.Body] = params.File |
| 1038 | elif any(isinstance(f.field_info, params.Form) for f in flat_dependant.body_params): |
| 1039 | BodyFieldInfo = params.Form |
| 1040 | else: |
| 1041 | BodyFieldInfo = params.Body |
| 1042 | |
| 1043 | body_param_media_types = [ |
| 1044 | f.field_info.media_type |
| 1045 | for f in flat_dependant.body_params |
| 1046 | if isinstance(f.field_info, params.Body) |
| 1047 | ] |
| 1048 | if len(set(body_param_media_types)) == 1: |
| 1049 | BodyFieldInfo_kwargs["media_type"] = body_param_media_types[0] |
| 1050 | final_field = create_model_field( |
| 1051 | name="body", |
| 1052 | type_=BodyModel, |
| 1053 | alias="body", |
| 1054 | field_info=BodyFieldInfo(**BodyFieldInfo_kwargs), |
| 1055 | ) |
| 1056 | return final_field |
| 1057 | |
| 1058 | |
| 1059 | def get_validation_alias(field: ModelField) -> str: |
no test coverage detected
searching dependent graphs…