diff --git a/zerver/tests/test_service_bot_system.py b/zerver/tests/test_service_bot_system.py index b5900e2bd9..5166f335ba 100644 --- a/zerver/tests/test_service_bot_system.py +++ b/zerver/tests/test_service_bot_system.py @@ -299,7 +299,7 @@ class TestServiceBotStateHandler(ZulipTestCase): "keys": ["This is a list, but should be a serialized string."], } result = self.client_get("/json/bot_storage", invalid_params) - self.assert_json_error(result, 'Argument "keys" is not valid JSON.') + self.assert_json_error(result, "keys is not valid JSON") params = { "keys": orjson.dumps(["key 1", "nonexistent key"]).decode(), @@ -311,7 +311,7 @@ class TestServiceBotStateHandler(ZulipTestCase): "storage": orjson.dumps({"foo": [1, 2, 3]}).decode(), } result = self.client_put("/json/bot_storage", params) - self.assert_json_error(result, "storage contains a value that is not a string") + self.assert_json_error(result, 'storage["foo"] is not a string') # Remove some entries. keys_to_remove = ["key 1", "key 2"] diff --git a/zerver/views/storage.py b/zerver/views/storage.py index 9c13acfb96..8988907ef6 100644 --- a/zerver/views/storage.py +++ b/zerver/views/storage.py @@ -1,6 +1,7 @@ from typing import Dict, List, Optional from django.http import HttpRequest, HttpResponse +from pydantic import Json from zerver.lib.bot_storage import ( StateError, @@ -10,17 +11,17 @@ from zerver.lib.bot_storage import ( set_bot_storage, ) from zerver.lib.exceptions import JsonableError -from zerver.lib.request import REQ, has_request_variables from zerver.lib.response import json_success -from zerver.lib.validator import check_dict, check_list, check_string +from zerver.lib.typed_endpoint import typed_endpoint from zerver.models import UserProfile -@has_request_variables +@typed_endpoint def update_storage( request: HttpRequest, user_profile: UserProfile, - storage: Dict[str, str] = REQ(json_validator=check_dict([], value_validator=check_string)), + *, + storage: Json[Dict[str, str]], ) -> HttpResponse: try: set_bot_storage(user_profile, list(storage.items())) @@ -29,11 +30,12 @@ def update_storage( return json_success(request) -@has_request_variables +@typed_endpoint def get_storage( request: HttpRequest, user_profile: UserProfile, - keys: Optional[List[str]] = REQ(json_validator=check_list(check_string), default=None), + *, + keys: Json[Optional[List[str]]] = None, ) -> HttpResponse: if keys is None: keys = get_keys_in_bot_storage(user_profile) @@ -44,11 +46,12 @@ def get_storage( return json_success(request, data={"storage": storage}) -@has_request_variables +@typed_endpoint def remove_storage( request: HttpRequest, user_profile: UserProfile, - keys: Optional[List[str]] = REQ(json_validator=check_list(check_string), default=None), + *, + keys: Json[Optional[List[str]]] = None, ) -> HttpResponse: if keys is None: keys = get_keys_in_bot_storage(user_profile)