From f1446adaa201e420f8b6b335511c328de0b0bbdf Mon Sep 17 00:00:00 2001 From: Jye Cusch Date: Mon, 15 Apr 2024 15:51:43 +1000 Subject: [PATCH] fix: parse topic events without struct fields (#137) Using to_dict on the struct includes internal fields from the protobuf struct, instead of a standard python dictionary --- nitric/context.py | 3 ++- nitric/resources/topics.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/nitric/context.py b/nitric/context.py index 7e5f382..e696998 100644 --- a/nitric/context.py +++ b/nitric/context.py @@ -31,6 +31,7 @@ from nitric.proto.topics.v1 import ClientMessage as TopicClientMessage from nitric.proto.topics.v1 import MessageResponse as TopicResponse from nitric.proto.topics.v1 import ServerMessage as TopicServerMessage +from nitric.utils import dict_from_struct Record = Dict[str, Union[str, List[str]]] PROPAGATOR = propagate.get_global_textmap() @@ -173,7 +174,7 @@ def _from_request(msg: TopicServerMessage) -> MessageContext: """Construct a new EventContext from a Topic trigger from the Nitric Membrane.""" return MessageContext( request=MessageRequest( - data=msg.message_request.message.struct_payload.to_dict(), + data=dict_from_struct(msg.message_request.message.struct_payload), topic=msg.message_request.topic_name, ) ) diff --git a/nitric/resources/topics.py b/nitric/resources/topics.py index 811ee7d..c430244 100644 --- a/nitric/resources/topics.py +++ b/nitric/resources/topics.py @@ -37,7 +37,7 @@ from nitric.proto.topics.v1 import RegistrationRequest, SubscriberStub from nitric.proto.topics.v1 import TopicPublishRequest, TopicsStub from nitric.resources.resource import SecureResource -from nitric.utils import new_default_channel, struct_from_dict +from nitric.utils import dict_from_struct, new_default_channel, struct_from_dict TopicPermission = Literal["publish"] @@ -131,7 +131,7 @@ def decorator(func: EventHandler) -> None: def _message_context_from_proto(msg: ProtoMessageRequest) -> MessageContext: return MessageContext( request=MessageRequest( - data=msg.message.struct_payload.to_dict(), + data=dict_from_struct(msg.message.struct_payload), topic=msg.topic_name, ) )