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

[YAML] Error parsing file when custom tag references itself #29462

Closed
eugene-manuilov opened this issue Dec 5, 2018 · 2 comments
Closed

[YAML] Error parsing file when custom tag references itself #29462

eugene-manuilov opened this issue Dec 5, 2018 · 2 comments

Comments

@eugene-manuilov
Copy link

Symfony version(s) affected: symfony/yaml@^4.2

Description

I have received the following error when I try to parse YAML document generated by YARD documentation tool. The custom tag (!ruby/object:YARDJS::CodeObjects::PropertyObject) in the YAML file has a reference to itself, but parser hasn't added it to the references registry yet thus throws the error.

PHP Fatal error:  Uncaught Symfony\Component\Yaml\Exception\ParseException: Reference "1" does not exist at line 4 (near "object: *1"). in vendor/symfony/yaml/Parser.php:642
Stack trace:
#0 vendor/symfony/yaml/Parser.php(337): Symfony\Component\Yaml\Parser->parseValue('1', 512, 'mapping')
#1 vendor/symfony/yaml/Parser.php(454): Symfony\Component\Yaml\Parser->doParse('Constructs a se...', 512)
#2 vendor/symfony/yaml/Parser.php(315): Symfony\Component\Yaml\Parser->parseBlock(18, 'str: |-\n  Const...', 512)
#3 vendor/symfony/yaml/Parser.php(454): Symfony\Component\Yaml\Parser->doParse(Array, 512)
#4 vendor/symfony/yaml/Parser.php(185): Symfony\Component\Yaml\Parser->parseBlock(10, 'files: []\ncurre...', 512)
#5 vendor/symfony/yaml/Parser.php(454): Symfony\Component\Yaml\Parser->doParse('- &1 !ruby/obje...', 512)
#6 vendor/symfony/yaml/Parser in vendor/symfony/yaml/Parser.php on line 642

How to reproduce

Just run:

use Symfony\Component\Yaml\Yaml;

$content = "
children: !ruby/array:YARD::CodeObjects::CodeObjectList
  internal:
  - &235 !ruby/object:YARD::CodeObjects::ModuleObject
    children: !ruby/array:YARD::CodeObjects::CodeObjectList
      internal:
      - &2 !ruby/object:YARD::CodeObjects::ClassObject
        children: !ruby/array:YARD::CodeObjects::CodeObjectList
          internal:
          - &1 !ruby/object:YARDJS::CodeObjects::PropertyObject
            files: []
            current_file_has_comments: false
            name: :constructor
            source_type: :ruby
            visibility: :public
            tags: []
            docstrings: {}
            docstring: !ruby/string:YARD::Docstring
              str: |-
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibu
                lum sit amet pulvinar odio, vitae convallis dolor.
              object: *1
              summary:
              hash_flag: false
";

$yaml = Yaml::parse( $content, Yaml::PARSE_CUSTOM_TAGS );
@eugene-manuilov
Copy link
Author

Need to correct myself. The PropertyObject tag has a reference to the Docstring tag, that has a reference to the "parent" object. It's a kind of a circular reference.

So when the parser tries to parse docstring object it sees a link to *1 object that hasn't been added to the refs array yet. I think a solution might be to add an empty tag to the refs array when the parser encounters &{xxx} and then update its value when it finishes parsing it.

@xabbuh
Copy link
Member

xabbuh commented Dec 18, 2018

see #29639

nicolas-grekas added a commit that referenced this issue Dec 19, 2018
This PR was merged into the 3.4 branch.

Discussion
----------

[Yaml] detect circular references

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29462
| License       | MIT
| Doc PR        |

Commits
-------

b7487f4 [Yaml] detect circular references
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants