[8.x] Avoid constructor call when fetching resource JSON options #40261
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.
As described in #40258 when we use a type hinted constructor, or a constructor that accepts more than 1 required parameter on a singular resource and it's constructed through a collection resource, the
collectsResources::jsonOptions()
method fails because it passes in an empty array just to be able to call the non-staticjsonOptions
method on the singular resource.I'm not sure if this is too much "magic", but it's the the only thing I can think of to maintain backwards compatibility with whats recently been added without reverting the original PR, so if you don't agree, please feel free to reject the PR.
This will work to fix the issue however, as the constructor is no longer called when creating a new instance of the singular resource, therefore bypassing any type hints or multiple required parameters. This should be safer than passing in an empty array, as by passing in an empty array it's assumed that the constructor does nothing other than set the
$resource
property as the passed in parameter.Automated test has been added, and I've checked this solution locally in https://github.com/philcross/laravel-jsonOptions-bug which is a project to reproduce the bug.