-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[9.x] Allow jsonSerialize() to return other types #42133
Conversation
Ping @X-Coder264 this is what I meant on that pr your sent in ^ |
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.
This change looks wrong to me. Having an implementation return a more specific type than that required by the interface is totally OK, and actually desirable in this case. https://3v4l.org/CYkJl
Updated it to be a union type with |
With |
@X-Coder264 my ping was more about you being okay with this change? |
@driesvints There's nothing intrinsically wrong with this change. From my perspective though it's a bit weird for a collection (which is just arrays on steroids) to be JSON serialized as anything else but an array. But if people want to do that I guess there's no reason from a framework perspective to block that so on that level I'm fine with the proposed change. |
It's actually the Collection item's I've updated it to still return an |
If we do this then I suggest we move to the mixed return type like the PHP interface and not bother with union types. |
This PR updates
jsonSerialize()
's return type tomixed
, matching PHP's definition.Use case
Allows models to override
jsonSerialize
, and serialize into strings rather than always having to be an JSON object.Example
We have a simple model,
Permission
, who's only attribute isname
:When we fetch all permissions, the collection is automatically serialized into JSON objects.
Controller
Would currently return
With this change,
Permission
, can overridejsonSerialize
, and return astring
.Updated response after this PR: