Add runtime validation that the discriminator map contains all entity classes, also abstract ones? #10389
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
abstract
intermediate 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
abstract
entities, 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
SchemaValidator
tool, 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
abstract
classes was added briefly afterwards in a0a81db.Until today, the exception thrown for an incomplete DM explicitly mentions
abstract
entity classes as an option:orm/lib/Doctrine/ORM/Mapping/MappingException.php
Lines 761 to 768 in c5e4e41
Consequences
Merging
abstract
entity classes in the past.ENUM
s and the like?)Not merging
SchemaValidator
check (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:)?