Problem/Motivation

PHP Fatal error: Declaration of Drupal\smart_date\Normalizer\SmartDateItemNormalizer::normalize($object, $format = null, array $context = []): ArrayObject|array|string|int|float|bool|null must be compatible with Drupal\serialization\Normalizer\TimestampItemNormalizer::normalize($object, $format = null, array $context = []): array in /var/www/html/web/modules/contrib/smart_date/src/Normalizer/SmartDateItemNormalizer.php on line 30

Steps to reproduce

Upgrade Drupal to 11.4.x-dev, witness the error.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork smart_date-3591184

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

ltrain created an issue. See original summary.

ishani patel made their first commit to this issue’s fork.

ishani patel’s picture

Hello,
I've raised MR for this.
Kindly check and review it.

Thank you!

ltrain’s picture

@ishani The MR works perfectly. Thanks!

ltrain’s picture

Status: Active » Needs review
dalin’s picture

Priority: Normal » Critical

mandclu made their first commit to this issue’s fork.

mandclu’s picture

I'm concerned that this change will break any site running Drupal <11.4, which would be its own critical issue.

I don't love the idea of having to have a new version that would only support Drupal 11.4+. Are there alternatives we could consider?

dalin’s picture

@mandclu

With the help of AI

> The good news is that return types in PHP are covariant: an overriding/implementing method may declare a narrower return type than the interface it implements, but not a wider one. Since array is one of the members of the union array|string|int|float|bool|\ArrayObject|null, declaring your method as returning plain array is a valid covariant override of an interface that declares the wider union. So this single declaration satisfies both the new interface (: array) and the old one (: array|string|int|float|bool|\ArrayObject|NULL):

mandclu’s picture

Thanks for that @dalin. I was able to load a patched version of Smart Date in Drupal 11.3 and didn't see any errors. That said, I didn't realize at the time that there is actually more required to trigger the error.

Also leaning into our robot overlords for advice:

The reason you likely can't reproduce it: the fatal error only fires when SmartDateItemNormalizer is actually autoloaded, which only happens during a serialization request. It won't appear on normal page loads. To reproduce it you need:

The serialization module enabled (it's the one that changed the parent class signature). JSON:API or REST module being enabled is the typical trigger.
A content type with a Smart Date field.
An actual serialization request against a node/entity with that field — e.g., a JSON:API GET: GET /jsonapi/node/article/<uuid> or a REST endpoint call.

If your local site doesn't use REST/JSON:API, or doesn't have the serialization module enabled, you'd never load the normalizer class and would never see the error.

I was ultimately able to reproduce the error by enabling JSON:API (and not before). Subsequently I was able to reproduce the error on the command line with (using a node with a Smart Date field):

ddev drush php-eval "\Drupal::service('serializer')->normalize(\Drupal\node\Entity\Node::load(1), 'application/json');"

This allowed me to confirm that the provided patch does, in fact resolve the fatal error. I feel like I should give contribution credit to Claude as well :P

mandclu’s picture

Version: 4.2.5 » 4.2.x-dev
Status: Needs review » Fixed

Thanks for the work here. I will roll a new release with this fix.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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