Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty, unreferenced arrays are not being serialized #98

Open
heaths opened this issue Feb 27, 2025 · 3 comments
Open

Empty, unreferenced arrays are not being serialized #98

heaths opened this issue Feb 27, 2025 · 3 comments

Comments

@heaths
Copy link

heaths commented Feb 27, 2025

When serializing a dictionary, any keys that are empty arrays but unreferenced are not being serialized. Consider the following TOML:

[features]
default = ["foo"]
foo = []
bar = []

A fairly common Cargo.toml manifest with default and non-default features. Deserializing this produces the expected ordered dictionary with array members for all three of default, foo, and bar. Serializing the resulting object only produces:

[features]
default = ["foo"]
foo = []

Notice that bar is missing.

This is causing jborean93/PSToml#10, which we currently use in the Azure SDK for Rust pipelines. If there is a way to work around this for now, that would be great; however, looking through your documentation nothing pops out at me.

@heaths
Copy link
Author

heaths commented Feb 27, 2025

With some additional testing of tomlyn directly @hallipr found it's actually just dropping the last entry:

var model = new TomlTable();
model.Add("something", "string value");
model.Add("array1", new TomlArray() { "hello" });
model.Add("array2", new TomlArray() { });
model.Add("array3", new TomlArray() { });
Console.WriteLine(Toml.FromModel(model));
something = "string value"
array1 = ["hello"]
array2 = []

There's no key named "string value" and both array2 and array3 are empty, but only array2 is serialized.

@hallipr
Copy link

hallipr commented Feb 27, 2025

An even more detailed repro:

var outer = new TomlTable
{
	["inner1"] = new TomlTable {},
	["inner2"] = new TomlTable {
		{ "array1", new TomlArray {} },
		{ "array2", new TomlArray {} }
	},
	["inner3"] = new TomlTable {
		{ "array1", new TomlArray {} },
		{ "array2", new TomlArray {} },
		{ "array3", new TomlArray { "hello" } },
		{ "array4", new TomlArray { } },
		{ "array5", new TomlArray { } },
		{ "array6", new TomlArray { } }
	},
	["inner4"] = new TomlTable {
		{ "array1", new TomlArray {} },
		{ "string", "value" },
		{ "array2", new TomlArray {} },
		{ "array3", new TomlArray {} },
	},
};

Console.WriteLine("---");
Console.WriteLine(Toml.FromModel(outer));
Console.WriteLine("---");
---
[inner1]
[inner3]
array1 = []
array2 = []
array3 = ["hello"]
array4 = []
array5 = []
[inner4]
string = "value"

---
  • empty tables are supported and serialize correctly.
  • a table containing only empty arrays is not serialized
  • a filled array sibling causes empty arrays to serialize except for the last empty array after the filled array
  • a string sibling does not cause empty arrays to serialize

@heaths
Copy link
Author

heaths commented Feb 27, 2025

To note, my repro was with versoin 0.17.0, which PSToml uses. @hallipr's repro was with 0.18.0, the latest version on nuget.org.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants