Can't encode object graphs with aliased objects or cycles #5
Replies: 3 comments 5 replies
-
My initial thoughts are this shouldn't be documented as part of this framework, or any other framework which makes use of Codable. This is the defined behavior of the Swift standard library. While it's true this behavior is not that of Objective C and |
Beta Was this translation helpful? Give feedback.
-
Looking into this more, can |
Beta Was this translation helpful? Give feedback.
-
I perhaps read too much into the "object" part of "object substitution attacks". Technically I believe what you're describing is protecting against class substitution attacks. Let me know what you find and I'd be happy to update the documentation with a helpful note if in fact this is the case. I just don't want to be providing a note that isn't actually applicable and therefore is more confusing than helpful. |
Beta Was this translation helpful? Give feedback.
-
One thing I've noticed in the passed is that you can't* use Swift's Codable facilities to express aliased objects (multiple references to the same object), or cycles in the object graph.
Basically, by the point you're getting
init(from decoder: Decoder)
called on your type, you're already "locked into" having to make a new instance (it was already made, actually, you're just initializing it). Unlike an Objective C initializer, you can't return an object that isn'tself
.This means that if you want to express one object of class
Foo
being referenced from multiple other objects, you can't just implement that object-aliasing logic in the decoding logic ofFoo
. You need to instead override the encoding logic of every single class that makes references toFoo
.Contrast this with Objective-C's
NSCoding
, where you can callawakeAfterUsingCoder:
to substitute one object for anotherThough this restriction isn't caused by this package, this package inherits it from Swift's Codable system. Should we document it explicitly?
Beta Was this translation helpful? Give feedback.
All reactions