symfonycakephpyaml

Is it possible to use alias for keys in yaml with symfony/yaml?


I've been trying to read the following yaml file using symfony/yaml(v4.4.0) with cakephp3.
But, I get the following error.

Reference "aaa" does not exist in "path to yml" at line xx (near "*aaa:").
Symfony\Component\Yaml\Exception\ParseException

I would like to user the 'aaa' as a key later.
It doesn't work with "*aaa:" and works with "1:".
Basically, is it possible to use alias for keys in yaml file?

Here's the yaml file.

aaa: &aaa 1
bbb: &bbb 2
ccc: &ccc 3

*aaa: # <- this doesn't work and works with '1:'
  - *bbb
  - *ccc

Solution

  • For general spec-conforming YAML parsers

    You need to write it with a space before :.

    aaa: &aaa 1
    bbb: &bbb 2
    ccc: &ccc 3
    
    *aaa :
      - *bbb
      - *ccc
    

    YAML 1.2 allows : to be part of an anchor and therefore, the line will not be parsed as implicit key if : is written adjacent to the alias (since it becomes part of the alias then). This has been discussed on the YAML core mailing list.

    For Symfony

    It seems Symfony parses *aaa: as alias *aaa with : as value indicator. While this is a spec violation, it shouldn't bother us since according to the mailing list, this is more like an oversight in the spec. However, Symfony fails to resolve the alias here, there's nothing much you can do about it but to file an issue for it.