Problem/Motivation
Came across this issue while working on a site that had a node type "404".
JSON:API uses array_keys()
on the result of \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getBundleInfo($entity_type_id)
. getBundleInfo()
returns an associative array keyed on bundle name. Unfortunately, array_keys()
will return (int) 404
in our case, passing the bundle name as integer down the stack, which in turn triggers some of the 'bundle-ID-is-string-and-not-empty' assertions (for example in isLocatableResourceType
and isMutableResourceType
:
protected static function isLocatableResourceType(EntityTypeInterface $entity_type, $bundle) {
assert(is_string($bundle) && !empty($bundle), 'A bundle ID is required. Bundleless entity types should pass the entity type ID again.');
return $entity_type->getStorageClass() !== ContentEntityNullStorage::class;
}
Proposed resolution
Explictly cast the result of array_keys(bundle_array) to a string to negate the automatic conversion to integer by array_keys().
Remaining tasks
Review & commit
User interface changes
None.
API changes
None.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#12 | 3075831-12.patch | 1.86 KB | aleevas |
Comments
Comment #2
Wim LeersHah, nice edge case!
I see you even provided test coverage! 🥳🙏 Thanks!
DrupalCI failed to test the test-only patch with a "fetch save error" 😲 Never seen that before. Retesting…
Comment #3
Wim LeersI think this qualifies as a
bug since this is a pretty peculiar edge case that very, very few people will ever run into! 😃Comment #5
Wim LeersThis looks like it's moving lines, but it's not. @mr.baileys just incremented 2 and 3 to 3 and 4 because of the extra
NodeType
he added :)Comment #7
larowlanCommitted cf5ad5f and pushed to 8.8.x. Thanks!
Leaving at RTBC for 8.7 for commit after freeze ends
Comment #8
idebr CreditAttribution: idebr at ezCompany commented#3 Actually we fixed this before in #2900459: InvalidArgumentException: Routing requirement for "_bundle" must be a string. Webform submission entities have a generated bundle that are always interpreted as integers in PHP.
Comment #9
Wim LeersD'oh. Thanks @idebr. Looks like this is not super rare then, because Webform is obviously widely used.
So this is just PHP's typecasting getting in the way and us needing to remember to always do the proper casting all over the place. Fascinating.
Comment #10
larowlanThis needs a re-roll for 8.7
Comment #11
Wim LeersComment #12
aleevasHere is my reroll for 8.7 branch
Comment #13
aleevasComment #14
Wim Leers👍 — thanks @aleevas!
(The patch is smaller because the
::testCaching()
test coverage did not exist in Drupal 8.7.)Comment #15
Gábor HojtsyCommitted d3e67cc and pushed to 8.7.x. Thanks!