-
Notifications
You must be signed in to change notification settings - Fork 86
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
null relation and NullResource return empty array instead of null #82
Comments
Strange, this does not sound like expected behavior, I'll look into it. |
I can't seem to replicate. Nullable has-one/belongs-to relations output |
I guess it's problem somewhere in my code then, I'll dig deeper and let you know if I find why this is happening |
Ok, I came back on the problematic code and found out some more info. The cause seems to be the fact that I provide an array to This have two effects:
How it is with the bug:
How it should be:
|
I pinpointed the problem with the empty-array-instead-of-null problem (which can be related to the second strange behaviour I reported right above). It's because for |
I see. I'm actually not sure why the |
I tryed to modify it to return null and an error bubbled up from fractal package. IF I remember well about Fractal removing null fields, and I cannot assure about it |
I figured it out. |
I'm not understanding what "including side-loads" actually means... I see that the In Fractal serializers, only the JsonApiSerializer overrides that method to return true. |
The sideload including is an additional step used for adding more data to the final output. For instance, the |
Ok, here is what I found about this issue and my hypotesis:
Given this, I changed |
An ever better idea would be to return null if the relation refers to a sigle entity and an empty array when the relation refers to a collection of entities, but we currently have no way to know it, right? |
Ok, the fix proposed won't work alone.
This can be resolved removing the return type or, when if using PHP7.1 or higher, using the nullable return type I think it's better to start writing a test suite with Feature tests, but I'll leave to you to set the format, given that you said you were already going to do that.
I noticed that, given that Laravel always returns a Collection for xxx-many relationships but it returns null on missing not-xxx-many relationships, this behaviour already take place with the updates I did. |
I believe I used the sideload includes in a previous version of the serializer, and then found a way to simplify the code for v2, but forgot to remove the methods. I believe these do absolutely nothing right now.
The reason
It should be pretty much the same for
Yes, as you pointed out, this is already the case.
Indeed, this is something I was looking briefly at when working on supporting primitive resources. I agree this is an unwanted limitation. If we change the contracts I think we should push a new major version, though, as people might rely on these.
Yeah, I'll get on this soon :) |
I'm not sure about this.
This is the definition of versions following the Semantic Versioning. Anyway, shall I make a PR? Then we can just leave it there until you prepare the tests |
This should be fixed with #92 |
Thanks. i also got this error. Thought it would have been fixed by now. @flugg is there a better solution for this? thanks
|
In pure Fractal, when you return a null from an include the field is not added on the output array.
If you return a NullResource I think it returns a "fieldname => null" instead (not 100% sure).
In laravel-responder it returns an empty array in both cases, it seems.
I actually recall that yesterday it returned a null field, but I don't remember if it was before or after i started playing with this library 🤔
Can you confirm this or it's a problem with my code? If it's the intended behaviour, would't it be better if it returned just null instead of an empty array?
The use case scenario here is that I have users with an avatar which is optional, so the relation could return null.
If it's not null, it returns the image transformed by its transformer as intended,
If it's null, it returns an empty array.
The text was updated successfully, but these errors were encountered: