psych icon indicating copy to clipboard operation
psych copied to clipboard

Don't let users attempt to alias/merge arrays

Open brahn opened this issue 8 years ago • 0 comments

I encountered the confusing behavior described below because I didn't realize that one couldn't merge arrays, only maps. Perhaps we should instead make the parser more strict and throw an error rather than producing the unexpected object structure?

The confusing behavior:

YAML.load("
# Bad! can't merge alias/arrays
foo: &foo  
  - a: 1
bar: 
  <<: *foo
")

=> {"foo"=>[{"a"=>1}], "bar"=>{"<<"=>[{"a"=>1}]}}

Note the appearance of the odd <<. I was expecting expected

   {"foo"=>[{"a"=>1}], "bar"=>[{"a"=>1}]}

As discussed here, my error is that the problem is that &foo is an attempted alias for an array. It would have been ideal if the parser had indicated my error instead.

In case relevant:

ENV['RBENV_VERSION']
=> "2.4"
YAML.libyaml_version
=> [0, 1, 4]

(Also, not entirely sure if this request belongs here or at https://github.com/yaml/libyaml , please feel free to redirect me!)

brahn avatar Jan 12 '18 16:01 brahn