|
|
@ -24,7 +24,7 @@ from fastapi._compat import ( |
|
|
|
PYDANTIC_V2, |
|
|
|
ErrorWrapper, |
|
|
|
ModelField, |
|
|
|
Required, |
|
|
|
RequiredParam, |
|
|
|
Undefined, |
|
|
|
_regenerate_error_with_loc, |
|
|
|
copy_field_info, |
|
|
@ -377,7 +377,9 @@ def analyze_param( |
|
|
|
field_info = copy_field_info( |
|
|
|
field_info=fastapi_annotation, annotation=use_annotation |
|
|
|
) |
|
|
|
assert field_info.default is Undefined or field_info.default is Required, ( |
|
|
|
assert ( |
|
|
|
field_info.default is Undefined or field_info.default is RequiredParam |
|
|
|
), ( |
|
|
|
f"`{field_info.__class__.__name__}` default value cannot be set in" |
|
|
|
f" `Annotated` for {param_name!r}. Set the default value with `=` instead." |
|
|
|
) |
|
|
@ -385,7 +387,7 @@ def analyze_param( |
|
|
|
assert not is_path_param, "Path parameters cannot have default values" |
|
|
|
field_info.default = value |
|
|
|
else: |
|
|
|
field_info.default = Required |
|
|
|
field_info.default = RequiredParam |
|
|
|
# Get Annotated Depends |
|
|
|
elif isinstance(fastapi_annotation, params.Depends): |
|
|
|
depends = fastapi_annotation |
|
|
@ -434,9 +436,9 @@ def analyze_param( |
|
|
|
), f"Cannot specify FastAPI annotation for type {type_annotation!r}" |
|
|
|
# Handle default assignations, neither field_info nor depends was not found in Annotated nor default value |
|
|
|
elif field_info is None and depends is None: |
|
|
|
default_value = value if value is not inspect.Signature.empty else Required |
|
|
|
default_value = value if value is not inspect.Signature.empty else RequiredParam |
|
|
|
if is_path_param: |
|
|
|
# We might check here that `default_value is Required`, but the fact is that the same |
|
|
|
# We might check here that `default_value is RequiredParam`, but the fact is that the same |
|
|
|
# parameter might sometimes be a path parameter and sometimes not. See |
|
|
|
# `tests/test_infer_param_optionality.py` for an example. |
|
|
|
field_info = params.Path(annotation=use_annotation) |
|
|
@ -480,7 +482,7 @@ def analyze_param( |
|
|
|
type_=use_annotation_from_field_info, |
|
|
|
default=field_info.default, |
|
|
|
alias=alias, |
|
|
|
required=field_info.default in (Required, Undefined), |
|
|
|
required=field_info.default in (RequiredParam, Undefined), |
|
|
|
field_info=field_info, |
|
|
|
) |
|
|
|
if is_path_param: |
|
|
|