Пример #1
0
def serialize_response(
    *,
    field: ModelField = None,
    response: Response,
    include: Union[SetIntStr, DictIntStrAny] = None,
    exclude: Union[SetIntStr, DictIntStrAny] = set(),
    by_alias: bool = True,
    exclude_unset: bool = False,
) -> Any:
    if field:
        errors = []
        if exclude_unset and isinstance(response, BaseModel):
            if PYDANTIC_1:
                response = response.dict(exclude_unset=exclude_unset)
            else:
                response = response.dict(
                    skip_defaults=exclude_unset)  # pragma: nocover
        value, errors_ = field.validate(response, {}, loc=("response", ))
        if isinstance(errors_, ErrorWrapper):
            errors.append(errors_)
        elif isinstance(errors_, list):
            errors.extend(errors_)
        if errors:
            raise ValidationError(errors, field.type_)
        return jsonable_encoder(
            value,
            include=include,
            exclude=exclude,
            by_alias=by_alias,
            exclude_unset=exclude_unset,
        )
    else:
        return jsonable_encoder(response)
Пример #2
0
async def serialize_response(
    *,
    field: ModelField = None,
    response_content: Any,
    include: Union[SetIntStr, DictIntStrAny] = None,
    exclude: Union[SetIntStr, DictIntStrAny] = set(),
    by_alias: bool = True,
    exclude_unset: bool = False,
    exclude_defaults: bool = False,
    exclude_none: bool = False,
    is_coroutine: bool = True,
) -> Any:
    if field:
        errors = []
        response_content = _prepare_response_content(
            response_content,
            by_alias=by_alias,
            exclude_unset=exclude_unset,
            exclude_defaults=exclude_defaults,
            exclude_none=exclude_none,
        )
        if is_coroutine:
            value, errors_ = field.validate(response_content, {}, loc=("response",))
        else:
            value, errors_ = await run_in_threadpool(
                field.validate, response_content, {}, loc=("response",)
            )
        if isinstance(errors_, ErrorWrapper):
            errors.append(errors_)
        elif isinstance(errors_, list):
            errors.extend(errors_)
        if errors:
            raise ValidationError(errors, field.type_)
        return jsonable_encoder(
            value,
            include=include,
            exclude=exclude,
            by_alias=by_alias,
            exclude_unset=exclude_unset,
            exclude_defaults=exclude_defaults,
            exclude_none=exclude_none,
        )
    else:
        return jsonable_encoder(
            response_content,
            include=include,
            exclude=exclude,
            by_alias=by_alias,
            exclude_unset=exclude_unset,
            exclude_defaults=exclude_defaults,
            exclude_none=exclude_none,
        )
Пример #3
0
def check_field_type(field: ModelField, value: V) -> V:
    _, errs_ = field.validate(value, {}, loc=())
    if errs_:
        raise TypeMisMatch(field, value)
    return value
Пример #4
0
async def serialize_response(
    *,
    field: ModelField = None,
    response_content: Any,
    include: Union[SetIntStr, DictIntStrAny] = None,
    exclude: Union[SetIntStr, DictIntStrAny] = set(),
    by_alias: bool = True,
    exclude_unset: bool = False,
    exclude_defaults: bool = False,
    exclude_none: bool = False,
    is_coroutine: bool = True,
    skip_jsonable_encoder: bool = False,
    skip_validation: bool = False,
) -> Any:
    if field:
        errors = []
        response_content = _prepare_response_content(
            response_content,
            by_alias=by_alias,
            exclude_unset=exclude_unset,
            exclude_defaults=exclude_defaults,
            exclude_none=exclude_none,
        )
        if skip_validation:
            # If include is explicitly set, then never skip jsonable_encoder
            #
            # _prepare_response_content emits a decoded dict/list python datastructure.
            # It however doesn't complete or coerce any types, so can only be used when
            #  explicitly specifying that one knows what one is doing, and disabling validation
            #
            # If you trust the encoder to handle all your types then we can just return
            #  the output of _prepare_response_content here
            if include is None and skip_jsonable_encoder is True:
                return response_content

            value = response_content
        else:
            if is_coroutine:
                value, errors_ = field.validate(response_content, {},
                                                loc=("response", ))
            else:
                value, errors_ = await run_in_threadpool(field.validate,
                                                         response_content, {},
                                                         loc=("response", ))
            if isinstance(errors_, ErrorWrapper):
                errors.append(errors_)
            elif isinstance(errors_, list):
                errors.extend(errors_)
            if errors:
                raise ValidationError(errors, field.type_)
        return jsonable_encoder(
            value,
            include=include,
            exclude=exclude,
            by_alias=by_alias,
            exclude_unset=exclude_unset,
            exclude_defaults=exclude_defaults,
            exclude_none=exclude_none,
        )
    else:
        # If you trust the encoder to handle all your types then we can just return
        #  the output of _prepare_response_content here
        return (response_content if skip_jsonable_encoder else
                jsonable_encoder(response_content))