MSC3873: Escape keys when flattening dicts. (#15004)

This disambiguates keys which attempt to match fields
with a dot in them (e.g. m.relates_to).

Disabled by default behind an experimental configuration flag.
This commit is contained in:
Patrick Cloke 2023-02-08 13:09:41 -05:00 committed by GitHub
parent a4126e2861
commit c951fbedcb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 4 deletions

View file

@ -48,6 +48,14 @@ class FlattenDictTestCase(unittest.TestCase):
input = {"foo": {"bar": "abc"}}
self.assertEqual({"foo.bar": "abc"}, _flatten_dict(input))
# If a field has a dot in it, escape it.
input = {"m.foo": {"b\\ar": "abc"}}
self.assertEqual({"m.foo.b\\ar": "abc"}, _flatten_dict(input))
self.assertEqual(
{"m\\.foo.b\\\\ar": "abc"},
_flatten_dict(input, msc3783_escape_event_match_key=True),
)
def test_non_string(self) -> None:
"""Non-string items are dropped."""
input: Dict[str, Any] = {