From f14e9c78b452b103efb1966262f496cc765c0283 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Thu, 27 Apr 2023 18:09:25 +0800 Subject: [PATCH] fix(#283): Wrong result stringifying subtables and nested arrays of tables, in arrays of tables Close #283 Signed-off-by: Frost Ming --- tests/test_write.py | 11 +++++++++++ tomlkit/container.py | 23 ++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/tests/test_write.py b/tests/test_write.py index e6bb83c1..eafd1ff8 100644 --- a/tests/test_write.py +++ b/tests/test_write.py @@ -24,3 +24,14 @@ def test_write_inline_table_in_nested_arrays(): expected = "foo = [[{a = 1}]]\n" assert expected == dumps(d) assert loads(dumps(d))["foo"] == [[{"a": 1}]] + + +def test_serialize_aot_with_nested_tables(): + doc = {"a": [{"b": {"c": 1}}]} + expected = """\ +[[a]] +[a.b] +c = 1 +""" + assert dumps(doc) == expected + assert loads(expected) == doc diff --git a/tomlkit/container.py b/tomlkit/container.py index 502f69a3..66448754 100644 --- a/tomlkit/container.py +++ b/tomlkit/container.py @@ -549,21 +549,18 @@ def _render_aot(self, key, aot, prefix=None): def _render_aot_table(self, table: Table, prefix: str | None = None) -> str: cur = "" - _key = prefix or "" + open_, close = "[[", "]]" - if not table.is_super_table(): - open_, close = "[[", "]]" - - cur += ( - f"{table.trivia.indent}" - f"{open_}" - f"{decode(_key)}" - f"{close}" - f"{table.trivia.comment_ws}" - f"{decode(table.trivia.comment)}" - f"{table.trivia.trail}" - ) + cur += ( + f"{table.trivia.indent}" + f"{open_}" + f"{decode(_key)}" + f"{close}" + f"{table.trivia.comment_ws}" + f"{decode(table.trivia.comment)}" + f"{table.trivia.trail}" + ) for k, v in table.value.body: if isinstance(v, Table):