Roundtrip not possible when using identical array twice
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start by reproducing the YAML.load(YAML.dump({ a: a, b: a })) example and inspect Psych's YAML.load, YAML.dump, and alias-handling paths. The issue names no files or tests and does not define whether dumping or loading should change, so acceptance criteria need to be established before implementation.
Written by the indexing model from the issue text.
Description
tl;dr:
require "yaml"
a = [1,2,3]
YAML.load(YAML.dump({ a: a, b: a }))
# raises 'Psych::Visitors::NoAliasRuby#visit_Psych_Nodes_Alias': Alias parsing was not enabled. To enable it, pass `aliases: true` to `Psych::load` or `Psych::safe_load`. (Psych::AliasesNotEnabled)
Long form
Hey there, today I noticed an error that surprised me quite a lot. Essentially as soon as you include the same array twice in a structure that you dump through Psych, it will not be loadable using a default YAML.load. You'd have to pass specific options to make it loadable.
The problem is that dump will generate YAML in the form:
---
:a: &1
- 1
- 2
- 3
:b: *1
This preserves the array identity for the hashes stored under a and b.
That's surprising to me. I'd have expected that if load disables certain YAML features by default, that dump would also only use those on an opt-in basis. Even worse: I don't see a setting documented that would allow me to disable the alias creation. The two fixes available to me right now:
- enable alias deserialization (which I believed to be disabled by default for security considerations; though I fail to find a source for that now)
- duplicate values in my
dumpinput to avoid alias generation
Second solution shown in code:
YAML.load(YAML.dump({ a: a, b: a.dup }))
=> {a: [1, 2, 3], b: [1, 2, 3]}
- Dominant language
- Ruby
- Stars
- 597
- Forks
- 223
- Avg merge
- 11h 23m
- Merged PRs (30d)
- 3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ruby/psych
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Documentation
Difficulty 1/5 Under an hour Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
-
Difficulty 4/5 3-5 days Newbie friendliness 68/100
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
palladius/rails8-app-on-gcp#145 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
rubocop/rubocop-rspec#2236 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
riscv/riscv-unified-db#2624 · 1 reaction ·
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100