taplo icon indicating copy to clipboard operation
taplo copied to clipboard

Arrays of tables are allowed to extend static arrays when nested inside another array of tables

Open benblank opened this issue 2 years ago • 1 comments

When parsing, taplo correctly forbids extending a static array using an array of tables, but only if those arrays are not themselves inside an array of tables.

By my read of the TOML spec, being inside an array of tables as well shouldn't change whether this is valid. The other two TOML parsers I've run this through consider it invalid regardless of context; tomllib in Python's standard library produces the error "Cannot mutate immutable namespace" and https://www.toml-lint.com/ the error "Can't extend an inline array".

In the example below, I've created three invalid TOML files. All three of them contain a static array and an array of tables, both named fruits, but in different contexts:

  • invalid1.toml has them in the root table. This is recognized as invalid by taplo.
  • invalid2.toml has them nested inside a table named food. This is recognized as invalid by taplo.
  • invalid3.toml has them nested inside an array of tables named food. This is considered valid by taplo, with the tables appended to the static array.
The result of running those three files through taplo get -o json
$ cat invalid1.toml 
fruits = ["one", "two"]

[[fruits]]
three = "four"
$ npx @taplo/cli get -o json < invalid1.toml 
error: expected array of tables
  ┌─ -:1:1
  │
1 │ fruits = ["one", "two"]
  │ ^^^^^^ expected array of tables
2 │ 
3 │ [[fruits]]
  │   ------ required by this key

ERROR operation failed error=semantic errors found
$ cat invalid2.toml 
[food]
fruits = ["one", "two"]

[[food.fruits]]
three = "four"
$ npx @taplo/cli get -o json < invalid2.toml 
error: expected array of tables
  ┌─ -:2:1
  │
2 │ fruits = ["one", "two"]
  │ ^^^^^^ expected array of tables
3 │ 
4 │ [[food.fruits]]
  │        ------ required by this key

ERROR operation failed error=semantic errors found
$ cat invalid3.toml 
[[food]]
fruits = ["one", "two"]

[[food.fruits]]
three = "four"
$ npx @taplo/cli get -o json < invalid3.toml 
{
  "food": [
    {
      "fruits": [
        "one",
        "two",
        {
          "three": "four"
        }
      ]
    }
  ]
}

benblank avatar Oct 26 '23 00:10 benblank

Thanks for the bug report! I confirm the issue. I don't have time to fix it though, so PRs are welcome.

ia0 avatar Oct 26 '23 09:10 ia0