Add runtime validation that the discriminator map contains all entity classes, also abstract ones?#10389
Closed
mpdude wants to merge 1 commit intodoctrine:2.14.xfrom
Closed
Conversation
… classes, also abstract ones
mpdude
commented
Jan 13, 2023
Comment on lines
+271
to
+273
| if (! in_array($class->name, $class->discriminatorMap, true)) { | ||
| throw MappingException::mappedClassNotPartOfDiscriminatorMap($class->name, $class->rootEntityName); | ||
| } |
Contributor
Author
There was a problem hiding this comment.
This extra check is needed here to catch inheritance roots missing in the DM.
This was referenced Jan 13, 2023
Contributor
Author
|
@beberlei I know you are short on time, but since you authored the core feature a decade ago, your assessment would be highly valued. |
This was referenced Jan 13, 2023
Closed
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.
A general decision needs to be made here.
This PR adds a runtime metadata check, requiring all entity classes – including
abstractintermediate ones – to be listed in the Discriminator Map (DM).Why?
#8378 (with a fix in #8903) added this requirement to the SchemaValidator tool.
At least #8736 and #9095 pointed out this was a policy change. #9096 briefly removed the requirement for listing
abstractentities, but #9142 opposed and put the check back in.If we think that it is necessary to have all classes in the DM to ensure correct ORM behaviour, then this should not only be checked by the
SchemaValidatortool, but instead deserves a runtime check in theClassMetadataFactory.Reasons for this requirement
The pro arguments are somewhat sketchy.
A failing test for that was provided in #9145 as POC, #10387 might be a fix.
That problem goes away when the abstract class is included in the DM (shown in #10388), but it may well be a real issue in its own right.
Reasons against the requirement
Since no instances of abstract entities can be created, the corresponding value from the DM is never actually used. Users would be required to put in a dummy discriminator value.
Doctrine could/should be able to figure out abstract intermediate entity classes by itself, given that all relevant leaf classes are listed in the DM.
Archaeological research as follows
Runtime validation of DM requirements was already in place, with abstract classes being explicitly exempted.
orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Lines 278 to 283 in c5e4e41
That goes back to 5ff44b5 where DM checking was initially added (corresponding issues #1809, #1810).
The exemption for
abstractclasses was added briefly afterwards in a0a81db.Until today, the exception thrown for an incomplete DM explicitly mentions
abstractentity classes as an option:orm/lib/Doctrine/ORM/Mapping/MappingException.php
Lines 761 to 768 in c5e4e41
Consequences
Merging
abstractentity classes in the past.ENUMs and the like?)Not merging
SchemaValidatorcheck (Fix SchemaValidator with abstract child class in discriminator map #9096)ClassMetadataInfo::$subclasses, it is not obvious what may/may not be included there.To-Do
👇🏻 What do you think about this – Merge (:+1:) or do not merge (:-1:)?