Automated Drupal 11 compatibility fixes for jsonapi_schema

Created on 18 March 2024, about 1 year ago

Problem/Motivation

Hello project maintainers,

This is an automated issue to help make this module compatible with Drupal 11.

Changes will periodically be added to this issue that remove deprecated API uses. To stop further changes from being posted, change the status to anything other than Active, Needs review, Needs work or Reviewed and tested by the community. Alternatively, you can remove the "ProjectUpdateBotD11" tag from the issue to stop the bot from posting updates.

The changes will be posted by the Project Update Bot โ†’ official user account. This account will not receive any issue credit contributions for itself or any company.

Proposed resolution

You have a few options for how to use this issue:

  1. Accept automated changes until this issue is closed

    If this issue is left open (status of Active, Needs review, Needs work or Reviewed and tested by the community) and the "ProjectUpdateBotD11" tag is left on this issue, new changes will be posted periodically if new deprecation fixes are needed.

    As the Drupal Rector project improves and is able to fix more deprecated API uses, the changes posted here will cover more of the deprecated API uses in the module.

    Patches and/or merge requests posted by others are ignored by the bot, and general human interactions in the issue do not stop the bot from posting updates, so feel free to use this issue to refine bot changes. The bot will still post new changes then if there is a change in the new generated patch compared to the changes that the bot posted last. Those changes are then up to humans to integrate.

  2. Leave open but stop new automated changes.

    If you want to use this issue as a starting point to remove deprecated API uses but then don't want new automated changes, remove the "ProjectUpdateBotD11" tag from the issue and use it like any other issue (the status does not matter then). If you want to receive automated changes again, add back the "ProjectUpdateBotD11" tag.

  3. Close it and don't use it

    If the maintainers of this project don't find this issue useful, they can close this issue (any status besides Active, Needs review, Needs work and Reviewed and tested by the community) and no more automated changes will be posted here.

    If the issue is reopened, then new automated changes will be posted.

    If you are using another issue(s) to work on Drupal 11 compatibility it would be very useful to other contributors to add those issues as "Related issues" when closing this issue.

Remaining tasks

Using the patches

  1. Apply the latest patch in the comments by Project Update Bot โ†’ or human contributors that made it better.
  2. Thoroughly test the patch. These patches are automatically generated so they haven't been tested manually or automatically.
  3. Provide feedback about how the testing went. If you can improve the patch, post an updated patch here.

Using the merge request

  1. Review the merge request and test it.
  2. Thoroughly test the changes. These changes are automatically generated so they haven't been tested manually or automatically.
  3. Provide feedback about how the testing went. If you can improve the merge request, create a new branch and merge request and work from there.

Warning: The 'project-update-bot-only' branch will always be overwritten. Do not work in that branch!

Providing feedback

If there are problems with one of the changes posted by the Project Update Bot โ†’ , such as it does not correctly replace a deprecation, you can file an issue in the Drupal Rector issue queue โ†’ . For other issues with the bot, for instance if the issue summary created by the bot is unclear, use the Project analysis issue queue โ†’ .

๐Ÿ“Œ Task
Status

Needs review

Version

1.0

Component

Code

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @project update bot
  • Open on Drupal.org โ†’
    Core: 10.2.1 + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    Waiting for branch to pass
  • This is an automated patch generated using Upgrade Status and Drupal Rector. Please see the issue summary for more details. A merge request is also openend and updated.

    It is important that any automated tests available are run and that you manually test the changes.

    Drupal 11 Compatibility

    According to the Upgrade Status module โ†’ these changes make this module compatible with Drupal 11! ๐ŸŽ‰
    Therefore these changes update the info.yml file for Drupal 11 compatibility.

    Leaving this issue open, even after committing the current patch, will allow the Project Update Bot โ†’ to post additional Drupal 11 compatibility fixes as they become available in Drupal Rector.

    Debug info

    Bot run #11-121090

    This patch was created using these packages:

    1. drupal/upgrade_status: 4.1.0
    2. mglaman/phpstan-drupal: 1.2.7
    3. palantirnet/drupal-rector: 0.20.1
  • Open on Drupal.org โ†’
    Core: 10.2.1 + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    Waiting for branch to pass
  • Status changed to RTBC 7 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany onfire84

    Patch worked for me on a Drupal 10.3.6 site. Setting to RTBC.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    I wonder if we can get this merged, and tag a "stable" 1.0? The last release was made 16 month ago. I know there's a lot of interesting things happening in core for Drupal 11 (see โœจ Introduce "schematic" normalizers Active ) but it seems that there is still some followup work necessary, and this module is still needed right now for users moving to Drupal 11. It is currently a blocker for farmOS: ๐ŸŒฑ Update Drupal core to 11.x in farmOS Active

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland Alexander Tallqvist

    Patch seems to be working for me on a Drupal 10.3.14 site.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Thanks for testing @onfire84 and @alexander-tallqvist.

    Let's get confirmation that this works on Drupal 11. Setting back to Needs Review.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    I tested installing on Drupal 11.1.5 and it failed with the following error:

    $ drush en jsonapi_schema
    
    Fatal error: Declaration of Drupal\jsonapi_schema\Normalizer\DataDefinitionNormalizer::normalize($entity, $format = null, array $context = []) must be compatible with Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize(mixed $data, ?string $format = null, array $context = []): ArrayObject|array|string|int|float|bool|null in /opt/repos/jsonapi_schema/src/Normalizer/DataDefinitionNormalizer.php on line 53
     [warning] Drush command terminated abnormally.
    

    I had to fix this in farmOS as well, so I'm familiar with the solution. I'll work on this...

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland Alexander Tallqvist

    @m.stena Probably related to this: https://www.drupal.org/project/jsonapi_schema/issues/3504440 ๐Ÿ› PHP8.3 compatibility issues Active

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Fatal error: Declaration of Drupal\jsonapi_schema\Normalizer\DataDefinitionNormalizer::normalize($entity, $format = null, array $context = []) must be compatible with Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize(mixed $data, ?string $format = null, array $context = [])

    I think fixing this is going to necessitate dropping support for Drupal 10.

    @bradjones1: I see that you have started a 2.x (and 2.0.x) branch that makes some of these changes already. Should I base a MR off of one of those? I can't find any mention of them in the issue queue, and there are a number of other changes. Any guidance you can provide on your intentions would be appreciated! I want to help push this forward, but don't want to step on toes or duplicate efforts.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    @m.stena Probably related to this: https://www.drupal.org/project/jsonapi_schema/issues/3504440 ๐Ÿ› PHP8.3 compatibility issues Active

    Thanks @alexander-tallqvist!

    This error is actually a result of changes in the upstream Symfony class that we extend from. So I don't think it's specific to PHP 8.3.

    We can probably close ๐Ÿ› PHP8.3 compatibility issues Active as a duplicate of this issue, and incorporate the MR changes into here. I'll start a new MR and cherry-pick them over.

  • Merge request !13Drupal 11 โ†’ (Open) created by m.stenta
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Opened a merge request with @volha_si's commit from ๐Ÿ› PHP8.3 compatibility issues Active (I updated the commit message to be more descriptive of its purpose, but kept the author credit).

    I also added a commit that drops support for Drupal 8, 9, and 10. So this will need to be a new major release (not on the 8.x-1.x branch) - pending @bradjones1's response to my question above (#10).

    I will do more testing locally and report back if I find any other issues.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Not working yet...

    I tried to view a schema endpoint (/api/[entity-type]/[bundle]/resource/schema) in my browser and the following error occurs:

    Uncaught PHP Exception Symfony\\Component\\Serializer\\Exception\\NotNormalizableValueException: "Could not normalize object of type "Drupal\\Core\\Field\\BaseFieldDefinition", no supporting normalizer found." at /opt/drupal/vendor/symfony/serializer/Serializer.php line 181
    
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    The root /api/schema endpoint and collection endpoints (/api/[entity-type]/[bundle]/collection/schema) seem to be working.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Pushed a commit that fixes the /api/[entity-type]/[bundle]/resource/schema routes.

    This Drupal core change record describes the change that was necessary: https://www.drupal.org/node/3359695 โ†’

    Back to Needs Review!

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland Alexander Tallqvist

    Tested MR!13 on a fresh Drupal site running 11.1.5. The schemas at /api/[entity-type]/[bundle]/resource/schema seem to be working fine, and the implemented changes are in line with change records at https://www.drupal.org/node/3359695 โ†’ . Everything looks good to me! Setting to RTBTC.

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland dmitry.korhov Poland, Warsaw

    Hi all,

    Can we merge this MR?

    Thanks

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland dmitry.korhov Poland, Warsaw
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    @dmitry.korhov See my comment #10 above.

    Drupal 11 upgrades Symfony from version 7, which changes the method signatures in Symfony's NormalizerInterface. I think this means we need to drop support for Drupal 10.

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland dmitry.korhov Poland, Warsaw

    @m.stenta
    Other modules with the same issue addressed are compatible for both 10.3 and 11 versions of Drupal.
    So I see no reasons of dropping support of 10.3 for this module as well. See https://git.drupalcode.org/project/rest_views/-/merge_requests/19/diffs#... ( https://www.drupal.org/project/rest_views/issues/3451283 ๐Ÿ“Œ Automated Drupal 11 compatibility fixes for rest_views Active )

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    @dmitry.korhov It isn't the getSupportedTypes() method that's at issue, it is the normalize() method.

    See this commit in the MR: https://git.drupalcode.org/issue/jsonapi_schema-3431487/-/commit/7f424f1...

    (Originally cherry-picked from ๐Ÿ› PHP8.3 compatibility issues Active )

    This is the fatal error that it fixes:

    Fatal error: Declaration of Drupal\jsonapi_schema\Normalizer\DataDefinitionNormalizer::normalize($entity, $format = null, array $context = []) must be compatible with Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize(mixed $data, ?string $format = null, array $context = [])
    

    Is there a way to support that in Drupal 10 and 11? I would be happy to adjust the MR if there is, but if not then I think this is the best we can do.

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland dmitry.korhov Poland, Warsaw

    Hi m.stenta,

    Will it work with `#[\ReturnTypeWillChange]` attribute (https://php.watch/versions/8.1/ReturnTypeWillChange)?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    @dmitry.korhov: I haven't tried it, but it sounds like that is for silencing deprecation notices. In this case it's a fatal error, not a deprecation notice. So I don't think it will help.

    I'm inclined to just keep things simple and make a clean break between 1.x and 2.x of this module. Is there any reason not to do that? The upgrade from D10 to D11 will be seamless either way. It just means that sites will need to update their version constraint in composer.json from ^1.0 to ^2.0.

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland Alexander Tallqvist

    I don't see a problem with @m.stenta's suggestion in #24. +1 for this approach.

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland dmitry.korhov Poland, Warsaw

    Hi @m.stenta,

    When a project is large and complex, the upgrade process goes beyond simply updating composer.json.

    What weโ€™ve observed is that 99% of projects are currently supported on both Drupal 10.3 and 11. According to the Drupal 10.3.0 release notes, thereโ€™s little reason not to make modules compatible with both versions:

    Drupal 10.3 provides almost the same public API as Drupal 11.0, which will be released in August. The main differences between Drupal 10.3 and 11.0 are dependency changes and the removal of deprecated code. For more information, refer to the Drupal 11.0.0-beta1 release notes.

    Ensuring that the module is compatible with both 10.3 and 11 allows for a smoother, step-by-step upgrade process:
    1. Update all contributed modules to support both 10.3 and 11.
    2. Run regression tests on the 10.3-compatible versions (installed on a Drupal 10.3 site).
    3. Upgrade the core to Drupal 11.
    4. Run regression tests again, this time focused only on the core.

    So, if silencing a few deprecations enables broader compatibility and allows other installations to use the same module version across both Drupal 10.3 and 11, it would be greatly appreciated to make that small change. You might even test it with the deprecations silenced to confirm.

    Iโ€™m inclined to just keep things simple and make a clean break between 1.x and 2.x of this module. Is there any reason not to do that? The upgrade from D10 to D11 will be seamless either way.

    Of course, if this is a personal or project-specific preference, thatโ€™s totally understandable. That said, itโ€™s worth noting that the vast majority of other modules (around 99%) support both 10.3 and 11 in their 2.x branches, which seems to be a more practical approach for most use cases.

    Thanks for considering!

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    @dmitry.korhov: I looked closer and I think you're right that it's possible to support both Drupal 10 and 11. I mistakenly assumed that the method signature changes were a breaking change and that it wouldn't be possible to support both.

    Relatedly, I've started writing automated tests for this module in a merge request in ๐Ÿ“Œ Add basic test coverage Active (which leverages ๐Ÿ“Œ Add .gitlab-ci.yml Active ). These will be very helpful for continued development and bug fixing, and will also allow us to demonstrate that it works in both Drupal 10 and 11.

    There is still more work to be done to finish the first pass at automated tests, but I will rebase this issue's MR onto that one so that we can test this in parallel in the meantime.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Rebased on top of the commits from ๐Ÿ“Œ Add basic test coverage Active so that tests can run. I restored the declaration of Drupal 10 support in jsonapi_schema.info.yml. I also added a commit to ensure tests run on the latest major Drupal version (11). This was disabled in ๐Ÿ“Œ Add basic test coverage Active because they would fail.

    I think we still need to declare a minimum minor version of Drupal 10 (eg: ^10.1 or ^10.2).

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Tests are passing on both Drupal 10 and 11! ๐ŸŽ‰

    I think we still need to declare a minimum minor version of Drupal 10 (eg: ^10.1 or ^10.2).

    As far as I can tell, the only change that requires this is the one related to https://www.drupal.org/node/3359695 โ†’ , which was introduced in Drupal 10.1. I pushed an amended commit that updates the version constraint to ^10.1.

    This is ready for final review! (But of course must wait for ๐Ÿ“Œ Add basic test coverage Active to be merged.) If anyone has other concerns/considerations, now is the time to voice them! If I don't hear anything by next week then I will assume the previous RTBC applies, along with the newly restored Drupal 10 support per @dmitry.korhov's feedback.

    Thanks all!

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    Setting this back to Needs Work because the automated tests I'm working on in ๐Ÿ“Œ Add basic test coverage Active revealed a bug that will affect Drupal 11 compatibility.

    I have rebased on top of the commit in ๐Ÿ“Œ Add basic test coverage Active that causes the issue to demonstrate the failing test (notably it passes on 8.x-1.x).

    There was 1 error:
    1) Drupal\Tests\jsonapi_schema\Kernel\JsonApiSchemaTest::testJsonApiSchema
    PHPUnit\Framework\Exception: Uncaught PHP Exception TypeError: "Drupal\jsonapi_schema\Normalizer\ComplexDataDefinitionNormalizer::normalize(): Return value must be of type ArrayObject|array|string|int|float|bool|null, stdClass returned" at /builds/project/jsonapi_schema/src/Normalizer/ComplexDataDefinitionNormalizer.php line 51
    ERRORS!
    Tests: 1, Assertions: 0, Errors: 1.
    

    The declaration of normalize() return types revealed a bug with our current code. When the field module is enabled, there are cases where normalize() attempts to return a stdClass object, which is not allowed. I don't know the exact cause of this yet, but we'll have to figure it out before this can be merged.

    This existing issue seems to be closely related: ๐Ÿ› Decimal field causes error message on custom entity Active

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    This is where the stdClass object is coming from: https://git.drupalcode.org/project/jsonapi_schema/-/blob/ed083e4274feec8...

    It appears that the (object) cast was originally added in this commit (which created the normalizers): 08a4b2f2 (from #3060299: Remove TypeMapper plugins in favor of normalizers โ†’ ).

    ... despite the fact that the parent method's docblock declares a return type of array: https://git.drupalcode.org/project/jsonapi_schema/-/blob/08a4b2f2b930a6a...

    It appears @ tried to remove this in cd73c2f2, but then reverted that in d30041d3

    So we have some things to detangle here... :-/

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States m.stenta

    I rebased this branch on top of latest 8.x-1.x (with merged ๐Ÿ“Œ Add basic test coverage Active ), and added a commit that changes DataDefinitionUndefinedNormalizer::extractPropertyData() so that it returns an array instead of an object. The declared return type is array, so it was wrong for it to return an object, and now that causes a fatal error with Symfony 7+.

    Tests are all green (except PHPStan, which will be fixed separately: ๐Ÿ“Œ Fix PHPStan errors Active ). Setting this back to "Needs Review". Please review this most recent commit and set to RTBC so we can get this merged! :-)

  • ๐Ÿ‡ต๐Ÿ‡ฑPoland dmitry.korhov Poland, Warsaw

    Looks good

Production build 0.71.5 2024