Images with non transliterated names will break the jsonapi validation

Created on 24 April 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

Production build 0.69.0 2024