In order to keep things consistent, all responses from a JSON API endpoint should return proper JSON. If you pass in a field that does not exist as a filter or sort a 4xx code is returned but the entire response is in HTML.

I do have JSON API extras configured. Not sure if that may have something to do with it?

Comments

mstef created an issue. See original summary.

gabesullice’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +DX (Developer Experience)

Hi @mstef, are you sending the request header: Accept: application/vnd.api+json?

mstef’s picture

Yes, I'm passing in both Accept and Content-type with that value.

gabesullice’s picture

Status: Postponed (maintainer needs more info) » Active

Thanks for the info :)

wim leers’s picture

Status: Active » Postponed (maintainer needs more info)

What is the Accept header you're sending?

Because I can reproduce getting a HTML response when requesting /jsonapi/node/article?filter[foobar]=baz … but only if I don't specify Accept: application/vnd.api+json.

mstef’s picture

I'm sending

Accept: application/vnd.api+json
Content-type: application/vnd.api+json

I am also getting HTML for 404s.

wim leers’s picture

StatusFileSize
new1.65 KB

I suspect something is off in your environment/tests then. Because this test passes for me.

mstef’s picture

Issue summary: View changes

Running docksal locally with latest D8. Let me know if there's something else I can test/report for you.

wim leers’s picture

#7 also passes when jsonapi_extras 8.x-1.x or 8.x-2.x is installed.

wim leers’s picture

#8: when you apply #7, do tests pass or fail for you?

mstef’s picture

I will test as soon as possible (tomorrow, most likely).

I was able to test this issue on a Platform.sh server and I am getting the same response that I originally reported. After I test the patch, I will PM you a live endpoint you can test against.

e0ipso’s picture

#7 also passes when jsonapi_extras 8.x-1.x or 8.x-2.x is installed.

Thanks for checking that @Wim Leers! ❤️

wim leers’s picture

#11: 👌

#12: ❤️

mstef’s picture

This is the output after running the test in the patched file:

Testing Drupal\Tests\jsonapi\Functional\NodeTest F.....F. 8 / 8 (100%)

Time: 4.65 minutes, Memory: 8.00MB

There were 2 failures:

1) Drupal\Tests\jsonapi\Functional\NodeTest::testPatchPath
Failed asserting that Array &0 (
'errors' => Array &1 (
0 => Array &2 (
'code' => 0
'detail' => 'The current user is not allowed to PATCH the selected field (path).'
'id' => '/node--camelids/91ca3c86-f390-46a3-88d8-9dc5fa2ecb76'
'links' => Array &3 (
'info' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4'
)
'source' => Array &4 (
'pointer' => '/data/attributes/path'
)
'status' => 403
'title' => 'Forbidden'
)
)
) is identical to Array &0 (
'errors' => Array &1 (
0 => Array &2 (
'code' => 0
'detail' => 'The current user is not allowed to PATCH the selected field (path). The following permissions are required: 'create url aliases' OR 'administer url aliases'.'
'id' => '/node--camelids/91ca3c86-f390-46a3-88d8-9dc5fa2ecb76'
'links' => Array &3 (
'info' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4'
)
'source' => Array &4 (
'pointer' => '/data/attributes/path'
)
'status' => 403
'title' => 'Forbidden'
)
)
).

/var/www/docroot/modules/contrib/jsonapi/tests/src/Functional/ResourceTestBase.php:598
/var/www/docroot/modules/contrib/jsonapi/tests/src/Functional/ResourceTestBase.php:550
/var/www/docroot/modules/contrib/jsonapi/tests/src/Functional/NodeTest.php:283

2) Drupal\Tests\jsonapi\Functional\NodeTest::testPatchIndividual
Failed asserting that Array &0 (
'errors' => Array &1 (
0 => Array &2 (
'code' => 0
'detail' => 'The current user is not allowed to PATCH the selected field (created).'
'id' => '/node--camelids/403c8e56-4d7c-4332-9cc0-a70668f2eaba'
'links' => Array &3 (
'info' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4'
)
'source' => Array &4 (
'pointer' => '/data/attributes/created'
)
'status' => 403
'title' => 'Forbidden'
)
)
) is identical to Array &0 (
'errors' => Array &1 (
0 => Array &2 (
'code' => 0
'detail' => 'The current user is not allowed to PATCH the selected field (created). The 'administer nodes' permission is required.'
'id' => '/node--camelids/403c8e56-4d7c-4332-9cc0-a70668f2eaba'
'links' => Array &3 (
'info' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4'
)
'source' => Array &4 (
'pointer' => '/data/attributes/created'
)
'status' => 403
'title' => 'Forbidden'
)
)
).

/var/www/docroot/modules/contrib/jsonapi/tests/src/Functional/ResourceTestBase.php:598
/var/www/docroot/modules/contrib/jsonapi/tests/src/Functional/ResourceTestBase.php:550
/var/www/docroot/modules/contrib/jsonapi/tests/src/Functional/ResourceTestBase.php:1704

FAILURES!
Tests: 8, Assertions: 334, Failures: 2.
wim leers’s picture

The two failing tests are NodeTest::testPatchPath() and NodeTest::testPatchIndividual(). That means the test I added, NodeTest::test2969398() passed.

AFAICT that means you also cannot reproduce the problem you originally reported? Can you still reproduce it manually? If so, what's different about the test I wrote in #7?

(Those 2 failures are currently happening when using JSON API on Drupal 8.6 — see https://www.drupal.org/pift-ci-job/976585. I'll get that fixed soon.)

mstef’s picture

StatusFileSize
new99.83 KB

edit: disregard this comment. i'll report back shortly.

it is actually working on a fresh install using a node endpoint. let me try to find the root of the problem.

mstef’s picture

e0ipso’s picture

@mstef if you are making these requests using the browser you need to make sure to have a browser extensions to pass in headers like Accept: application/vnd.api+json. Can you try that and report back? Alternatively you can use a REST client like Postman for an easier setup.

mstef’s picture

@e0ipso, I know; I am and have been. I just did that without thinking for a minute..

It is working fine on a fresh 8.5.3 install. So, as you expected, it must be stemming from something on my site, but I have no idea what just yet. I will try to find out and report back. It happens there for node and all other entity endpoint as well still.

mstef’s picture

Project: JSON:API » JSON:API Extras

I can confirm this is stemming from JSON API Extras. If I disable that module on the affected environment, I get a proper JSON response. Once I re-enable JSON API Extras, hitting an overridden endpoint with a non-existent field as a filter or sort results in an HTML response.

mstef’s picture

Status: Postponed (maintainer needs more info) » Active
StatusFileSize
new108.55 KB
new57.53 KB

Here is a before and after

mstef’s picture

Seems to happen only if the "path prefix" is changed.

Given that, maybe it's still related to JSON API, and not the "extras" module.

wim leers’s picture

Title: 4xx errors returned for using an invalid field in filter or sort returns HTML response rather than JSON » [PP-2] When using JSON API Extras' ability to change the base path, 4xx errors returned for using an invalid field in filter or sort returns HTML response rather than JSON
Project: JSON:API Extras » JSON:API
Assigned: Unassigned » e0ipso
Status: Active » Postponed

@mstef++ for that digging! That helped a lot :) In fact, it made it easy to pin this down!

The root cause is #2971745: Don't hardcode `/jsonapi/` in FormatSetter, read JSON API base path from container parameter instead, which @e0ipso added to jsonapi specifically for jsonapi_extras (see #2949632-9: Make ResourceTypeRepository aware of the path prefix).

Once #2949632: Make ResourceTypeRepository aware of the path prefix lands, #2971745 can land too, and then this will be fixed. But it requires JSON API Extras to be updated to be compatible with #2949632. That's the only reason #2949632 hasn't yet been committed.

e0ipso’s picture

👍🏽!

wim leers’s picture

Title: [PP-2] When using JSON API Extras' ability to change the base path, 4xx errors returned for using an invalid field in filter or sort returns HTML response rather than JSON » [PP-1] When using JSON API Extras' ability to change the base path, 4xx errors returned for using an invalid field in filter or sort returns HTML response rather than JSON
wim leers’s picture

Title: [PP-1] When using JSON API Extras' ability to change the base path, 4xx errors returned for using an invalid field in filter or sort returns HTML response rather than JSON » When using JSON API Extras' ability to change the base path, 4xx errors returned for using an invalid field in filter or sort returns HTML response rather than JSON
Assigned: e0ipso » Unassigned
Status: Postponed » Closed (duplicate)
Issue tags: +API-First Initiative
Related issues: +#2971745: Don't hardcode `/jsonapi/` in FormatSetter, read JSON API base path from container parameter instead

Actually, this is just a pure duplicate of #2971745: Don't hardcode `/jsonapi/` in FormatSetter, read JSON API base path from container parameter instead AFAICT. It's great to have @e0ipso's +1 though :) So, closing this.