-
Notifications
You must be signed in to change notification settings - Fork 8
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
Handle null data when retrieving value #33
base: main
Are you sure you want to change the base?
Handle null data when retrieving value #33
Conversation
@@ -53,7 +56,7 @@ public function export($value) | |||
]; | |||
foreach ($rc->getProperties() as $prop) { | |||
$prop->setAccessible(true); | |||
$v = $prop->isInitialized($value) ? $prop->getValue($value) : null; | |||
$v = $prop->isInitialized($value) ? $prop->getValue($value) : self::UNINITIALIZED; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied the behaviour from the Php81DeSerializationTest
class. I think it's better to be more explicit about the returned value so the tests are self-documenting
PR checks are not passing |
@khepin fixed! |
So, I'm not sure this is the correct approach (leaving it uninitialized) when we receive null. |
@khepin I hear what you're saying. In principal I agree that receiving Right now, we throw a typeError in those incompatible data scenarios, and I think we should consider throwing errors when non-nullable field has a But for this current PR, I did some more testing and I realized that this error only gets thrown in a more specific scenario than I originally thought. I updated the PR description but copying the change here:
So the library currently behaves this way for scalar type fields because it doesn't have to call Barring any other changes, I think it makes sense to handle |
[Issue]
During deserialization, in some cases the following error gets thrown:
This is because
retrieveValue
accepts?array
butarray_key_exists
does not.Scenarios where I've seen this occur:
The model field is defined as non-nullable AND it requires internal deserialization itself but the data received for that field is `null
string $foo
and we receive"foo": null
then it will stay uninitialized currentlyCategory $foo
andCategory
has#[Json]
annotated fields then this error will get thrownIn the case of nested property paths, if any of the containing fields are null
data:null
for the below:[Fix]
$pathBits
to the field insideretrieveValue
null
.[Tests]
Added tests to demonstrate the fix.
Prior to the fix: