From 5fc8335f9711b914401dce04caf0145ed47e1ee9 Mon Sep 17 00:00:00 2001 From: Ganapathi Diddi Date: Wed, 12 Mar 2025 20:12:50 +0530 Subject: [PATCH] Fix Middleware Chain to Allow Awaiting Final Logic Result --- .../botbuilder/core/turn_context.py | 23 ++++++++----------- .../tests/test_turn_context.py | 2 +- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/libraries/botbuilder-core/botbuilder/core/turn_context.py b/libraries/botbuilder-core/botbuilder/core/turn_context.py index 852fd1f31..36215a84e 100644 --- a/libraries/botbuilder-core/botbuilder/core/turn_context.py +++ b/libraries/botbuilder-core/botbuilder/core/turn_context.py @@ -288,20 +288,15 @@ async def _emit(self, plugins, arg, logic): async def emit_next(i: int): context = self - try: - if i < len(handlers): - - async def next_handler(): - await emit_next(i + 1) - - await handlers[i](context, arg, next_handler) - - except Exception as error: - raise error - - await emit_next(0) - # logic does not use parentheses because it's a coroutine - return await logic + if i < len(handlers): + try: + return await handlers[i](context, arg, lambda: emit_next(i + 1)) + except Exception as error: + raise error + else: + return await logic + + return await emit_next(0) async def send_trace_activity( self, name: str, value: object = None, value_type: str = None, label: str = None diff --git a/libraries/botbuilder-core/tests/test_turn_context.py b/libraries/botbuilder-core/tests/test_turn_context.py index 473580ef0..cf622a352 100644 --- a/libraries/botbuilder-core/tests/test_turn_context.py +++ b/libraries/botbuilder-core/tests/test_turn_context.py @@ -241,7 +241,7 @@ async def update_handler(context, activity, next_handler_coroutine): assert context is not None assert activity.id == activity_id assert activity.conversation.id == ACTIVITY.conversation.id - await next_handler_coroutine() + return await next_handler_coroutine() context.on_update_activity(update_handler) new_activity = MessageFactory.text("test text")