Invalid URI when the optional File Directory is empty

Created on 19 September 2020, over 3 years ago
Updated 30 January 2023, over 1 year ago

Problem/Motivation

I am posting files using REST API. When the file is being saved, I get an error saying:

Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException: Unprocessable Entity: validation failed. uri.0.value: This value should be of the correct primitive type. in Drupal\file\Plugin\rest\resource\FileUploadResource->resourceValidate() (line 55 of /var/www/html/web/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php).

I did some digging and found that the problem is caused by FileUploadResource::handleFileUploadForField() when there is no directory configured in the field settings, see screenshot

Steps to reproduce

- Create a field and configure it to allow upload of images for example
- Leave the File Directory empty

- Use Rest to post an image to /file/upload/node/page/field_file?_format=json

}
  'fid' =>
  array(1) {
    'value' =>
    string(6) "494563"
  }
  'filename' =>
  array(1) {
    'value' =>
    string(37) "inputx.jpg"
  }
  'uri' =>
  array(1) {
    'value' =>
    string(46) "public://inputx.jpg"
  }
  'filemime' =>
  array(1) {
    'value' =>
    string(10) "image/jpeg"
  }
  'filesize' =>
  array(1) {
    'value' =>
    string(6) "177510"
  }
  'type' =>
  array(1) {
    'target_id' =>
    string(5) "image"
  }
  'status' =>
  array(1) {
    'value' =>
    string(1) "1"
  }
  'data' =>
  array(1) {
    'value' =>
    string(236680) "ZJRgABAQAAkACQAAD/4QB0RXhpZgAATU0AKgAAAAgABAEaAAUAAAABAAAAPgEbAAUAA..."
  }
}

Proposed resolution

I think we need to check if the optional configuration is empty to avoid appending an extra / to the scheme i.e. public:// + /

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
File moduleΒ  β†’

Last updated 3 days ago

Created by

πŸ‡¬πŸ‡§United Kingdom marcelovani London

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

Production build 0.69.0 2024