Problem/Motivation

Originally posted this issue in JSON:API here: https://www.drupal.org/project/drupal/issues/3407746

Our development site this is on hasn't updated to 10.x yet so we are using the 1.0.2 version of the module on 9.5.11. It is a multilingual site.

The issue:
With the module installed, when navigating to a URL like https://sitenme.org/jsonapi/ we expect the path to redirect to https://sitename.org/en/jsonapi. It doesn't and instead gives a 500 error in the browser.

{"jsonapi":{"version":"1.0","meta":{"links":{"self":{"href":"http:\/\/jsonapi.org\/format\/1.0\/"}}}},"errors":[{"title":"Internal Server Error","status":"500","detail":"Syntax error","links":{"via":{"href":"https:\/\/branch-sitename.pantheonsite.io\/jsonapi\/"},"info":{"href":"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html#sec10.5.1"}}}]}

and in the logs it gives:

JsonException: Syntax error in json_decode() (line 120 of /code/web/modules/contrib/json_api_book/src/EventSubscriber/ResponseSubscriber.php).

I believe the 1.1.x and 1.0.2 are using the same code, but I haven't tested this on a D10 site yet.

NOTE: The same issue happens when navigating to https://sitenme.org/en/jsonapi/, it doesn't remove the trailing "/", so it may not be just with multilingual sites.

Steps to reproduce

  1. Drupal 9.5.11 site with JSON:API enabled and multilingual enabled
  2. JSON:API Book module enabled
  3. Navigate in a browser to https://sitenme.org/jsonapi/
CommentFileSizeAuthor
#6 blocked_rerouting-3408255.patch782 bytessaurabh-2k17
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

ridefree created an issue. See original summary.

jurgenhaas’s picture

Version: 1.0.2 » 1.1.x-dev

Hmm, never seen this before, but looking at the code, I wonder if $response->getContent() isn't returning any valid content which then leads to the JsonException.

jurgenhaas’s picture

Status: Active » Needs review

Please give the MR/patch a try and let us know if that fixes your issue.

saurabh-2k17’s picture

Hi @jurgenhaas, thanks for your response and MR. I updated the Drupal core to 10.1.7 and tried to visit https://sitenme.org/jsonapi/ and faced the similar issue. Later, I used your MR and patched the module, but the same problem persists.

Edit:

Moreover, the error message in logs remains the same, just the line number changes because of the plain diff used as a patch.

JsonException: Syntax error in json_decode() (line 123 of /var/www/html/web/modules/contrib/json_api_book/src/EventSubscriber/ResponseSubscriber.php).

saurabh-2k17’s picture

StatusFileSize
new782 bytes

I found that the problem was not with the content, the content was already there, just that it was expecting it to be in a readable format. I made a slight change and it works now.

jurgenhaas’s picture

Would you mind collaborating in the MR instead of a new patch? The latter will not be supported for much longer.

WRT to your proposed change, I'm not sure that this is reasonable. The patch replaces the third argument 512 to JSON_UNESCAPED_SLASHES which is about the depth of the JSON decoding, where the new argument is a constant which should be used as a constant for the fourth argument.

Looking at my proposed change in the MR, I just realized that we're currently using the option JSON_THROW_ON_ERROR. That might be the issue, as this will throw the exception and prevent any further handling of other middlewares being in place.

I've updated the MR to catch exceptions from JSON decoding and encoding to make sure, that this subscriber will not do anything if the JSON caused any issues.

Please give that another try and if you need to propose further changes, please do that in the issue fork.

saurabh-2k17’s picture

Status: Needs review » Reviewed & tested by the community

Apologies @jurgenhaas, I will keep MR thing in mind. I see what you did and tested it too, it works! I am changing the state to RTBC.

  • jurgenhaas committed d3d996e7 on 1.1.x
    Issue #3408255 by jurgenhaas, saurabh-2k17: Blocked rerouting of URL for...
jurgenhaas’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.