Adds support for closure inside context with enum property #47
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #43
Description
This PR fixes a bug when trying to serialize a closure that was defined inside a class where any class in its context has a property containing an enum. Since that's quite a mouthful, here's an example.
Given these enum and class declarations:
A couple important things to point out:
MyClass
has a property with an enum as its value. It's not actually important that this field exists onMyClass
specifically. As long as some class somewhere in the closure's context has a property with an enum as its value, the bug will still occur. So ifMyClass
had a property of typeMyOtherClass
andMyOtherClass
had a property of typeMyEnum
, the same thing would happen.$this
. This is important (maybe pun intended) because its what's triggering the call toSerializers\Native::wrapClosures
.$enum
field actually has a value. If this was a nullable field and it happened to benull
the error would not occur.When this closure gets serialized, the serializer recursively walks through every property of
MyClass
(and all of its parent classes, if applicable) and and callswrapClosures
on each of them. Once it hits the property containing the enum, it crashes with aCannot instantiate enum MyEnum
error. This is because it's trying to callReflectionClass::newInstanceWithoutConstructor
on the enum which doesn't work.The fix
Since enums serialize just fine on their own, I fixed this by adding an additional base case to the respective
elseif
branch of thewrapClosures
method to exclude instances ofUnitEnum
. Theis_object
call on its own is not specific enough since that returnstrue
forUnitEnum
as well.I have added the simplest test case I could come up with to reproduce the error. I have also tested this patch on one of my current projects where I encountered this bug and everything seems to work.