Skip to content

Commit 298e2a3

Browse files
authored
fix: Prevent a null prerequisites list from stopping the client from initializing (#587)
<!-- TERMAG_PR_BODY_BEGIN --> When flag data sets `prerequisites` to null, the SDK cannot write the data set to its store. The store puts each prerequisite flag before the flags that depend on it, and that ordering iterated over the null value and raised a TypeError. The sync client then restarted the stream again and again and never initialized. The async client fails the same way when it writes to a persistent store. A null prerequisites list now has the same effect as an omitted list, which is how the flag model already reads it. Testing: added a unit test for store ordering with a null prerequisites list; the explicit-null data-model contract tests now pass, and the full v2 and v3 contract suites are unchanged for the sync and async clients. SDK-3667 <!-- TERMAG_PR_BODY_END --> <!-- termag: box=i-099691946751009d8 project=appease-ai-feverish-2 --> <sub>Opened from termag</sub> <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Overview** > Fixes client initialization when flag payloads include **`prerequisites: null`**. Feature store ordering used `flag.get('prerequisites', [])`, which still returns `None` when the field is present but null, so dependency traversal raised a **TypeError** and blocked store writes (sync stream restart loop / async persistent store failures). > > **`FEATURES.get_dependency_keys`** now treats a null prerequisites list like a missing list via `(flag.get('prerequisites') or [])`. > > Adds **`test_store_data_set_ordering_treats_null_prerequisites_as_empty`** to assert flags still land in dependency order (`b` before `a`) when a prerequisite flag has null prerequisites. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 8b76d12. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 85e948c commit 298e2a3

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎ldclient/testing/test_ldclient.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,29 @@ def test_store_data_set_ordering():
175175
if prereq_index > item_index:
176176
all_keys = (f["key"] for f in flags_list)
177177
raise Exception("%s depends on %s, but %s was listed first; keys in order are [%s]" % (item["key"], prereq["key"], item["key"], ", ".join(all_keys)))
178+
179+
180+
def test_store_data_set_ordering_treats_null_prerequisites_as_empty():
181+
data = {
182+
FEATURES: {
183+
"a": {"key": "a", "prerequisites": [{"key": "b"}]},
184+
"b": {"key": "b", "prerequisites": None},
185+
},
186+
SEGMENTS: {},
187+
}
188+
189+
class NullPrerequisitesDataUpdateProcessor(DependencyOrderingDataUpdateProcessor):
190+
def __init__(self, config, store, ready):
191+
store.init(data)
192+
ready.set()
193+
194+
store = CapturingFeatureStore()
195+
config = Config(sdk_key='SDK_KEY', send_events=False, feature_store=store, update_processor_class=NullPrerequisitesDataUpdateProcessor)
196+
197+
# Initialize the store with a flag whose prerequisites are null.
198+
with LDClient(config=config):
199+
pass
200+
201+
# The store receives every flag, and the prerequisite comes first.
202+
assert store.received_data is not None
203+
assert list(store.received_data[FEATURES].keys()) == ["b", "a"]

‎ldclient/versioned_data_kind.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def get_dependency_keys(self) -> Optional[Callable[[dict], Iterable[str]]]:
6868
stream_api_path="/flags/",
6969
decoder=FeatureFlag,
7070
priority=1,
71-
get_dependency_keys=lambda flag: (p.get('key') for p in flag.get('prerequisites', [])),
71+
# A null prerequisites list is the same as an omitted list.
72+
get_dependency_keys=lambda flag: (p.get('key') for p in (flag.get('prerequisites') or [])),
7273
)
7374

7475
SEGMENTS = VersionedDataKindWithOrdering(namespace="segments", request_api_path="/sdk/latest-segments", stream_api_path="/segments/", decoder=Segment, priority=0, get_dependency_keys=None)

0 commit comments

Comments
 (0)