Problem/Motivation

Assertion fails when uploading a file to a file field that is a BaseFieldDefinition.

    assert(is_null($entity) || $field_definition->getTargetEntityTypeId() === $entity->getEntityTypeId() && $field_definition->getTargetBundle() === $entity->bundle());

This assertion is incorrect because base fields never have a bundle but entities that are not "bundleable" default to returning the entity type form $entity->bundle().

Proposed resolution

Not sure on the best solution, we could check if $field_definition is an instance of BaseFieldDefinition and then not do that assertion?

CommentFileSizeAuthor
#7 3048453-7.patch1.7 KBbenjy
#2 3048453-2.patch1.59 KBbenjy

Comments

benjy created an issue. See original summary.

benjy’s picture

Status: Active » Needs review
StatusFileSize
new1.59 KB

Proposed patch.

wim leers’s picture

Title: Incorrect assertion in TemporaryJsonapiFileFieldUploader » File uploads fail when the @FieldType=file is a base field due to an incorrect assertion in TemporaryJsonapiFileFieldUploader
Issue tags: +API-First Initiative, +Needs tests

Wow, great catch! I'm pretty sure this is going to be a bug in REST's \Drupal\file\Plugin\rest\resource\FileUploadResource too. Nice example of wrong assumptions!

We'll need test coverage for this; \Drupal\Tests\jsonapi\Functional\FileUploadTest is the best fit I think. Core doesn't have an entity type with a @FieldType=file as a base field, so I think we'll need to create that, much like \Drupal\entity_test\Entity\EntityTestMapField.

Note that this fix will first need to land in Drupal core since JSON:API has been committed to Drupal 8.7 core. But to have a faster feedback cycle, I propose we first develop a patch here, where test runs take only a few minutes rather than an hour. I'd be happy to reroll the patch to apply to Drupal core later. Once it is committed to Drupal core, we'll backport it to the contrib module for sites on Drupal 8.5 or 8.6.

wim leers’s picture

Status: Needs review » Needs work

Oh, and the change itself looks good to me! Marking Needs work for test coverage.

benjy’s picture

I propose we first develop a patch here, where test runs take only a few minutes rather than an hour. I'd be happy to reroll the patch to apply to Drupal core later. Once it is committed to Drupal core, we'll backport it to the contrib module for sites on Drupal 8.5 or 8.6.

Interesting point here, it would be cool if you could run Drupal core tests for only the selected module, many changes, like this are so isolated you wouldn't want to run the full suite until you were pretty much ready to commit the change.

wim leers’s picture

Yes, that would be cool :) You actually can nowadays: you’d just need to also patch the drupalci.yml file!

benjy’s picture

StatusFileSize
new1.7 KB

Here's the core version of the patch.