Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yaml merge ignoring properties defined in anchor

Consider this yaml

- node_1:
    properties: &node_1_prop
      role: management
      layer: 1

- node_2:
    properties:
      level:  24
      <<: *node_1_prop

I am trying to create node graph using snakeyaml library and I am expecting two properties for node_1 and three for node_2 like this.

Path yamlPath = Paths.get( "nodes.yaml");
InputStream yamlStream = Files.newInputStream(yamlPath);
StreamReader sreader = new StreamReader(new UnicodeReader(yamlStream));
Composer composer = new Composer(new ParserImpl(sreader), new Resolver());
Node rootNode = composer.getSingleNode();

The output node graph by snakeyaml is showing << as property for node_2.

Code example showing the result on Git.

Edit:

Nodes gets constructed fine if I define the yaml as below:

- node_1:
    properties: &node_1_prop
      role: management
      layer: 1

- node_2:
      <<: *node_1_prop

However my requirement is not just copy the properties as it is but to have additional properties.

like image 499
Ubercool Avatar asked Oct 19 '25 14:10

Ubercool


1 Answers

You do not load your YAML completely, you only compose it (see graph in the YAML 1.1 spec which SnakeYaml implements).

The compose step resolves aliases, but keeps the tags – tags are resolved during construction. The merge key is defined as a tag and thus, does not get processed when you compose the YAML input.

like image 143
flyx Avatar answered Oct 22 '25 03:10

flyx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!