Images with non transliterated names will break the jsonapi validation

Created on 24 April 2024, 7 months ago
Updated 5 September 2024, 2 months ago

Problem/Motivation

For what ever reason the JSONAPI responded from one day to another with a JSONAPI validation error.

This was the cause point. When I put // in front of the assert everything worked.

  public function onResponse(ResponseEvent $event) {
    $response = $event->getResponse();
    if (!str_contains($response->headers->get('Content-Type', ''), 'application/vnd.api+json')) {
      return;
    }

    // Wraps validation in an assert to prevent execution in production.
    assert($this->validateResponse($response, $event->getRequest()), 'A JSON:API response failed validation (see the logs for details). Report this in the Drupal issue queue at https://www.drupal.org/project/issues/drupal');
  }

The validation had 19 or so errors, which made no sense to me. Like wrong uri schemas and so forth. I had to manually check the JSON response in Postman to check what it means. It turns out that the JSONAPI checks URIs inside quotes. So even if an image file might be requestable with spaces in it (I do not know), the schema check of it will kill the JSON API response.

See below what caused it for me. No idea IF you can do anything about it. I will have to reupload filenames without spaces I guess, but as you know, your "links" addition to the JSON API response can kill the response. I mean it is not forbidden to use files with spaces in it.

{
            "type": "file--file",
            "id": "22a7c0d6-06e7-4f6d-aef5-26c2cba57de8",
            "links": {
                "avatar": {
                    "href": "http://foobar.com/sites/default/files/styles/avatar/public/pictures/2024-04/Example Model1_Nero AI_avatar (3).jpg?itok=ud8KmNHD",
                    "meta": {
                        "width": "400",
                        "height": "400",
                        "rel": [
                            "drupal://jsonapi/extensions/consumer_image_styles/links/relation-types/#derivative"
                        ]
                    }
                },
                "icon": {
                    "href": "http://foobar.com/sites/default/files/styles/icon/public/pictures/2024-04/Example Model1_Nero AI_avatar (3).jpg?itok=RD4N0wGg",
                    "meta": {
                        "width": "64",
                        "height": "64",
                        "rel": [
                            "drupal://jsonapi/extensions/consumer_image_styles/links/relation-types/#derivative"
                        ]
                    }
                },
                "self": {
                    "href": "http://foobar.com/en/jsonapi/file/file/22a7c0d6-06e7-4f6d-aef5-26c2cba57de8"
                },
                "thumbnail": {
                    "href": "http://foobar.com/sites/default/files/styles/thumbnail/public/pictures/2024-04/Example Model1_Nero AI_avatar (3).jpg?itok=vQq7pQ1W",
                    "meta": {
                        "width": "67",
                        "height": "100",
                        "rel": [
                            "drupal://jsonapi/extensions/consumer_image_styles/links/relation-types/#derivative"
                        ]
                    }
                },

See also the attachment.

🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

🇩🇪Germany ro-no-lo

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

Comments & Activities

  • Issue created by @ro-no-lo
  • 🇩🇪Germany ro-no-lo

    Side note: I had "Sanitize filenames" everything off.

    A fix will be to turn everything on there and reupload the images with spaces in it.

  • Faced the same issue.

    I agree killing the response is a big deal, but the problem here is that although having files with spaces is allowed, links, according to the JSONAPI specs (https://jsonapi.org/format/#document-links) , MUST follow RFC3986, which does not allow spaces.

    FYI attached patch is not an ideal one, it should be applied at the derivative link generation step, but I was unable to break at the derivative link generation step (and flushing caches did not help), so I just parse it, sanitize and build it back.

    Probably replacing back %2F is not a good idea either. A room for an improvement.

    Attached patch was tested on spaces and all the rest stuff. :, /, ?, #, [, ], @, !, $, &, ', (, ), *, +, ,, ;, =

  • 🇧🇪Belgium stijndmd

    Patch that first decodes the url before parsing and encoding, because we were running into issues with old filenames containting spaces.
    They were being encoded to %2520 instead of %20.

Production build 0.71.5 2024