Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_send: Convert to typed endpoint.
Kenneth Rodrigues2483e600a255
diff --git a/zerver/lib/validator.py b/zerver/lib/validator.pyindex 53e7f462edb..e4cbe755d8b 100644--- a/zerver/lib/validator.py+++ b/zerver/lib/validator.py@@ -572,10 +572,6 @@ def to_non_negative_int(var_name: str, s: str, max_int_size: int = 2**32 - 1) ->return x-def to_float(var_name: str, s: str) -> float:- return float(s)--def check_string_or_int_list(var_name: str, val: object) -> str | list[int]:if isinstance(val, str):return valdiff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.pyindex eeb0c0cb2d2..7d60443e96e 100644--- a/zerver/tests/test_openapi.py+++ b/zerver/tests/test_openapi.py@@ -44,6 +44,7 @@ VARMAP = {"boolean": bool,"object": dict,"NoneType": type(None),+ "number": float,}@@ -423,17 +424,25 @@ do not match the types declared in the implementation of {function.__name__}.\n"# matching that of our OpenAPI spec. If not so, hint that the# Json[T] wrapper might be missing from the type annotation.if actual_param.request_var_name in json_request_var_names:- self.assertEqual(- actual_param_schema.get("contentMediaType"),- "application/json",- USE_JSON_CONTENT_TYPE_HINT.format(- param_name=actual_param.param_name,- param_type=actual_param.param_type,- ),- )- # actual_param_schema is a json_schema. Reference:- # https://docs.pydantic.dev/latest/api/json_schema/#pydantic.json_schema.GenerateJsonSchema.json_schema- actual_param_schema = actual_param_schema["contentSchema"]+ # skipping this check for send_message_backend 'to' parameter because it is a+ # special case where the content type of the parameter is application/json but the+ # parameter may or may not be JSON encoded since previously we also accepted a raw+ # string and some ad-hoc bot might still depend on sending a raw string.+ if (+ function.__name__ != "send_message_backend"+ or actual_param.param_name != "req_to"+ ):+ self.assertEqual(+ actual_param_schema.get("contentMediaType"),+ "application/json",+ USE_JSON_CONTENT_TYPE_HINT.format(+ param_name=actual_param.param_name,+ param_type=actual_param.param_type,+ ),+ )+ # actual_param_schema is a json_schema. Reference:+ # https://docs.pydantic.dev/latest/api/json_schema/#pydantic.json_schema.GenerateJsonSchema.json_schema+ actual_param_schema = actual_param_schema["contentSchema"]elif "contentMediaType" in actual_param_schema:function_schema_type = schema_type(actual_param_schema, defs_mapping)# We do not specify that the content type of int or booldiff --git a/zerver/views/message_send.py b/zerver/views/message_send.pyindex 88429a7e92c..d5fcbbf7b5e 100644--- a/zerver/views/message_send.py+++ b/zerver/views/message_send.py@@ -1,11 +1,12 @@from collections.abc import Iterable, Sequencefrom email.headerregistry import Address-from typing import cast+from typing import Annotated, Literal, castfrom django.core import validatorsfrom django.core.exceptions import ValidationErrorfrom django.http import HttpRequest, HttpResponsefrom django.utils.translation import gettext as _+from pydantic import Jsonfrom zerver.actions.message_send import (check_send_message,@@ -17,10 +18,14 @@ from zerver.actions.message_send import ()from zerver.lib.exceptions import JsonableErrorfrom zerver.lib.markdown import render_message_markdown-from zerver.lib.request import REQ, RequestNotes, has_request_variables+from zerver.lib.request import RequestNotesfrom zerver.lib.response import json_success-from zerver.lib.topic import REQ_topic-from zerver.lib.validator import check_bool, check_string_in, to_float+from zerver.lib.typed_endpoint import (+ DOCUMENTATION_PENDING,+ ApiParamConfig,+ OptionalTopic,+ typed_endpoint,+)from zerver.lib.zcommand import process_zcommandsfrom zerver.lib.zephyr import compute_mit_user_fullnamefrom zerver.models import Client, Message, RealmDomain, UserProfile@@ -123,21 +128,30 @@ def same_realm_jabber_user(user_profile: UserProfile, email: str) -> bool:return RealmDomain.objects.filter(realm=user_profile.realm, domain=domain).exists()-@has_request_variables+@typed_endpointdef send_message_backend(request: HttpRequest,user_profile: UserProfile,- req_type: str = REQ("type", str_validator=check_string_in(Message.API_RECIPIENT_TYPES)),- req_to: str | None = REQ("to", default=None),- req_sender: str | None = REQ("sender", default=None, documentation_pending=True),- forged_str: str | None = REQ("forged", default=None, documentation_pending=True),- topic_name: str | None = REQ_topic(),- message_content: str = REQ("content"),- widget_content: str | None = REQ(default=None, documentation_pending=True),- local_id: str | None = REQ(default=None),- queue_id: str | None = REQ(default=None),- time: float | None = REQ(default=None, converter=to_float, documentation_pending=True),- read_by_sender: bool | None = REQ(json_validator=check_bool, default=None),+ *,+ req_type: Annotated[Literal["direct", "private", "stream", "channel"], ApiParamConfig("type")],+ req_to: Annotated[str | None, ApiParamConfig("to")] = None,+ req_sender: Annotated[+ str | None, ApiParamConfig("sender", documentation_status=DOCUMENTATION_PENDING)+ ] = None,+ forged_str: Annotated[+ str | None, ApiParamConfig("forged", documentation_status=DOCUMENTATION_PENDING)+ ] = None,+ topic_name: OptionalTopic = None,+ message_content: Annotated[str, ApiParamConfig("content")],+ widget_content: Annotated[+ str | None, ApiParamConfig("widget_content", documentation_status=DOCUMENTATION_PENDING)+ ] = None,+ local_id: str | None = None,+ queue_id: str | None = None,+ time: Annotated[+ Json[float] | None, ApiParamConfig("time", documentation_status=DOCUMENTATION_PENDING)+ ] = None,+ read_by_sender: Json[bool] | None = None,) -> HttpResponse:recipient_type_name = req_typeif recipient_type_name == "direct":@@ -151,7 +165,7 @@ def send_message_backend(# message (created, schdeduled, drafts) objects/dicts.recipient_type_name = "stream"- # If req_to is None, then we default to an+ # If to is None, then we default to an# empty list of recipients.message_to: Sequence[int] | Sequence[str] = []@@ -258,16 +272,19 @@ def send_message_backend(return json_success(request, data=data)-@has_request_variables+@typed_endpointdef zcommand_backend(- request: HttpRequest, user_profile: UserProfile, command: str = REQ("command")+ request: HttpRequest, user_profile: UserProfile, *, command: str) -> HttpResponse:return json_success(request, data=process_zcommands(command, user_profile))-@has_request_variables+@typed_endpointdef render_message_backend(- request: HttpRequest, user_profile: UserProfile, content: str = REQ()+ request: HttpRequest,+ user_profile: UserProfile,+ *,+ content: str,) -> HttpResponse:message = Message()message.sender = user_profile