Recursive AI

Replace a YAML alias without changing its anchor in C#

OneYaml is a free, open-source YAML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Call doc.Set on the key that holds the alias (/web), not on a key inside it (/web/size).

Terminal
dotnet add package OneYaml

The library targets .NET 8 and later; the example project uses .NET 10.

C#
using OneYaml;
 
using var doc = YamlDocument.Parse("""
    base: &pool {size: 2}
    web: *pool
    jobs: *pool
    """);
doc.Set("/web", new { size = 4 });
Console.WriteLine(doc);

Setting /web/size instead of /web on the original document throws a YamlException with category RefusedAlias: it would write through the alias.

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

The snippet prints:

YAML
base: &pool {size: 2}
web:
  size: 4
jobs: *pool

Variations

Change the shared default on purpose. Replace the Set call in the example with this one. Both aliases stay in the YAML and now read the updated size.

C#
doc.Set("/base/size", 6);
YAML
base: &pool {size: 6}
web: *pool
jobs: *pool

The short answer

Call YamlDocument.Set on the path that contains the alias itself. Here it’s /web, not /web/size. The new object is written as a YAML mapping, and the other alias still points to the original mapping. Supply every key that web should have: this is a replacement, not a merge.

How do YAML anchors and aliases share values?

An anchor such as &pool names a value in a YAML document. An alias such as *pool refers to that earlier value; it isn’t a separate editable copy. In the input above, web and jobs refer to the same one-key mapping. OneYaml follows aliases when reading with Get, so reading /web/size before the edit gives 2. But writing /web/size while web is still an alias would have to change the shared mapping. OneYaml refuses that edit; see the anchor and alias editing guide.

That leaves two explicit choices. To change every consumer, edit the value where the anchor is defined, as the variation does. To change only one consumer, replace the alias. After the replacement, web is a mapping in its own right, so subsequent writes to /web/size no longer go through an alias. A later edit to /base/size won’t affect web either.

Can I override just one key in a YAML alias?

Not with a bare alias: web: *pool stands for the entire value. Replacing it with an explicit mapping works even when the YAML reader has no merge-key support, but you must include any settings you still want from the original. That’s useful when you want a complete local configuration that won’t change with the defaults.

If web should still inherit other keys, use a mapping with a << merge and a local key instead, provided the tool that reads the file supports merges. In a reader that supports the YAML merge-key extension, a key written locally wins over one inherited from the anchor. That’s a different behavior: later edits to shared keys that aren’t overridden still affect web.

All oneyaml C# recipes