africanlii / liiweb

Drupal-based LII website powering free access to law portals publishing caselaw, legislation, government gazettes and journal articles. Developed by AfricanLII at the University of Cape Town and Laws.Africa
GNU Lesser General Public License v3.0
4 stars 3 forks source link

Error when linking images to a legislation node #233

Open longhotsummer opened 3 years ago

longhotsummer commented 3 years ago

We associated images (much like PDFs) with a node by uploading the image and then updating the node with a list of all the associated files (both images and PDFs).

We always get an error when updating the node to attach an image to it. We can successfully upload the file and get a node id for the file, but linking it to the node fails.

The error is:

{"jsonapi":{"version":"1.0","meta":{"links":{"self":{"href":"http:\/\/jsonapi.org\/format\/1.0\/"}}}},"errors":[{"title":"Unprocessable Entity","status":"422","detail":"Unprocessable Entity: validation failed.","links":{"via":{"href":"http:\/\/namiblii.org\/akn\/na\/act\/p\/1978\/ag4\/eng@1978-01-30"}}}]}

The file is successfully uploaded and exists as a temporary file on the server and in S3:

image

image

Note that this occurs whether or not a temporary file with this name already exists on the server.

Steps to reproduce:

  1. upload a new legislation image file and get an id
  2. PATCH a legislation node and include the image file:
{"data": {
    "type": "node--legislation",
    "relationships": {
        "field_files": {
            "data": []
        },
        "field_images": {
            "data": [{
                "type": "file--file",
                "id": "0ac29d66-f38c-4768-885a-e1e799084eec"
            }]
        }
    }
}}
cristiroma commented 3 years ago

I have a hint, please open LiiWebEntityResource.php and set a breakpoint at line 265:

    array_reduce($field_names, function (EntityInterface $destination, $field_name) use ($resource_type, $parsed_entity) {
      $this->updateEntityField($resource_type, $parsed_entity, $destination, $field_name);
>>> HERE >>>      return $destination;
    }, $entity);

And see why validation fails.

Hayk-web commented 3 years ago

Thanks. I have reproduced it already. The issue related to the static::validate function which is based on core/jsonapi module. Now I am working on fixing the validation issue.

Hayk-web commented 3 years ago

Does this issue actual? I have sent a request to http://site_name/jsonapi/node/legislation/field_images endpoint for image uploading. Image uploads correctly(s3://legislation/images/test1.png path in Drupal database). For the second step, I have sent the request to "a legislation node URL" endpoint, and the file has been attached to the node correctly. Screenshot from 2021-03-15 12-32-27 Screenshot from 2021-03-15 12-33-06

longhotsummer commented 3 years ago

Hi Hayk, I'm still seeing this issue in production. Was there a patch created to fix it which we haven't applied?

Hayk-web commented 3 years ago

There wasn't a patch. I've cloned the project from the repo(master branch). I will try to reproduce it on my local again. I have tried with existing files and with new ones. All works correctly. Let me check again.

longhotsummer commented 3 years ago

@Hayk-web any progress? You mentioned above that you were able to reproduce this? Have you been able to try @cristiroma 's suggestion about the breakpoint?

Thanks.

Hayk-web commented 3 years ago

I haven't been able to reproduce the issue yet. Will try one more time.

Hayk-web commented 3 years ago

Info: I am planning to have a look at this task today or tomorrow. I'll come back to you with the results. Thanks.