Browse Source

♻️ Refactor serialize_response parameter name (#1031)

pull/1056/head
Patrick McKenna 5 years ago
committed by GitHub
parent
commit
e26f94018c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      fastapi/routing.py

18
fastapi/routing.py

@ -50,7 +50,7 @@ except ImportError: # pragma: nocover
async def serialize_response( async def serialize_response(
*, *,
field: ModelField = None, field: ModelField = None,
response: Response, response_content: Any,
include: Union[SetIntStr, DictIntStrAny] = None, include: Union[SetIntStr, DictIntStrAny] = None,
exclude: Union[SetIntStr, DictIntStrAny] = set(), exclude: Union[SetIntStr, DictIntStrAny] = set(),
by_alias: bool = True, by_alias: bool = True,
@ -59,16 +59,18 @@ async def serialize_response(
) -> Any: ) -> Any:
if field: if field:
errors = [] errors = []
if exclude_unset and isinstance(response, BaseModel): if exclude_unset and isinstance(response_content, BaseModel):
if PYDANTIC_1: if PYDANTIC_1:
response = response.dict(exclude_unset=exclude_unset) response_content = response_content.dict(exclude_unset=exclude_unset)
else: else:
response = response.dict(skip_defaults=exclude_unset) # pragma: nocover response_content = response_content.dict(
skip_defaults=exclude_unset
) # pragma: nocover
if is_coroutine: if is_coroutine:
value, errors_ = field.validate(response, {}, loc=("response",)) value, errors_ = field.validate(response_content, {}, loc=("response",))
else: else:
value, errors_ = await run_in_threadpool( value, errors_ = await run_in_threadpool(
field.validate, response, {}, loc=("response",) field.validate, response_content, {}, loc=("response",)
) )
if isinstance(errors_, ErrorWrapper): if isinstance(errors_, ErrorWrapper):
errors.append(errors_) errors.append(errors_)
@ -84,7 +86,7 @@ async def serialize_response(
exclude_unset=exclude_unset, exclude_unset=exclude_unset,
) )
else: else:
return jsonable_encoder(response) return jsonable_encoder(response_content)
async def run_endpoint_function( async def run_endpoint_function(
@ -151,7 +153,7 @@ def get_request_handler(
return raw_response return raw_response
response_data = await serialize_response( response_data = await serialize_response(
field=response_field, field=response_field,
response=raw_response, response_content=raw_response,
include=response_model_include, include=response_model_include,
exclude=response_model_exclude, exclude=response_model_exclude,
by_alias=response_model_by_alias, by_alias=response_model_by_alias,

Loading…
Cancel
Save