Yaml merge ignoring properties defined in anchor

Ubercool :

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.

flyx :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=95107&siteId=1