Skip to content

Commit 912539f

Browse files
committed
fix(hook): propagate evaluation context to subsequent before hooks (#628)
Signed-off-by: Ritinpaul <ritin.pal125@gmail.com>
1 parent e81c933 commit 912539f

3 files changed

Lines changed: 309 additions & 14 deletions

File tree

‎openfeature/hook/_hook_support.py‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22
import typing
3-
from functools import reduce
43

54
from openfeature.evaluation_context import EvaluationContext
65
from openfeature.flag_evaluation import FlagEvaluationDetails, FlagType
@@ -59,19 +58,32 @@ def before_hooks(
5958
hooks_and_context: list[tuple[Hook, HookContext]],
6059
hints: HookHints | None = None,
6160
) -> EvaluationContext:
62-
kwargs = {"hints": hints}
63-
executed_hooks = _execute_hooks_unchecked(
64-
flag_type=flag_type,
65-
hooks_and_context=hooks_and_context,
66-
hook_method=HookType.BEFORE,
67-
**kwargs,
68-
)
69-
filtered_hooks = [result for result in executed_hooks if result is not None]
70-
71-
if filtered_hooks:
72-
return reduce(lambda a, b: a.merge(b), filtered_hooks)
73-
74-
return EvaluationContext()
61+
# Requirement 4.3.4: Any evaluation context returned from a before hook MUST be
62+
# passed to subsequent before hooks (via HookContext).
63+
accumulated: EvaluationContext | None = None
64+
65+
for hook, hook_context in hooks_and_context:
66+
if not hook.supports_flag_value_type(flag_type):
67+
continue
68+
69+
if accumulated is not None:
70+
# Propagate the accumulated context into this hook's HookContext so that
71+
# it can observe the evaluation context returned by earlier before hooks.
72+
if isinstance(hook_context.evaluation_context, EvaluationContext):
73+
hook_context.evaluation_context = hook_context.evaluation_context.merge(
74+
accumulated
75+
)
76+
else:
77+
hook_context.evaluation_context = accumulated
78+
79+
result = hook.before(hook_context=hook_context, hints=hints or {})
80+
81+
if isinstance(result, EvaluationContext):
82+
accumulated = (
83+
accumulated.merge(result) if accumulated is not None else result
84+
)
85+
86+
return accumulated if accumulated is not None else EvaluationContext()
7587

7688

7789
def _execute_hooks(

‎tests/hook/test_hook_support.py‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,163 @@ def test_before_hooks_merges_evaluation_contexts():
122122
assert context == EvaluationContext("bar", {"key_1": "val_1", "key_2": "val_2"})
123123

124124

125+
def test_before_hooks_propagates_context_to_subsequent_hook():
126+
"""Requirement 4.3.4 / Issue #628: context returned by a before hook must reach the next hook."""
127+
# Given
128+
initial_context = EvaluationContext(attributes={"initial": "present"})
129+
hook_context_a = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
130+
hook_context_b = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
131+
132+
received_by_hook_b: list[EvaluationContext] = []
133+
134+
hook_a = MagicMock(spec=Hook)
135+
hook_a.before.return_value = EvaluationContext(
136+
attributes={"from_hook_a": "visible"}
137+
)
138+
139+
hook_b = MagicMock(spec=Hook)
140+
141+
def hook_b_before(hook_context, hints):
142+
received_by_hook_b.append(hook_context.evaluation_context)
143+
return None
144+
145+
hook_b.before.side_effect = hook_b_before
146+
147+
# When
148+
before_hooks(
149+
FlagType.BOOLEAN,
150+
[(hook_a, hook_context_a), (hook_b, hook_context_b)],
151+
)
152+
153+
# Then
154+
assert len(received_by_hook_b) == 1
155+
assert received_by_hook_b[0].attributes.get("from_hook_a") == "visible", (
156+
"Hook B did not receive the evaluation context returned by Hook A"
157+
)
158+
assert received_by_hook_b[0].attributes.get("initial") == "present"
159+
160+
161+
def test_before_hooks_accumulates_context_across_three_hooks():
162+
"""Requirement 4.3.4 / Issue #628: Hook C must see context returned by both A and B."""
163+
# Given
164+
initial_context = EvaluationContext(attributes={"initial": "present"})
165+
ctx_a = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
166+
ctx_b = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
167+
ctx_c = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
168+
169+
received_by_hook_b: list[EvaluationContext] = []
170+
received_by_hook_c: list[EvaluationContext] = []
171+
172+
hook_a = MagicMock(spec=Hook)
173+
hook_a.before.return_value = EvaluationContext(attributes={"from_a": "A"})
174+
175+
hook_b = MagicMock(spec=Hook)
176+
177+
def hook_b_before(hook_context, hints):
178+
received_by_hook_b.append(hook_context.evaluation_context)
179+
return EvaluationContext(attributes={"from_b": "B"})
180+
181+
hook_b.before.side_effect = hook_b_before
182+
183+
hook_c = MagicMock(spec=Hook)
184+
185+
def hook_c_before(hook_context, hints):
186+
received_by_hook_c.append(hook_context.evaluation_context)
187+
return None
188+
189+
hook_c.before.side_effect = hook_c_before
190+
191+
# When
192+
before_hooks(
193+
FlagType.BOOLEAN,
194+
[(hook_a, ctx_a), (hook_b, ctx_b), (hook_c, ctx_c)],
195+
)
196+
197+
# Then
198+
assert received_by_hook_b[0].attributes.get("from_a") == "A", (
199+
"Hook B did not receive the evaluation context returned by Hook A"
200+
)
201+
assert received_by_hook_c[0].attributes.get("from_a") == "A", (
202+
"Hook C did not receive the evaluation context returned by Hook A"
203+
)
204+
assert received_by_hook_c[0].attributes.get("from_b") == "B", (
205+
"Hook C did not receive the evaluation context returned by Hook B"
206+
)
207+
208+
209+
def test_before_hooks_later_hook_overrides_earlier_on_conflict():
210+
"""Later hook results take precedence over earlier hook results on conflicting attributes."""
211+
# Given
212+
initial_context = EvaluationContext(attributes={"initial": "present"})
213+
ctx_a = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
214+
ctx_b = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
215+
ctx_c = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
216+
217+
received_by_hook_c: list[EvaluationContext] = []
218+
219+
hook_a = MagicMock(spec=Hook)
220+
hook_a.before.return_value = EvaluationContext(attributes={"shared": "A"})
221+
222+
hook_b = MagicMock(spec=Hook)
223+
hook_b.before.return_value = EvaluationContext(attributes={"shared": "B"})
224+
225+
hook_c = MagicMock(spec=Hook)
226+
227+
def hook_c_before(hook_context, hints):
228+
received_by_hook_c.append(hook_context.evaluation_context)
229+
return None
230+
231+
hook_c.before.side_effect = hook_c_before
232+
233+
# When
234+
before_hooks(
235+
FlagType.BOOLEAN,
236+
[(hook_a, ctx_a), (hook_b, ctx_b), (hook_c, ctx_c)],
237+
)
238+
239+
# Then
240+
assert received_by_hook_c[0].attributes.get("shared") == "B", (
241+
"Later hook (B) result should override earlier hook (A) result for the same attribute"
242+
)
243+
244+
245+
def test_before_hooks_none_result_does_not_corrupt_accumulated_context():
246+
"""A hook returning None must not wipe the accumulated context for subsequent hooks."""
247+
# Given
248+
initial_context = EvaluationContext(attributes={"initial": "present"})
249+
ctx_a = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
250+
ctx_b = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
251+
ctx_c = HookContext("flag_key", FlagType.BOOLEAN, True, initial_context)
252+
253+
received_by_hook_c: list[EvaluationContext] = []
254+
255+
hook_a = MagicMock(spec=Hook)
256+
hook_a.before.return_value = EvaluationContext(attributes={"from_a": "A"})
257+
258+
# Hook B returns None — must not erase Hook A's contribution
259+
hook_b = MagicMock(spec=Hook)
260+
hook_b.before.return_value = None
261+
262+
hook_c = MagicMock(spec=Hook)
263+
264+
def hook_c_before(hook_context, hints):
265+
received_by_hook_c.append(hook_context.evaluation_context)
266+
return None
267+
268+
hook_c.before.side_effect = hook_c_before
269+
270+
# When
271+
before_hooks(
272+
FlagType.BOOLEAN,
273+
[(hook_a, ctx_a), (hook_b, ctx_b), (hook_c, ctx_c)],
274+
)
275+
276+
# Then
277+
assert received_by_hook_c[0].attributes.get("from_a") == "A", (
278+
"Hook C should still see Hook A's context even though Hook B returned None"
279+
)
280+
281+
125282
def test_after_hooks_run_after_method(mock_hook):
126283
# Given
127284
hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, "")
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import pytest
2+
3+
from openfeature.api import get_client, set_provider_and_wait
4+
from openfeature.evaluation_context import EvaluationContext
5+
from openfeature.hook import Hook
6+
from openfeature.provider.no_op_provider import NoOpProvider
7+
8+
9+
class CapturingHook(Hook):
10+
"""A hook that records the evaluation_context it received and returns
11+
a fixed EvaluationContext (or None) from its before() method."""
12+
13+
def __init__(self, return_value=None):
14+
self.received_context: EvaluationContext | None = None
15+
self._return_value = return_value
16+
17+
def before(self, hook_context, hints):
18+
self.received_context = hook_context.evaluation_context
19+
return self._return_value
20+
21+
22+
# ---------------------------------------------------------------------------
23+
# Synchronous integration tests
24+
# ---------------------------------------------------------------------------
25+
26+
27+
def test_sync_second_before_hook_receives_context_from_first():
28+
"""Requirement 4.3.4 / Issue #628: Hook B must see context returned by Hook A."""
29+
# Given
30+
set_provider_and_wait(NoOpProvider())
31+
client = get_client()
32+
33+
hook_a = CapturingHook(
34+
return_value=EvaluationContext(attributes={"from_hook_a": "visible"})
35+
)
36+
hook_b = CapturingHook(return_value=None)
37+
38+
client.add_hooks([hook_a, hook_b])
39+
40+
# When
41+
client.get_boolean_value(flag_key="test-flag", default_value=False)
42+
43+
# Then
44+
assert hook_b.received_context is not None
45+
assert hook_b.received_context.attributes.get("from_hook_a") == "visible", (
46+
"Hook B did not receive the evaluation context returned by Hook A (sync path)"
47+
)
48+
49+
50+
def test_sync_third_before_hook_receives_accumulated_context():
51+
"""Requirement 4.3.4 / Issue #628: Hook C must see context from both A and B."""
52+
# Given
53+
set_provider_and_wait(NoOpProvider())
54+
client = get_client()
55+
56+
hook_a = CapturingHook(return_value=EvaluationContext(attributes={"from_a": "A"}))
57+
hook_b = CapturingHook(return_value=EvaluationContext(attributes={"from_b": "B"}))
58+
hook_c = CapturingHook(return_value=None)
59+
60+
client.add_hooks([hook_a, hook_b, hook_c])
61+
62+
# When
63+
client.get_boolean_value(flag_key="test-flag", default_value=False)
64+
65+
# Then
66+
assert hook_b.received_context.attributes.get("from_a") == "A", (
67+
"Hook B did not receive the evaluation context returned by Hook A (sync path)"
68+
)
69+
assert hook_c.received_context.attributes.get("from_a") == "A", (
70+
"Hook C did not receive Hook A's context (sync path)"
71+
)
72+
assert hook_c.received_context.attributes.get("from_b") == "B", (
73+
"Hook C did not receive Hook B's context (sync path)"
74+
)
75+
76+
77+
@pytest.mark.asyncio
78+
async def test_async_second_before_hook_receives_context_from_first():
79+
"""Requirement 4.3.4 / Issue #628: Hook B must see context returned by Hook A."""
80+
# Given
81+
set_provider_and_wait(NoOpProvider())
82+
client = get_client()
83+
84+
hook_a = CapturingHook(
85+
return_value=EvaluationContext(attributes={"from_hook_a": "visible"})
86+
)
87+
hook_b = CapturingHook(return_value=None)
88+
89+
client.add_hooks([hook_a, hook_b])
90+
91+
# When
92+
await client.get_boolean_value_async(flag_key="test-flag", default_value=False)
93+
94+
# Then
95+
assert hook_b.received_context is not None
96+
assert hook_b.received_context.attributes.get("from_hook_a") == "visible", (
97+
"Hook B did not receive the evaluation context returned by Hook A (async path)"
98+
)
99+
100+
101+
@pytest.mark.asyncio
102+
async def test_async_third_before_hook_receives_accumulated_context():
103+
"""Requirement 4.3.4 / Issue #628: Hook C must see context from both A and B."""
104+
# Given
105+
set_provider_and_wait(NoOpProvider())
106+
client = get_client()
107+
108+
hook_a = CapturingHook(return_value=EvaluationContext(attributes={"from_a": "A"}))
109+
hook_b = CapturingHook(return_value=EvaluationContext(attributes={"from_b": "B"}))
110+
hook_c = CapturingHook(return_value=None)
111+
112+
client.add_hooks([hook_a, hook_b, hook_c])
113+
114+
# When
115+
await client.get_boolean_value_async(flag_key="test-flag", default_value=False)
116+
117+
# Then
118+
assert hook_b.received_context.attributes.get("from_a") == "A", (
119+
"Hook B did not receive the evaluation context returned by Hook A (async path)"
120+
)
121+
assert hook_c.received_context.attributes.get("from_a") == "A", (
122+
"Hook C did not receive Hook A's context (async path)"
123+
)
124+
assert hook_c.received_context.attributes.get("from_b") == "B", (
125+
"Hook C did not receive Hook B's context (async path)"
126+
)

0 commit comments

Comments
 (0)