Problem/Motivation

If the file--file resource has been aliased using JSON:API Extras or #3105318: Add a public API for aliasing resource type names, uploading files fails

    "errors": [
        {
            "title": "Internal Server Error",
            "status": "500",
            "detail": "Route \\u0022jsonapi.file--file.individual\\u0022 does not exist.",

This is due to a hardcoded string for the resource type name:

    // @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.
    $self_link = new Link(new CacheableMetadata(), Url::fromRoute('jsonapi.file--file.individual', ['entity' => $file->uuid()]), 'self');
    /* $self_link = new Link(new CacheableMetadata(), $this->entity->toUrl('jsonapi'), ['self']); */
    $links = new LinkCollection(['self' => $self_link]);

The @todo is irrelevant to this issue (although it'd make life a lot easier.)

Steps to reproduce

Alias file entity resource. Try uploading.

Proposed resolution

Fetch the resource type from the repository.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#3 cannot_upload_files-3171154-3.patch1.62 KBmglaman

Issue fork drupal-3171154

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mglaman created an issue. See original summary.

mglaman’s picture

Actually, a few lines down we get the file resource type and could just use the type name from there

    $relatable_resource_types = $resource_type->getRelatableResourceTypesByField($resource_type->getPublicName($file_field_name));
    $file_resource_type = reset($relatable_resource_types);
    $resource_object = ResourceObject::createFromEntity($file_resource_type, $file);
mglaman’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.62 KB

Here is a patch to prevent using a hardcoded resource type name.

mglaman’s picture

Actually, we have \Drupal\jsonapi\Routing\Routes::getRouteName

  public static function getRouteName(ResourceType $resource_type, $route_type) {
    return sprintf('jsonapi.%s.%s', $resource_type->getTypeName(), $route_type);
  }

This code should call that method with the resource type and "individual".

mglaman’s picture

This seems like an oversight from #2958554: Allow creation of file entities from binary data via JSON API requests and would be a follow up to address the missing usage of the route name helper method.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

kristen pol’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative, +Needs tests

Thanks for the issue and patch. I agree with #4 that Routes::getRouteName() should be used so moving back to needs work. I might try to do this via gitlab so I'll open a fork branch.

mglaman’s picture

Status: Needs work » Needs review
kristen pol’s picture

Thanks for the status change. Not sure doing the patch is gitlab was helpful for me but figured I'd try it once. :)

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bbrala’s picture

I love this change, code it good.

We do need tests for this issue. Preferably a seperate test patch to show the failure, but since the testing methology is change in #3105318: Add a public API for aliasing resource type names perhaps we should wait for that issue.

bbrala’s picture

Status: Needs review » Needs work

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.