Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is YAML.safe_load failing on a YAML alias?

I've a locale file in my Rails application that works fine with Rails, but when I tried to use it with react_on_rails rake task (rake react_on_rails:locale) I'm getting this error:

Psych::BadAlias: Unknown alias: item_attributes

I found that the rake task is basically calling YAML.safe_load, so I prepared the simplest example without Rails and the error is still there. Here's the sample Ruby script:

require 'yaml'

YAML.safe_load(File.open('test.yml'))

And here's the test.yml file (shorter version of the actual locale file):

pl:
  language: Polski
  dictionary_name: simple
  activerecord:
    attributes:
      line_item: &item_attributes
        variant: Produkt
        quantity: Ilosc
        price: Cena Netto
        total_price: Wartosc Netto
        vat_rate: VAT
        total_vat_amount: Kwota VAT
        total_gross_price: Wartosc Brutto
      order_item:
        <<: *item_attributes

I'm still getting the error:

/usr/local/Cellar/ruby/2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:402:in `visit_Psych_Nodes_Alias': Unknown alias: item_attributes (Psych::BadAlias)

Any ideas why this works fine with Rails but fails here? Any other way to avoid duplication in the YAML file and make it work for both Rails and YAML.safe_load?

like image 309
Matt Avatar asked Mar 24 '17 13:03

Matt


1 Answers

I found an answer in the Psych documentation, at https://ruby-doc.org/stdlib-2.1.0/libdoc/psych/rdoc/Psych.html.

Aliases can be explicitly allowed by changing the aliases parameter. For example:

x = []
x << x
yaml = Psych.dump x
Psych.safe_load yaml               # => raises an exception
Psych.safe_load yaml, [], [], true # => loads the aliases

Whether to allow aliases or not is a boolean passed to safe_load as the fourth argument.

like image 159
ipd Avatar answered Oct 22 '22 20:10

ipd