Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/local-eval-regex-json-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
posthog: patch
---

Stringify non-string properties as compact JSON for local `regex` and `not_regex` matching, so booleans, lists, and objects match the same way they do on the flags service.
4 changes: 3 additions & 1 deletion posthog/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,9 @@ def compute_exact_match(value, override_value):
return matched

if operator in ("regex", "not_regex"):
property_string = "null" if override_value is None else str(override_value)
property_string = _value_to_string(
override_value, UnresolvedFlagReason.MISSING_CONTEXT
)
regex_match = _match_regex(str(value), property_string)
if regex_match is None:
return False
Expand Down
19 changes: 19 additions & 0 deletions posthog/test/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -5871,6 +5871,25 @@ def test_match_properties_regex(self):

self.assertFalse(match_property(property_d, {"key": "value"}))

@parameterized.expand(
[
("list", r"\[1,2\]", [1, 2], True),
("bool", "^true$", True, True),
("python_bool_spelling", "True", True, False),
("object", r'^\{"a":1,"b":\[null\]\}$', {"b": [None], "a": 1}, True),
("python_object_spelling", "'a'", {"a": 1}, False),
]
)
def test_regex_stringifies_non_string_properties_as_compact_json(
self, _name, pattern, property_value, expected
):
regex = self.property("key", pattern, "regex")
not_regex = self.property("key", pattern, "not_regex")
self.assertEqual(expected, match_property(regex, {"key": property_value}))
self.assertEqual(
not expected, match_property(not_regex, {"key": property_value})
)

def test_match_properties_math_operators(self):
property_a = self.property(key="key", value=1, operator="gt")
self.assertTrue(match_property(property_a, {"key": 2}))
Expand Down