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

Are aliases supposed to be equal by value or by reference? #660

Closed
0x0ade opened this issue Jan 1, 2022 · 1 comment
Closed

Are aliases supposed to be equal by value or by reference? #660

0x0ade opened this issue Jan 1, 2022 · 1 comment

Comments

@0x0ade
Copy link

0x0ade commented Jan 1, 2022

Apologies in advance if this question is misplaced here.

@WEGFan found a possible problem in how we're using YamlDotNet in a project of ours, and it's unclear if it's a problem to begin with, or if I'm misunderstanding the docs.

According to the documentation:

When the same object appears multiple times in the serialized graph, the default behaviour is to emit it only once, assigning it an anchor, and use aliases to reference it on its subsequent occurrences. DisableAliases prevents this and causes the object to be emitted multiple times.

What does "same object" mean? Going by the current implementation, it's "equal by value", but judging by how anchors and aliases are supposed to help with circular references (at least from what I understand), I'd rather take it to mean "equal by reference". This is causing a bit of a headache for us right now as objects are being "merged" when round-tripping, most easily seen with records. Should two equal records exist which do not refer to the same object, both now do after deserialization. And we'd want to disable that merging behavior without risking reused references causing problems.

@WEGFan found that the AnchorAssigner is using a dictionary under the hood. Should it be using an equality comparer that uses ReferenceEquals? Or is the default behavior checking Equals and GetHashCode intended for aliases?

@EdwardCooke
Copy link
Collaborator

After digging in the code it doesn't do anything special to the objects it uses as a key in the underlying dictionary. So, with that, it would use what the dictionary uses which I believe is Equals and GetHashCode to compare the objects and not by reference.
Hopefully that helps.

If you need more help on the internals of YamlDotNet, I'm here to help, reopen the issue with any additional comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants