diff options
author | Erik Johnston <erik@matrix.org> | 2022-08-24 09:58:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-24 09:58:29 +0100 |
commit | cbb157548676865793f39b4da0b7f3fa5ee01058 (patch) | |
tree | 77ff41ddcb9b01938b4efdd109cf90981c86a23e /synapse/http/servlet.py | |
parent | Newsfile (diff) | |
parent | Instrument `_check_sigs_and_hash_and_fetch` to trace time spent in child conc... (diff) | |
download | synapse-cbb157548676865793f39b4da0b7f3fa5ee01058.tar.xz |
Merge branch 'develop' into erikj/less_state_membership github/erikj/less_state_membership erikj/less_state_membership
Diffstat (limited to 'synapse/http/servlet.py')
-rw-r--r-- | synapse/http/servlet.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/synapse/http/servlet.py b/synapse/http/servlet.py index 4ff840ca0e..26aaabfb34 100644 --- a/synapse/http/servlet.py +++ b/synapse/http/servlet.py @@ -23,9 +23,12 @@ from typing import ( Optional, Sequence, Tuple, + Type, + TypeVar, overload, ) +from pydantic import BaseModel, ValidationError from typing_extensions import Literal from twisted.web.server import Request @@ -694,6 +697,28 @@ def parse_json_object_from_request( return content +Model = TypeVar("Model", bound=BaseModel) + + +def parse_and_validate_json_object_from_request( + request: Request, model_type: Type[Model] +) -> Model: + """Parse a JSON object from the body of a twisted HTTP request, then deserialise and + validate using the given pydantic model. + + Raises: + SynapseError if the request body couldn't be decoded as JSON or + if it wasn't a JSON object. + """ + content = parse_json_object_from_request(request, allow_empty_body=False) + try: + instance = model_type.parse_obj(content) + except ValidationError as e: + raise SynapseError(HTTPStatus.BAD_REQUEST, str(e), errcode=Codes.BAD_JSON) + + return instance + + def assert_params_in_dict(body: JsonDict, required: Iterable[str]) -> None: absent = [] for k in required: |