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
- Drupal 9.5.11 site with JSON:API enabled and multilingual enabled
- JSON:API Book module enabled
- Navigate in a browser to https://sitenme.org/jsonapi/
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | blocked_rerouting-3408255.patch | 782 bytes | saurabh-2k17 |
Issue fork json_api_book-3408255
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
Comment #2
jurgenhaasHmm, 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.Comment #4
jurgenhaasPlease give the MR/patch a try and let us know if that fixes your issue.
Comment #5
saurabh-2k17 commentedHi @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).Comment #6
saurabh-2k17 commentedI 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.
Comment #7
jurgenhaasWould 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
512toJSON_UNESCAPED_SLASHESwhich 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.
Comment #8
saurabh-2k17 commentedApologies @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.
Comment #10
jurgenhaas