TypeError: Argument 1 passed to Drupal\jsonapi_schema\Controller\JsonApiSchemaController::Drupal\jsonapi_schema\Controller\{closure}() must be an instance of Drupal\jsonapi\ResourceType\ResourceType

Created on 26 December 2020, over 4 years ago
Updated 14 May 2025, 6 days ago

Problem/Motivation

See originating issue in the farmOS issue queue: #3189918: Broken relationship schema link in farmOS 2.x JSON:API β†’

There appears to be a bug in the JsonApiSchemaController::getDocumentSchema() method when the $resource_type parameter is an array. The method runs an array_map() but appears to have an assumption that each item in the array will be an object of type ResourceType, when it is an array of strings (the resource type names).

Here is the current code:

    if (is_array($resource_type)) {
      $titles = array_map(function (ResourceType $type) use ($route_type) {

This appears to be happening on entity reference fields that do not limit which bundles are allowed to be referenced. I have two entity reference fields - one of which DOES limit to a specific bundle, and one that does not. This issue does NOT appear to affect the former, but it does affect the latter.

I wonder if there was a recent refactor to move away from passing ResourceType objects around, and started passing the resource type name strings instead, and this case was missed, perhaps? Seems like an easy one to overlook. This other issue almost seems like it could have been caused by the same refactor: #3179685: Resource type on route defaults is a string, may not be set at all β†’

Steps to reproduce

  1. Create a node type (eg: test) with an entity reference field (eg: test_reference) that does not limit the allowed bundle types.
  2. Go to /api/node/test/resource/relationships/test_reference/related/schema... error!

Proposed resolution

Simply removing the ResourceType type hint appears to fix this issue.

    if (is_array($resource_type)) {
      $titles = array_map(function ($type) use ($route_type) {

I assume we'll also want to add a test to avoid regressions in the future.

Remaining tasks

  • Provide new test(s) to prevent regressions.
πŸ› Bug report
Status

RTBC

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States m.stenta

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • @mstenta opened merge request.
  • πŸ‡ΊπŸ‡ΈUnited States m.stenta

    I added general tests for this module in πŸ“Œ Add basic test coverage Active , which allowed me to test this. I opened a MR that demonstrates the issue.

    In testing this, I found that I had to remove the ResourceType type hint from both line 120 and 159. The automated tests caught both of them.

    The GitLab CI test results show the same error:

    There was 1 error:
    1) Drupal\Tests\jsonapi_schema\Kernel\JsonApiSchemaTest::testJsonApiSchemaRelated
    PHPUnit\Framework\Exception: Uncaught PHP Exception TypeError: "Drupal\jsonapi_schema\Controller\JsonApiSchemaController::Drupal\jsonapi_schema\Controller\{closure}(): Argument #1 ($type) must be of type Drupal\jsonapi\ResourceType\ResourceType, string given" at /builds/project/jsonapi_schema/src/Controller/JsonApiSchemaController.php line 159
    

    https://git.drupalcode.org/project/jsonapi_schema/-/jobs/5260475

    Now I will push a fix commit, so we can see tests pass.

  • πŸ‡ΊπŸ‡ΈUnited States m.stenta

    Tests are green!

    • m.stenta β†’ committed 494c3baf on 8.x-1.x
      Issue #3189930: TypeError: Argument 1 passed to Drupal\jsonapi_schema\...
  • πŸ‡ΊπŸ‡ΈUnited States m.stenta
Production build 0.71.5 2024