fix(laravel): skip relation metadata for abstract Eloquent models#7933
Open
cay89 wants to merge 1 commit intoapi-platform:4.3from
Open
fix(laravel): skip relation metadata for abstract Eloquent models#7933cay89 wants to merge 1 commit intoapi-platform:4.3from
cay89 wants to merge 1 commit intoapi-platform:4.3from
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
RelationMetadataLoadercrashes withCannot instantiate abstract classwhen an API resource model extends an abstract base model.The Symfony serializer's
ClassMetadataFactorywalks the full class hierarchy when building metadata. For a chain likeItemTranslation extends AbstractModel extends Model, the loader is also invoked forAbstractModel. SinceAbstractModelis still a subclass ofModel, the existingis_a(..., Model::class, true)guard passes, and$refl->newInstanceWithoutConstructor()then throws because the class is abstract.This PR adds an early
return falsewhen the reflected class is abstract — there is nothing meaningful to load on an abstract model anyway, and the recursion continues normally for the concrete subclass.Changes
src/Laravel/Eloquent/Serializer/Mapping/Loader/RelationMetadataLoader.php: guard withReflectionClass::isAbstract()before instantiation.src/Laravel/Tests/Eloquent/Serializer/Mapping/Loader/RelationMetadataLoaderTest.php.src/Laravel/workbench/app/Models/AbstractModel.php(generic abstract Eloquent model, reusable by future tests).Backwards compatibility
No BC break: previously, calling the loader with an abstract model class always threw
Error: Cannot instantiate abstract class. There is no existing code path that relied on this behavior.